当需要成批插入或者更新记录时。可以采用 Java 的批量更新机制,这一机制允许多条语句一次性提交给数据库批量处理。通常这种情况下比单独提交处理更有效率。
JDBC 的批量处理语句包括以下两个方法:
1)addBatch(String):添加需要批量处理的 SQL 语句或是参数
2)executeBatch():执行批量处理语句
通常会遇到两种批量执行 SQL 语句的情况:
1)多条 SQL 语句的批量处理
2)一个 SQL 语句的批量传参
@Test
public void testBatch(){
Connection connection = null;
PreparedStatement preparedStatement = null;
String sql = null;
try {
connection = JDBCTools.GetConnection();
JDBCTools.beginTransaction(connection);
sql = "INSERT INTO mytable VALUES(?, ?)";
preparedStatement = connection.prepareStatement(sql);
Date date = new Date(new Date().getTime());
long begin = System.currentTimeMillis();
for(int i = 0; i < 10000; i++){
preparedStatement.setInt(1, i + 1);
preparedStatement.setString(2, "name_" + i);
//积攒 SQL
preparedStatement.addBatch();
//当积攒到一定程度就统一的执行一次,并且清空先前积攒的 SQL
if((i + 1) % 300 == 0){
preparedStatement.executeBatch();
preparedStatement.clearBatch();
}
}
//若总条数不是批量数值的整数倍,则还需要在额外的执行一次
if(10000 % 300 != 0){
preparedStatement.executeBatch();
preparedStatement.clearBatch();
}
long end = System.currentTimeMillis();
System.out.println("Time: " + (end - begin));//746
JDBCTools.commit(connection);
} catch (Exception e) {
e.printStackTrace();
JDBCTools.rollback(connection);
}finally{
JDBCTools.release(null, preparedStatement, connection);
}
}
@Test
public void testBatchWithPreparedStatement(){
Connection connection = null;
PreparedStatement preparedStatement = null;
String sql = null;
try {
connection = JDBCTools.GetConnection();
JDBCTools.beginTransaction(connection);
sql = "INSERT INTO mytable VALUES(?, ?)";
preparedStatement = connection.prepareStatement(sql);
Date date = new Date(new Date().getTime());
long begin = System.currentTimeMillis();
for(int i = 0; i < 10000; i++){
preparedStatement.setInt(1, i + 1);
preparedStatement.setString(2, "name_" + i);
preparedStatement.executeUpdate();
}
long end = System.currentTimeMillis();
System.out.println("Time: " + (end - begin));//1041
JDBCTools.commit(connection);
} catch (Exception e) {
e.printStackTrace();
JDBCTools.rollback(connection);
}finally{
JDBCTools.release(null, preparedStatement, connection);
}
}
@Test
public void testJDBCBatch() {
Connection connection = null;
Statement statement = null;
String sql = null;
try {
connection = JDBCTools.GetConnection();
JDBCTools.beginTransaction(connection);
statement = connection.createStatement();
long begin = System.currentTimeMillis();
for(int i = 0; i < 10000; i++){
sql = "INSERT INTO mytable VALUES(" + (i + 1) + ", '"+ "name_" + i +"')";
statement.executeUpdate(sql);
}
long end = System.currentTimeMillis();
System.out.println("Time: " + (end - begin));//4161
JDBCTools.commit(connection);
} catch (Exception e) {
e.printStackTrace();
JDBCTools.rollback(connection);
}finally{
JDBCTools.release(null, statement, connection);
}
}
public class JDBCTools {
//提交事務
public static void commit(Connection connection){
if(connection != null){
try {
connection.commit();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
//回滾 事務
public static void rollback(Connection connection){
if(connection != null){
try {
connection.rollback();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
//开始事务
public static void beginTransaction(Connection connection){
if(connection != null){
try {
connection.setAutoCommit(false);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
//关闭资源
public static void release(ResultSet rs, Statement statement,
Connection conn) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (statement != null)
try {
statement.close();
} catch (Exception e2) {
e2.printStackTrace();
}
if (conn != null)
try {
conn.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
/*
* 1. 获取连接的方法 通过读配置文件从数据库服务器获取一个连接
*/
public static Connection GetConnection() throws Exception {
String driverClass = null;
String jdbcUrl = null;
String user = null;
String password = null;
InputStream in = JDBCTools.class.getClassLoader().getResourceAsStream(
"jdbc.properties");
Properties properties = new Properties();
properties.load(in);
driverClass = properties.getProperty("driver");
jdbcUrl = properties.getProperty("jdbcUrl");
user = properties.getProperty("user");
password = properties.getProperty("password");
Driver driver = (Driver) Class.forName(driverClass).newInstance();
Properties info = new Properties();
info.put("user", user);
info.put("password", password);
Connection connection = driver.connect(jdbcUrl, info);
return connection;
}
}