JAVA JDBC示例

try {
     // 加载MySql的驱动类
     Class.forName("com.mysql.jdbc.Driver");
    } catch (ClassNotFoundException e) {
     System.out.println("找不到驱动程序类 ,加载驱动失败!");
     e.printStackTrace();
     return;
    }

    // 连接MySql数据库,用户名和密码都是root
    String url = "jdbc:mysql://192.168.0.216:3306/rzzxmaindb?useUnicode=true&characterEncoding=utf8";
    String username = "root";
    String password = "root";

    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;

    List<InsideuserInfo> list = new ArrayList();
    try {
     conn = DriverManager.getConnection(url, username, password);
     stmt = conn.createStatement();
     rs = stmt.executeQuery("SELECT * FROM t_insideuser");

     while (rs.next()) {
      InsideuserInfo info = new InsideuserInfo();
      info.setUsername(rs.getString("username"));
      info.setUsertel(rs.getString("usertel"));

      list.add(info);
     }

    } catch (SQLException se) {
     System.out.println("数据库连接失败!");
     se.printStackTrace();
    }
    if (rs != null) { // 关闭记录集
     try {
      rs.close();
     } catch (SQLException e) {
      e.printStackTrace();
     }
    }
    if (stmt != null) { // 关闭声明
     try {
      stmt.close();
     } catch (SQLException e) {
      e.printStackTrace();
     }
    }
    if (conn != null) { // 关闭连接对象
     try {
      conn.close();
     } catch (SQLException e) {
      e.printStackTrace();
     }
    }

    // ///
    url = "jdbc:mysql://192.168.0.216:3306/accesscontroldb?useUnicode=true&characterEncoding=utf8";
    username = "root";
    password = "root";

    conn = null;
    stmt = null;
    rs = null;

    try {
     conn = DriverManager.getConnection(url, username, password);
     stmt = conn.createStatement();
     int n = 0;
     for (int i = 0; i < list.size(); i++) {
      InsideuserInfo info = list.get(i);
      System.out.println(info.getUsername());
      String fid = "0835DB4591DE41A4AB1EFC454F42C" + i;

      int rows = stmt
        .executeUpdate("INSERT INTO accesscontroldb.sys_user (fID, VERSION, fUserCode, fUserName, fGender, fIDCard, fTelephone, fDeptId, fState) VALUES ('"
          + fid
          + "', 0, '"
          + info.getUsertel()
          + "', '"
          + info.getUsername()
          + "', 'M', '"
          + info.getUsertel()
          + "', '"
          + info.getUsertel()
          + "', '92E41919D4134FC78A411F00C2EB1E9E', 1);");
      if (rows > 0) {
       n++;
      }
     }

     System.out.println("插入数据:" + n);

    } catch (SQLException se) {
     System.out.println("数据库连接失败!");
     se.printStackTrace();
    }
    if (rs != null) { // 关闭记录集
     try {
      rs.close();
     } catch (SQLException e) {
      e.printStackTrace();
     }
    }
    if (stmt != null) { // 关闭声明
     try {
      stmt.close();
     } catch (SQLException e) {
      e.printStackTrace();
     }
    }
    if (conn != null) { // 关闭连接对象
     try {
      conn.close();
     } catch (SQLException e) {
      e.printStackTrace();
     }
    } 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值