Java Statement和PreparedStatement性能测试(转)

时间:2023-03-09 15:05:41
Java Statement和PreparedStatement性能测试(转)

本文转载自http://blog.****.net/liubo5005/article/details/7239935

先上代码:

  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.PreparedStatement;
  4. import java.sql.Statement;
  5. public class TestSql {
  6. public static void main(String[] args) throws Exception {
  7. testStatement();
  8. testBatchPreparedStatement();
  9. testBatchPreparedStatement();
  10. }
  11. public static void testStatement() throws Exception {
  12. Class.forName("com.ibm.db2.jcc.DB2Driver").newInstance();
  13. String url = "jdbc:db2://172.17.252.68:60012/glhssdb";
  14. Connection con = DriverManager.getConnection(url, "ppapdb2", "ppapdb2");
  15. Statement st = con.createStatement();
  16. Long beginTime1 = System.currentTimeMillis();
  17. System.out.print("insert...");
  18. for (int i = 0; i < 100000; i++) {
  19. String sql = "insert into GL_HISDB.TESTSQL(id,name) values (" + i
  20. + ",'" + i + "')";
  21. st.executeUpdate(sql);
  22. }
  23. Long endTime1 = System.currentTimeMillis();
  24. System.out.println("st:" + (endTime1 - beginTime1) / 1000 + "秒");// 计算时间
  25. st.close();
  26. con.close();
  27. }
  28. public static void testPreparedStatement() throws Exception {
  29. Class.forName("com.ibm.db2.jcc.DB2Driver").newInstance();
  30. String url = "jdbc:db2://172.17.252.68:60012/glhssdb";
  31. Connection con = DriverManager.getConnection(url, "ppapdb2", "ppapdb2");
  32. PreparedStatement pst = con
  33. .prepareStatement("insert into GL_HISDB.TESTSQL(id,name) values (?,?)");
  34. Long beginTime1 = System.currentTimeMillis();
  35. System.out.print("insert...");
  36. for (int i = 0; i < 100000; i++) {
  37. pst.setInt(1, i);
  38. pst.setString(2, "" + i);
  39. pst.execute();
  40. }
  41. Long endTime1 = System.currentTimeMillis();
  42. System.out.println("pst:" + (endTime1 - beginTime1) / 1000 + "秒");// 计算时间
  43. pst.close();
  44. con.close();
  45. }
  46. public static void testBatchPreparedStatement() throws Exception {
  47. Class.forName("com.ibm.db2.jcc.DB2Driver").newInstance();
  48. String url = "jdbc:db2://172.17.252.68:60012/glhssdb";
  49. Connection con = DriverManager.getConnection(url, "ppapdb2", "ppapdb2");
  50. PreparedStatement pst = con
  51. .prepareStatement("insert into GL_HISDB.TESTSQL(id,name) values (?,?)");
  52. Long beginTime1 = System.currentTimeMillis();
  53. con.setAutoCommit(false);// 手动提交
  54. System.out.print("insert...");
  55. for (int i = 0; i < 100000; i++) {
  56. pst.setInt(1, i);
  57. pst.setString(2, "" + i);
  58. pst.addBatch();
  59. if (i % 1000 == 0) {// 可以设置不同的大小;如50,100,500,1000等等
  60. pst.executeBatch();
  61. con.commit();
  62. pst.clearBatch();
  63. }
  64. }
  65. Long endTime1 = System.currentTimeMillis();
  66. System.out.println("pst batch:" + (endTime1 - beginTime1) / 1000 + "秒");// 计算时间
  67. pst.close();
  68. con.close();
  69. }
  70. }

三种调用方式Statment、PreparedStatement 以及PreparedStatement  Batch,往DB2数据库插入10万条数据,跑出来的时间为:

1、insert...st:291秒 2、insert...pst:150秒 3、insert...pst batch:5秒

总结引用经典:在JDBC应用中,如果你已经是稍有水平开发者,你就应该始终以PreparedStatement代替Statement.也就是说,在任何时候都不要使用Statement。