问题描述
第一个没用批处理,时间反而短。。。第二个用了批处理,时间反而长publicstaticvoidadd1(){Connectionconn=null;PreparedStatementps=null;try{conn=DBManager.getConection();Stringsql="insertintouser1values(?,?)";longbegin=System.currentTimeMillis();ps=conn.prepareStatement(sql);for(inti=1;i<10000;i++){ps.setString(1,"name"+i);ps.setInt(2,i);ps.executeUpdate();}longend=System.currentTimeMillis();System.out.println(end-begin);//时间8790//批处理publicstaticvoidadd2()throwsSQLException{Connectionconn=null;PreparedStatementps=null;conn=DBManager.getConection();Stringsql="insertintouser1values(?,?)";longbegin=System.currentTimeMillis();ps=conn.prepareStatement(sql);for(inti=1;i<10000;i++){ps.setString(1,"name"+i);ps.setInt(2,i);ps.addBatch();if((i+1)%1000==0){ps.executeBatch();ps.clearBatch();}ps.executeUpdate();}longend=System.currentTimeMillis();System.out.println(end-begin);时间24536不知道我哪里写错了。。新手。。谢谢
解决方案
解决方案二:
//批处理publicstaticvoidadd2()throwsSQLException{Connectionconn=null;PreparedStatementps=null;conn=DBManager.getConection();Stringsql="insertintouser1values(?,?)";longbegin=System.currentTimeMillis();ps=conn.prepareStatement(sql);for(inti=1;i<10000;i++){ps.setString(1,"name"+i);ps.setInt(2,i);ps.addBatch();if((i+1)%1000==0){ps.executeBatch();ps.clearBatch();}ps.executeUpdate();//楼主这里是什么意思啊用ps.executeBatch();试试}longend=System.currentTimeMillis();System.out.println(end-begin);
解决方案三:
刚刚说错了//批处理publicstaticvoidadd2()throwsSQLException{Connectionconn=null;PreparedStatementps=null;conn=DBManager.getConection();Stringsql="insertintouser1values(?,?)";longbegin=System.currentTimeMillis();ps=conn.prepareStatement(sql);for(inti=1;i<10000;i++){ps.setString(1,"name"+i);ps.setInt(2,i);ps.addBatch();if((i+1)%1000==0){ps.executeBatch();ps.clearBatch();}}ps.executeBatch();longend=System.currentTimeMillis();System.out.println(end-begin);
你每次循环里面调用ps.executeUpdate();等于没有批量更新
解决方案四:
for(inti=1;i<10000;i++){ps.setString(1,"name"+i);ps.setInt(2,i);ps.addBatch();}ps.executeUpdate();
解决方案五:
for(inti=1;i<10000;i++){ps.setString(1,"name"+i);ps.setInt(2,i);ps.addBatch();}ps.executeBatch();
解决方案六:
在javaEE中,通过浏览器浏览:http://127.0.0.1/TestApp/HelloServlet,(HelloServlet配置完全正确,并之前已经加载HelloServlet的实例),