Java Statement和PreparedStatement性能测试

2 篇文章 0 订阅

 先上代码:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Statement;

public class TestSql {
 
 public static void main(String[] args) throws Exception {
  testStatement();
  testBatchPreparedStatement();
  testBatchPreparedStatement();
 }

 public static void testStatement() throws Exception {
  Class.forName("com.ibm.db2.jcc.DB2Driver").newInstance();
  String url = "jdbc:db2://172.17.252.68:60012/glhssdb";
  Connection con = DriverManager.getConnection(url, "ppapdb2", "ppapdb2");
  Statement st = con.createStatement();

  Long beginTime1 = System.currentTimeMillis();
  System.out.print("insert...");
  for (int i = 0; i < 100000; i++) {

   String sql = "insert into GL_HISDB.TESTSQL(id,name) values (" + i
     + ",'" + i + "')";

   st.executeUpdate(sql);
  }
  Long endTime1 = System.currentTimeMillis();
  System.out.println("st:" + (endTime1 - beginTime1) / 1000 + "秒");// 计算时间
  st.close();
  con.close();
 }

 public static void testPreparedStatement() throws Exception {
  Class.forName("com.ibm.db2.jcc.DB2Driver").newInstance();
  String url = "jdbc:db2://172.17.252.68:60012/glhssdb";
  Connection con = DriverManager.getConnection(url, "ppapdb2", "ppapdb2");
  PreparedStatement pst = con
    .prepareStatement("insert into GL_HISDB.TESTSQL(id,name) values (?,?)");

  Long beginTime1 = System.currentTimeMillis();
  System.out.print("insert...");
  for (int i = 0; i < 100000; i++) {

   pst.setInt(1, i);
   pst.setString(2, "" + i);
   pst.execute();
  }
  Long endTime1 = System.currentTimeMillis();
  System.out.println("pst:" + (endTime1 - beginTime1) / 1000 + "秒");// 计算时间
  pst.close();
  con.close();
 }

 public static void testBatchPreparedStatement() throws Exception {
  Class.forName("com.ibm.db2.jcc.DB2Driver").newInstance();
  String url = "jdbc:db2://172.17.252.68:60012/glhssdb";
  Connection con = DriverManager.getConnection(url, "ppapdb2", "ppapdb2");
  PreparedStatement pst = con
    .prepareStatement("insert into GL_HISDB.TESTSQL(id,name) values (?,?)");

  Long beginTime1 = System.currentTimeMillis();
  con.setAutoCommit(false);// 手动提交
  System.out.print("insert...");
  for (int i = 0; i < 100000; i++) {

   pst.setInt(1, i);
   pst.setString(2, "" + i);
   pst.addBatch();

   if (i % 1000 == 0) {// 可以设置不同的大小;如50,100,500,1000等等
    pst.executeBatch();
    con.commit();
    pst.clearBatch();
   }

  }
  Long endTime1 = System.currentTimeMillis();
  System.out.println("pst batch:" + (endTime1 - beginTime1) / 1000 + "秒");// 计算时间
  pst.close();
  con.close();
 }
}


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

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

总结引用经典: 在JDBC应用中,如果你已经是稍有水平开发者,你就应该始终以PreparedStatement代替Statement.也就是说,在任何时候都不要使用Statement。
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值