批量更新(batch)

package com.jdbctest.batch;
import com.jdbctest.Util.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

//PreparedStatement接口的批量更新
public class PreparedStatementBatchtest {

public static void main(String args[]){
Connection conn = null;
PreparedStatement ps = null;

conn = JDBCUtil.getConnection();

String sql="INSERT INTO score(sno,sname,score)"+"VALUES(?,?,?)";

try {
long start =System.currentTimeMillis();
ps = conn.prepareStatement(sql);
for(int i = 0 ; i<10000;i++){
ps.setString(1, "20060800"+i);
ps.setString(2, "wuyu");
ps.setString(4, 88);
ps.addBatch();
}
ps.executeBatch();
long end=System.currentTimeMillis();
System.out.println("用时: "+(end-start));
} catch (SQLException e) {

e.printStackTrace();
}

}


}


package com.jdbctest.batch;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

import com.jdbctest.Util.JDBCUtil;

/**
* Statement 批量更新
* @author wuyu
*
*/
public class StatementBatchTest {

/**
* @param args
*/
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;

conn = JDBCUtil.getConnection();

try {long begin=System.currentTimeMillis();
stmt = conn.createStatement();
for(int i = 1; i<10000;i++){
stmt.addBatch("INSERT INTO score(sno,sname,score)"+"VALUES('20006080"+i+"','wuyu',88.9)");

}
stmt.executeBatch();
long end = System.currentTimeMillis();
System.out.println("用时:"+(end-begin));
} catch (SQLException e) {
e.printStackTrace();
}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值