java使用mysql-connector-java批处理数据库:更新、删除、插入

    //批处理:更新、删除、插入
    public static void batch() throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection conn = DriverManager.getConnection("jdbc:mysql://192.168.10.102/?useUnicode=true&characterEncoding=UTF-8", "账号", "密码");
        PreparedStatement pstmt = conn.prepareStatement("update sql.t_backend_user_role set create_time=? where id=?");//更新数据
//        PreparedStatement pstmt = conn.prepareStatement("delete from sql.t_backend_user_role where id=?");//删除数据
//        PreparedStatement pstmt = conn.prepareStatement("insert into sql.t_backend_user_role(id,role_id,backend_user_id,create_time) values(?,?,9,'2017-05-22 23:29:27')");//插入数据
        for (int i = 1; i < 4; i++) {
            pstmt.setString(1, "2017-05-23 00:00:0" + i);
            pstmt.setInt(2, i);
            pstmt.addBatch();//添加到同一个批处理中
        }
        System.out.println("执行批处理结果:" + Arrays.toString(pstmt.executeBatch()));
        pstmt.close();
        conn.close();
    }

 

你可能感兴趣的:(程序开发)