用jdbc虽然对程序开发效率较低,由于其封装性较低,其在数据存储访问时具有更高的性能,这点上是其他orm框架不能比拟的,因此如果对数据存取性能要求高,存取量巨大时直接使用jdbc编写程序则满足需求。
存取大量数据时用update效率还是很低的,建议使用addBatch
实例如下
conn.setAutoCommit(false);//把自动提交改为手动提交 long start=System.currentTimeMillis(); //获取开始时间 state=conn.prepareStatement("insert into student(sno,sname,ssex,sage,clno) values(?,'姓名','男',20,311)"); for(int i=0;i<100000;i++){ state.setDouble(1,i);//使学号不同 state.addBatch(); if (i % 5000 == 0){ state.executeBatch(); conn.commit();//每5000条提交一次 } } state.executeBatch(); conn.commit(); long end=System.currentTimeMillis(); //获取结束时间 System.out.println("程序运行时间: "+(end-start)+"ms");
十万条插入语句运行时间接近20s
用hibernate插入同样的数据用时更久