注意:
  1. mysql服务器默认是关闭批处理的,我们需要通过一个参数,让mysql开启批处理的支持。将?rewriteBatchedStatements=true写在配置文件的url后面
  2. 使用更新的mysql 驱动:mysql-connector-java-5.1.37-bin.jar
1. 声明配置文件
jdbc.properties
user=root
password=00000000
url=jdbc:mysql://localhost:3306/test?rewriteBatchedStatements=true
driverClass=com.mysql.jdbc.Driver
2. 工具类
JDBCUtils.java
public class JDBCUtils {
    //获取数据库的连接
    public static Connection getConnection() throws Exception {
        //1.读取配置文件中的4个基本信息
        InputStream inputStream = ClassLoader.getSystemClassLoader().getResourceAsStream("jdbc.properties");
        Properties properties = new Properties();
        properties.load(inputStream);
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        String url = properties.getProperty("url");
        String driverClass = properties.getProperty("driverClass");
        //2.加载驱动
        Class.forName(driverClass);
        //3.获取连接
        return DriverManager.getConnection(url, user, password);
    }
    //关闭连接和Statement的操作
    public static void closeResource(Connection connection, Statement statement) {
        try {
            if(statement != null)
                statement.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            if(connection != null)
                connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    //关闭资源操作
    public static void closeResource(Connection connection, Statement statement, ResultSet resultSet) {
        try {
            if(statement != null)
                statement.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            if(connection != null)
                connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            if(resultSet != null)
                resultSet.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
3. goods表结构

批量插入

4. 测试代码
    @Test
public void testInsert() {
    Connection connection = null;
    PreparedStatement preparedStatement = null;
    try {
        long start = System.currentTimeMillis();
        connection = JDBCUtils.getConnection();
        //设置不允许自动提交数据
        connection.setAutoCommit(false);
        String sql = "insert into goods(name) values(?)";
        preparedStatement = connection.prepareStatement(sql);
        for (int i = 1; i <= 1000000; i++) {
            preparedStatement.setObject(1, "name_" + i);
            //1."攒"sql
            preparedStatement.addBatch();
            if (i % 500 == 0) {
                //2.执行batch
                preparedStatement.executeBatch();
                //3.清空batch
                preparedStatement.clearBatch();
            }
        }
        //提交数据
        connection.commit();
        long end = System.currentTimeMillis();
        System.out.println("花费的时间为:" + (end - start)); //7983
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        JDBCUtils.closeResource(connection, preparedStatement);
    }
}
goods表

在这里插入图片描述

Logo

更多推荐