mysql for 批处理语句_Mysql 批处理多条sql语句

packagecn.itcast.demo;importjava.sql.Connection;importjava.sql.PreparedStatement;importjava.sql.ResultSet;importjava.sql.Statement;importorg.junit.Test;importcn.itcast.utils.JdbcUtils;public classDemo3 {/*** jdbc批处理的两种方式:statement 和 preparedstatement

create table testbatch

(

id int primary key,

name varchar2(20)

);*/@Testpublic voidtestbatch1(){

Connection conn= null;

Statement st= null;

ResultSet rs= null;try{

conn=JdbcUtils.getConnection();

String sql1= "insert into testbatch(id,name) values(1,'aaa')";

String sql2= "insert into testbatch(id,name) values(2,'bbb')";

String sql3= "delete from testbatch where id=1";

st=conn.createStatement();

st.addBatch(sql1);

st.addBatch(sql2);

st.addBatch(sql3);

st.executeBatch();

st.clearBatch();

}catch(Exception e) {

e.printStackTrace();

}finally{

JdbcUtils.release(conn, st, rs);

}

}

@Testpublic voidtestbatch2(){long starttime =System.currentTimeMillis();

Connection conn= null;

PreparedStatement st= null;

ResultSet rs= null;try{

conn=JdbcUtils.getConnection();

String sql= "insert into testbatch(id,name) values(?,?)";

st=conn.prepareStatement(sql);for(int i=1;i<10000008;i++){ //i=1000 2000

st.setInt(1, i);

st.setString(2, "aa" +i);

st.addBatch();if(i%1000==0){

st.executeBatch();

st.clearBatch();

}

}

st.executeBatch();

}catch(Exception e) {

e.printStackTrace();

}finally{

JdbcUtils.release(conn, st, rs);

}long endtime =System.currentTimeMillis();

System.out.println("程序花费时间:" + (endtime-starttime)/1000 + "秒!!");

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值