批量插入的代码java_java ssm批量插入代码

private String url = "jdbc:mysql://localhost:3306/test?rewriteBatchedStatements=true";

private String user = "root";

private String password = "123456";

//    private String msql = "INSERT INTO  `orders`( `wid`, `pid`, `brand_id`, `orderid`, `state`, `pay_type`, `trade_type`, `ordertype`, `vender_id`, `order_source`, `category_id`, `vender_order_date`, `wname`, `uname`, `enc_uname`, `telephone`, `province`, `city`, `ask_price`, `ask_price_max`, `final_amount`, `gain_amount`, `wnum`, `created`, `distribute_time`, `modified`)" +

//            "  VALUES (200118231, 144815, 10201, '1000001454', 9, 3, 1, 4, 200118, 2, 303, '2018-06-25 18:25:35', '奢侈品测试', 'testcai1', 'fzXl4RnftkWNYabXJm8HEA==', '159****5487', '北京', '朝阳区', 123.00, 1231.00, 0.00, 0.00, 1, '2018-06-25 18:25:34', NULL, '2018-06-25 18:40:03')";

private String msqlvalue1 ="(200155531, 155515, 13331, '1000044454', 9, 3, 1, 4, 200118, 2, 303, '2017-06-25 18:25:35', '奢侈品测试', 'testcai1', 'fzXl4RnftkWNYabXJm8HEA==', '159****5487', '北京', '朝阳区', 123.00, 1231.00, 0.00, 0.00, 1, '2017-06-25 18:25:34', NULL, '2017-06-25 18:40:03')";

private String msqlvalue2="(202255531, 133515, 11131, '1002244454', 9, 3, 1, 4, 211118, 2, 303, '2017-02-25 18:25:35', '奢侈品测试', 'testcai1', 'fzXl4RnftkWNYabXJm8HEA==', '159****5487', '北京', '朝阳区', 123.00, 1231.00, 0.00, 0.00, 1, '2017-02-25 18:25:34', NULL, '2017-02-25 18:40:03')";

private String msql = "INSERT into `order` (`updateat`) VALUES ('2015-04-25 18:25:34'), ('2018-01-25 18:25:34'),('2017-02-25 18:25:34')";

@Test

public void Test() {

Connection conn = null;

Statement pstm = null;

ResultSet rt = null;

try {

Class.forName("com.mysql.jdbc.Driver");

conn = DriverManager.getConnection(url, user, password);

conn.setAutoCommit(false);

pstm = conn.createStatement();

Long startTime = System.currentTimeMillis();

for(int i = 0 ; i < 3; i++){

if (i %3 == 0){

msql += ",('2016-02-25 18:25:34')";

}else {

msql += ",('2016-11-25 18:25:34')";

}

}

for (int i = 1; i <= 2000000; i++) {

pstm.addBatch(msql);

}

pstm.executeBatch();

conn.commit();

Long endTime = System.currentTimeMillis();

System.out.println("OK,用时:" + (endTime - startTime));

} catch (Exception e) {

e.printStackTrace();

throw new RuntimeException(e);

} finally {

if (pstm != null) {

try {

pstm.close();

} catch (SQLException e) {

e.printStackTrace();

throw new RuntimeException(e);

}

}

if (conn != null) {

try {

conn.close();

} catch (SQLException e) {

e.printStackTrace();

throw new RuntimeException(e);

}

}

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值