JavaWeb ----使用JDBC实现批处理

JDBC实现批处理方式有两种:statement() or preparedstatement()

一.使用preparedstatement()方法实现

package test;

import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
import org.junit.Test;

/**
 * JDBC操作
 * */
public class JdbcTest {

    private static String url;
    private static String username;
    private static String password;
    
    static {
        try {
            InputStream is = PropTest.class.getResourceAsStream("/jdbc.properties");
            Properties prop = new Properties();
            prop.load(is);
            
            // 将驱动类加载到JVM中
            String driver = prop.getProperty("jdbc.driver");
            Class.forName(driver);
            
            url = prop.getProperty("jdbc.url");
            username = prop.getProperty("jdbc.username");
            password = prop.getProperty("jdbc.password");
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
    
    public static void main(String[] args) throws Exception {
        Connection conn = DriverManager.getConnection(url, username, password);
        System.out.println(conn);
    }
    
    // ============= 单元测试 ==============
    @Test
    public void create() throws Exception {
        String sql = "create table tb_user (id int primary key auto_increment, name varchar(30), age int)";
        Connection conn = DriverManager.getConnection(url, username, password);
        PreparedStatement pst = conn.prepareStatement(sql);
        pst.executeUpdate();
        pst.close();
        
        conn.close();
    }
    
    @Test
    public void insert() throws Exception {
        String sql = "insert into tb_user values(null,?,?)";
        Connection conn = DriverManager.getConnection(url, username, password);
        
        for(int i=0; i<10; i++) {
            PreparedStatement pst = conn.prepareStatement(sql);
            pst.setString(1, "test_" + i);
            pst.setInt(2, 20 + i);
            pst.executeUpdate();
            pst.close();
        }
        conn.close();
    }
    
    @Test
    public void update() throws Exception {
        String sql = "update tb_user set name=? where id=?";
        Connection conn = DriverManager.getConnection(url, username, password);
        
        PreparedStatement pst = conn.prepareStatement(sql);
        pst.setObject(1, "张三");
        pst.setObject(2, 1);
        pst.executeUpdate();
        pst.close();
        
        conn.close();
    }
    
    @Test
    public void delete() throws Exception {
        String sql = "delete from tb_user where age>=?";
        Connection conn = DriverManager.getConnection(url, username, password);
        
        PreparedStatement pst = conn.prepareStatement(sql);
        pst.setObject(1, 28);
        pst.executeUpdate();
        pst.close();
        
        conn.close();
    }
    
    @Test
    public void query1() throws Exception {
        String sql = "select * from tb_user where id=?";
        Connection conn = DriverManager.getConnection(url, username, password);
        
        PreparedStatement pst = conn.prepareStatement(sql);
        pst.setObject(1, 1);
        
        ResultSet rs = pst.executeQuery();
        if(rs.next()) {
            int id = rs.getInt("id");
            String name = rs.getString("name");
            int age = rs.getInt("age");
            
            String msg = String.format("id=%d, name=%s, age=%d", id, name, age);
            System.out.println(msg);
        }
        
        pst.close();
        conn.close();
    }
    
    @Test
    public void query2() throws Exception {
        String sql = "select * from tb_user";
        Connection conn = DriverManager.getConnection(url, username, password);
        
        PreparedStatement pst = conn.prepareStatement(sql);
        ResultSet rs = pst.executeQuery();
        while(rs.next()) {
            int id = rs.getInt("id");
            String name = rs.getString("name");
            int age = rs.getInt("age");
            
            String msg = String.format("id=%d, name=%s, age=%d", id, name, age);
            System.out.println(msg);
        }
        
        pst.close();
        conn.close();
    }
    
    /// ========== 批量操作 =============
    @Test
    public void insert2() throws Exception {
        String sql = "insert into tb_user values(null,?,?)";
        Connection conn = DriverManager.getConnection(url, username, password);
        long begin = System.currentTimeMillis();
        for(int i=0; i<100000; i++) {
            PreparedStatement pst = conn.prepareStatement(sql);
            pst.setString(1, "test_" + i);
            pst.setInt(2, 20 + i);
            pst.executeUpdate();
            pst.close();
        }        
        conn.close();
        System.out.println(System.currentTimeMillis() - begin);
    }
    
    // executeBatch()没有效果
    @Test
    public void insert3() throws Exception {
        String sql = "insert into tb_user values(null,?,?)";
        Connection conn = DriverManager.getConnection(url, username, password);
        long begin = System.currentTimeMillis();
        PreparedStatement pst = conn.prepareStatement(sql);
        for(int i=0; i<100000; i++) {
            pst.setString(1, "test_" + i);
            pst.setInt(2, 20 + i);
            pst.addBatch();
        }
        pst.executeBatch();
        pst.close();
        conn.close();
        System.out.println(System.currentTimeMillis() - begin);
    }
    
    // executeBatch():必须保证事务不自动提交
    @Test
    public void insert4() throws Exception {
        String sql = "insert into tb_user values(null,?,?)";
        Connection conn = DriverManager.getConnection(url, username, password);
        conn.setAutoCommit(false);
        
        long begin = System.currentTimeMillis();
        PreparedStatement pst = conn.prepareStatement(sql);
        for(int i=0; i<100000; i++) {
            pst.setString(1, "test_" + i);
            pst.setInt(2, 20 + i);
            pst.addBatch();
            
            // 如果数据量比较大,需要分批次提交
            if((i+1)%10000 == 0) {
                pst.executeBatch();
                pst.clearBatch();
            }
        }
        pst.executeBatch();
        conn.setAutoCommit(true);
        
        pst.close();
        conn.close();
        
        System.out.println(System.currentTimeMillis() - begin);
    }
}

 

转载于:https://www.cnblogs.com/jjjjjjy/p/11516283.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值