Java Statement和PreparedStatement性能测试

 先上代码:

[java]  view plain copy
  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。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值