当前位置:首页 > 开发 > 编程语言 > 编程 > 正文

批量更新表的数据

发表于: 2012-09-04   作者:bozch   来源:转载   浏览:
摘要: Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); Connection conn = DriverManager.getConnection( "jdbc:sqlserver://localhost:1443;databasename=zxtplat_ExamOnLine", &qu
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();

批量更新表的数据

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号