您的位置:首页 > 其它

利用JDBC批量处理语句

2017-07-14 11:45 197 查看
      有时候我们想要在本地数据库的一个表中插入大量的数据,这时候如果用普通的方法,插入的速度就会很慢,所以JDBC中给我们提供了一个方法,用来批量的插入数据,该方法在PreparedStatement对象中,方法名为addBatch(String s),其中的参数根据情况传入,可以有参数,也可以无参数,有参数的情况是一条SQL语句的批量传参,而有参数的情况是多条语句的批量处理,这个方法可以将每一条准备好的SQL语句先保留下来,然后由开发人员设置其保存的最大容量,当达到其容量后,就可以调用executeBatch()方法批量执行SQL语句,执行完后调用clearBatch()清空执行过的SQL语句,以便于准备下一次批量处理。
      下面是一个一条语句批量传参的实例:
现在本地数据库中有这样一个表:



向其中插入100000条记录,看如下代码:
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import org.junit.Test;
/*
* 使用JDBC批量处理语句
*/
public class TestBatch {
@Test
public void TestBatch(){//使用addBatch()方法批量处理SQL语句
Connection con = null;
PreparedStatement ps = null;
String sql = "INSERT INTO customer(id,name,date) VALUES(?,?,?)";
try {
con = JDBCTools.getConnection();
con.setAutoCommit(false);//取消事务执行完后自动提交
ps = con.prepareStatement(sql);
Date date = new Date(new java.util.Date().getTime());
long start = System.currentTimeMillis();
for(int i = 0;i < 100000;i++){
ps.setInt(1, i+1);
ps.setString(2, "name"+(i + 1));
ps.setDate(3, date);

ps.addBatch();//累积SQL
if((i + 1) % 1000 == 0){
ps.executeBatch();//累积到一定数量时开始执行
ps.clearBatch();//执行完后清空
}
}
long end = System.currentTimeMillis();
con.commit();
System.out.println("Time:"+(end - start));//Time:1647
} catch (Exception e) {
e.printStackTrace();
} finally{
JDBCTools.release(null, con, ps);
}
}
@Test
p
4000
ublic void TestBathUsePreparedStatement(){//使用PreparedStatement插入100000条数据
Connection con = null;
PreparedStatement ps = null;
String sql = "INSERT INTO customer(id,name,date) VALUES(?,?,?)";

try {
con = JDBCTools.getConnection();
con.setAutoCommit(false);//取消事务执行完后自动提交
ps = con.prepareStatement(sql);
Date date = new Date(new java.util.Date().getTime());
long start = System.currentTimeMillis();

for(int i = 0;i < 100000;i++){
ps.setInt(1, i+1);
ps.setString(2, "name"+(i + 1));
ps.setDate(3, date);
ps.executeUpdate();
}
long end = System.currentTimeMillis();
con.commit();
System.out.println("Time:"+(end - start));//Time:13972
} catch (Exception e) {
e.printStackTrace();
} finally{
JDBCTools.release(null, con, ps);
}
}
}

//JDBC工具类  包含数据库的连接,更新,关闭等功能
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
//JDBC的工具类,用于关闭数据库连接操作,更新操作和查询操作
public class JDBCTools {
public static Connection getConnection() throws Exception{//连接数据库
String driverClass = null;
String url = null;
String user = null;
String password = null;

Properties properties = new Properties();

InputStream in = Review.class.getClassLoader().getResourceAsStream("jdbc.properties");
properties.load(in);

driverClass = properties.getProperty("driver");
url = properties.getProperty("jdbcurl");
user = properties.getProperty("user");
password = properties.getProperty("password");
Class.forName(driverClass);
return DriverManager.getConnection(url, user, password);
}
public static void release(Connection con , Statement state){//关闭数据库连接
if(state != null){
try {
state.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(con != null){
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}

}
public static void release(ResultSet rs , Connection con , Statement state){//关闭数据库连接
if(rs != null)
{
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(state != null){
try {
state.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(con != null){
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}

//连接数据库的类,在JDBCTools.类中利用该类使用了反射,从而实现了数据库的连接
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;
public class Review {
public  Connection getConnection() throws Exception{//连接数据库
String driverClass = null;
String url = null;
String user = null;
String password = null;

Properties properties = new Properties();

InputStream in = Review.class.getClassLoader().getResourceAsStream("jdbc.properties");

properties.load(in);

driverClass = properties.getProperty("driver");
url = properties.getProperty("jdbcurl");
user = properties.getProperty("user");
password = properties.getProperty("password");
Class.forName(driverClass);
return DriverManager.getConnection(url, user, password);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: