高效批处理
public void insert() {
Connection conn=null;
PreparedStatement pstmt=null;
try {
long start=System.currentTimeMillis();
conn = JDBCUtils.getConnection();
conn.setAutoCommit(false);
String sql="insert into student(sno)values(?)";
pstmt= conn.prepareStatement(sql);
for (int i=0; i<1000000;i++){
pstmt.setObject(1,i);
pstmt.addBatch();
if (i%500==0){
pstmt.executeBatch();
pstmt.clearBatch();
}
}
conn.commit();
long end =System.currentTimeMillis();
System.out.println(end-start);
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtils.closeResoure(conn,pstmt);
}
}