`
bozch
  • 浏览: 456352 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

批量更新表的数据

 
阅读更多
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); Connection conn = DriverManager.getConnection( "jdbc:sqlserver://localhost:1443;databasename=zxtplat_ExamOnLine", "sa", "sa123456"); conn.setAutoCommit(false); String sql = "select userId,username from t_usertable where userid not in (1,2,3,4,5,6)"; //查询结果集 Statement stmt = conn.createStatement(); String presql = "update t_usertable set userpassword=? where userid=?"; PreparedStatement pstmt = (PreparedStatement) conn.prepareStatement(presql); ResultSet rs = stmt.executeQuery(sql); int i = 0; while (rs.next()) { int a = rs.getInt("userId"); String user = rs.getString("username"); if(user == null || "".equals(user)){ user = "1"; } pstmt.setString(1, new MD5().getMD5ofStr(user.trim())); pstmt.setInt(2,a); pstmt.addBatch(); i++; if(i%50 == 0){ pstmt.executeBatch(); i = 0; } } conn.commit(); pstmt.close(); stmt.close(); conn.close();
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics