微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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);
    }
}

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐