利用JDBC批量处理语句

      有时候我们想要在本地数据库的一个表中插入大量的数据,这时候如果用普通的方法,插入的速度就会很慢,所以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
    public 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);
       }
       
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值