转自:http://blog.csdn.net/jwlsky/article/details/7516274
public void exec3(String sql) {
try {
Connection conn = null;
conn = getConnection();
Long beginTime = System.currentTimeMillis();
PreparedStatement pst = conn
.prepareStatement(sql);
for (int i = 1; i <= 10000; i++) {
pst.setInt(1, i);
pst.setString(1, String.valueOf(i));
pst.addBatch();
if (i % 1000 == 0) {// 可以设置不同的大小;如50,100,500,1000等等
pst.executeBatch();
//conn.commit(); //PreparedStatement是自动提交,不需要该语句
pst.clearBatch();
}
}
Long endTime = System.currentTimeMillis();
System.out.println("pst+batch:" + (endTime - beginTime) / 1000
+ "秒");
pst.close();
conn.close();
} catch (Exception e) {
// TODO Auto-generated catchblock
e.printStackTrace();
}
}
public Connection getConnection() throws DaoException {
log.info("数据层:获取数据库的JDBC连接");
try {
return getSession().connection();
} catch (Exception e) {
throw new DaoException(DaoException.ERRORCODE_CONNECTION);
}
}