JDBC中批处理的应用
preparedStatement.setInt(1,i);
preparedStatement.setString(2,"zjc"+i);
preparedStatement.addBatch();
preparedStatement.executeBatch();
preparedStatement.clearBatch();
connection.commit(); //事物提交
public class zzz {
public static void main(String[] args) {
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
int i = 0;
try {
String sql = "insert into jdbc values(?,?)";
connection = JDBCUtils.getConnection();
connection.setAutoCommit(false);//开启事物,设置自动提交为false。
preparedStatement = connection.prepareStatement(sql);
while(i < 5000){
preparedStatement.setInt(1,i);
preparedStatement.setString(2,"zjc"+i);
preparedStatement.addBatch();
i ++;
}
preparedStatement.executeBatch();
preparedStatement.clearBatch();
connection.commit(); //事物提交
} catch (Exception E) {
E.printStackTrace();
} finally {
JDBCUtils.close(resultSet,preparedStatement,connection);
}
}
}