Jdbc批量数据操作

Jdbc批量数据操作

package jdbc;

import org.junit.Test;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * @author 小邱
 * @version 0.0.1
 * @description 批量数据操作
 * <p>
 * 当需要成批插入或者更新记录时,可以采用Java的批量更新机制,这一机制允许多条
 * 语句一次性提交给数据库批量处理。通常情况下比单独提交处理更有效率
 * <p>
 * JDBC的批量处理语句包括下面三个方法:
 * addBatch(String):添加需要批量处理的SQL语句或是参数;
 * executeBatch():执行批量处理语句;
 * clearBatch():清空缓存的数据
 * 通常我们会遇到两种批量执行SQL语句的情况:
 * 多条SQL语句的批量处理;
 * 一个SQL语句的批量传参;
 * @since 2021/9/28 18:21
 */
public class PreparedStatementBatch {
    //实现层次一:使用Statement
    @Test
    public void test1() throws SQLException {
        Connection connection = JdbcUtils.getConnection();
        Statement statement = connection.createStatement();
        for (int i = 1; i <= 20000; i++) {
            String sql = "insert into goods(name) values('name_' + " + i + ")";
            statement.executeUpdate(sql);
        }
        JdbcUtils.close(connection, statement);
    }

    //实现层次二:使用PreparedStatement
    @Test
    public void test2() throws SQLException {
        Connection connection = JdbcUtils.getConnection();
        String sql = "insert into goods(name) values(?)";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        //本质上还是一条一条执行(不算真正的批处理)
        for (int i = 0; i < 20000; i++) {
            preparedStatement.setObject(1, "name_" + i);
            preparedStatement.execute();
        }
        JdbcUtils.close(connection, preparedStatement);

    }

    // 实现层次三:使用 addBatch() / executeBatch() / clearBatch()
    //mysql服务器默认是关闭批处理的,我们需要通过一个参数,让mysql开启批处理的支持。
    //?rewriteBatchedStatements=true 写在配置文件的url后面
    //mysql驱动5.1.37以上的新版本才支持
    @Test
    public void test3() throws SQLException {
        Connection connection = JdbcUtils.getConnection();
        String sql = "insert into goods(name) values(?)";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        for (int i = 0; i < 20000; i++) {
            preparedStatement.setObject(1, "name_" + i);
            //1.积攒sql
            preparedStatement.addBatch();
            if (i % 500 == 0) {//每500条执行一次
                //2.执行
                preparedStatement.executeBatch();
                //3.清空
                preparedStatement.clearBatch();
            }
        }
        JdbcUtils.close(connection, preparedStatement);
    }
    //实现层次四::在层次三的基础上操作 使用Connection 的 setAutoCommit(false) / commit()
    @Test
    public void test4() throws SQLException {
        Connection connection = JdbcUtils.getConnection();
        //设置为不自动提交数据
        connection.setAutoCommit(false);

        String sql = "insert into goods(name) values(?)";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        for (int i = 0; i < 20000; i++) {
            preparedStatement.setObject(1, "name_" + i);
            //1.积攒sql
            preparedStatement.addBatch();
            if (i % 1000 == 0) {//每500条执行一次
                //2.执行
                preparedStatement.executeBatch();
                //3.清空
                preparedStatement.clearBatch();
            }
        }
        //提交数据
        connection.commit();
        JdbcUtils.close(connection, preparedStatement);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用JDBC进行批量插入数据,你可以按照以下步骤操作: 1. 获取数据库连接:使用JDBC驱动程序获取与数据库的连接。例如,使用`DriverManager.getConnection()`方法。 2. 创建PreparedStatement对象:使用连接对象创建`PreparedStatement`对象,并指定插入语句,但是不指定参数值。例如,`connection.prepareStatement("INSERT INTO table_name (column1, column2) VALUES (?, ?)")`。 3. 设置参数值:使用`PreparedStatement`对象的`setXxx()`方法为每个参数设置值。例如,`preparedStatement.setInt(1, value1)`和`preparedStatement.setString(2, value2)`。 4. 添加批量操作:使用`addBatch()`方法将每个`PreparedStatement`对象添加到批处理中。例如,`preparedStatement.addBatch()`。 5. 执行批处理:使用`executeBatch()`方法执行批处理操作。例如,`preparedStatement.executeBatch()`。 6. 关闭连接和声明对象:在操作完成后,关闭连接和所有相关的声明对象。例如,`preparedStatement.close()`和`connection.close()`。 以下是一个示例代码片段,演示如何使用JDBC进行批量插入数据: ```java Connection connection = null; PreparedStatement preparedStatement = null; try { connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/database_name", "username", "password"); String insertQuery = "INSERT INTO table_name (column1, column2) VALUES (?, ?)"; preparedStatement = connection.prepareStatement(insertQuery); // 设置参数值并添加到批处理 preparedStatement.setInt(1, value1); preparedStatement.setString(2, value2); preparedStatement.addBatch(); // 可以继续添加更多批处理操作 // 执行批处理 int[] batchResult = preparedStatement.executeBatch(); // 处理批处理结果 } catch (SQLException e) { e.printStackTrace(); } finally { // 关闭连接和声明对象 if (preparedStatement != null) { try { preparedStatement.close(); } catch (SQLException e) { e.printStackTrace(); } } if (connection != null) { try { connection.close(); } catch (SQLException e) { e.printStackTrace(); } } } ``` 请注意,以上代码只是一个示例,你需要根据实际的表结构和数据来修改插入语句和参数设置。确保在使用JDBC进行数据库操作时,遵循相关的安全和最佳实践。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值