MySQL的批处理

批处理,降低与数据库的连接次数,提高执行效率。
BatchDemo.java文件:

package com.itheima.jdbc;

import java.sql.Connection;
import java.sql.Statement;

import org.junit.Test;

import com.itheima.util.JdbcUtil;

/**
 * 批处理,降低与数据库的连接次数,提高执行效率
 * @author wym
 *
 */
public class BatchDemo {
    /*
     create database day16;
     use day16;
     create table t1(
        id int primary key,
        name varchar(100)
     );
     */

    //向数据库中插入2条记录,再把第1条删掉
    @Test
    public void test1() throws Exception{
        Connection conn = JdbcUtil.getConnection();
        Statement stmt = conn.createStatement();
        String sql1 = "insert into t1 values(1,'aaa')";
        String sql2 = "insert into t1 values(2,'bbb')";
        String sql3 = "delete from t1 where id=1";
        stmt.addBatch(sql1);//Statement实例内部有一个List,sql语句加到List中了
        stmt.addBatch(sql2);
        stmt.addBatch(sql3);

        int[] ii = stmt.executeBatch();//返回的是一个数组,数组的元素为每条语句影响到的行数
        for(int i : ii){
            System.out.println(i);
        }
        JdbcUtil.release(null, stmt, conn);
    }

    //插入10条记录
    @Test
    public void test2() throws Exception{
        Connection conn = JdbcUtil.getConnection();
        java.sql.PreparedStatement stmt = conn.prepareStatement("insert into t1 values(?,?)");
        for(int i=0; i<10; i++){
            stmt.setInt(1, i+1);
            stmt.setString(2,"aaaa"+(i+1));
            stmt.addBatch();
        }

        stmt.executeBatch();//返回的是一个数组,数组的元素为每条语句影响到的行数
        JdbcUtil.release(null, stmt, conn);
    }

    //插入10000条数据
    @Test
    public void test3() throws Exception{
        Connection conn = JdbcUtil.getConnection();
        java.sql.PreparedStatement stmt = conn.prepareStatement("insert into t1 values(?,?)");
        for(int i=0; i<10000; i++){
            stmt.setInt(1, i+1);
            stmt.setString(2,"aaaa"+(i+1));
            stmt.addBatch();
            if(i%1000 ==0){
                stmt.executeBatch();
                stmt.clearBatch();
            }
        }

        stmt.executeBatch();//返回的是一个数组,数组的元素为每条语句影响到的行数
        JdbcUtil.release(null, stmt, conn);
    }
}

JdbcUtil.java文件:

package com.itheima.util;

import java.io.IOException;
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;

//工具类
public class JdbcUtil {

    private static String driverClass;
    private static String url;
    private static String user;
    private static String password;

    static{
        try {
            ClassLoader cl = JdbcUtil.class.getClassLoader();
            InputStream in = cl.getResourceAsStream("dbcfg.properties");
            Properties props = new Properties();
            props.load(in);
            driverClass = props.getProperty("driverClass");
            url = props.getProperty("url");
            user = props.getProperty("user");
            password = props.getProperty("password");

            Class.forName(driverClass);
        } catch (Exception e) {
            throw new ExceptionInInitializerError(e);
        }
    }


    public static Connection getConnection() throws Exception{
        Connection conn = DriverManager.getConnection(url,user, password);
        return conn;
    }
    public static void release(ResultSet rs,Statement stmt,Connection conn){
        if(rs!=null){
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            rs = null;
        }
        if(stmt!=null){
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            stmt = null;
        }
        if(conn!=null){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            conn = null;
        }
    }
}

dbcnfg.properties文件:

driverClass=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/day16
user=root
password=admin

运行结果:
1 aaaa1
2 aaaa2
3 aaaa3
4 aaaa4
5 aaaa5
。。。共10000条

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值