zoukankan      html  css  js  c++  java
  • Java Statement和PreparedStatement性能测试(转)

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

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

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

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

  • 相关阅读:
    集合容器
    洛谷P3953 逛公园
    洛谷P1967 货车运输
    洛谷P1073 最优贸易
    洛谷P4568 [JLOI2011]飞行路线
    洛谷P1265 公路修建
    洛谷P1503 鬼子进村
    洛谷P1613 跑路
    洛谷P4933 大师
    洛谷P4017 最大食物链计数
  • 原文地址:https://www.cnblogs.com/abc8023/p/4727137.html
Copyright © 2011-2022 走看看