Java JDBC批处理添加出现问题,求解决方案

晚辈使用JDBC批处理时出现一个问题,使用addBatch()方法将记录加入批处理中,我想让这五千条记录每达到一千条记录再执行,以此提高效率,可最后执行在数据库查看时仅五条记录,我尝试将 

preparedStatement.executeUpdate();
提出if语句,虽然是有五千条记录,但效率相当的慢
请求前辈们给出解决方案,谢谢

JDBC 的工具类,用于连接和关闭JDBC的相关资源

import java.io.FileReader;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;

/**/
public class JDBC {
    private static String user; //用户名
    private static String password; //密码
    private static String url;  //url
    private static String driver;   //驱动
    //使用静态代码块将信息全部获取并赋值给4个属性
    static {
        try {
            //创建Properties文件对象
            Properties properties = new Properties();
            //读取文件位置
            properties.load(new FileReader("src\\MySQLConnectorUserPassword.Properties"));
            //get相关值并赋值
            user = properties.getProperty("user");
            password = properties.getProperty("password");
            url = properties.getProperty("url");
            driver = properties.getProperty("driver");
        }catch (IOException e){
            throw new RuntimeException(e);
        }
    }
    //定义静态方法获取连接,并返回连接
    public static Connection getConnection(){
        try {
            return DriverManager.getConnection(url,user,password);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    //创建静态方法关闭连接等资源
    public static void close(ResultSet resultSet, PreparedStatement preparedStatement, Connection connection){
        //为了防止有null,需要使用if进行判断
        try {
            if(resultSet != null){
                resultSet.close();
            }
            if(preparedStatement != null){
                preparedStatement.close();
            }
            if(connection != null){
                connection.close();
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}

然后下面就是批处理的相关代码
import jdbcutils.JDBC;

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

public class A1Batch {
    //批处理   同时处理多条sql语句
    //使用批处理需要在url中加入rewriteBatchStatements=true
    public static void main(String[] args) throws SQLException {
        new A1Batch().batch();
    }
    public void batch() throws SQLException {
        //连接
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        //sql语句
        String sql = "insert into admin2 values (null,?);";
        //资源赋值
        connection = JDBC.getConnection();
        preparedStatement = connection.prepareStatement(sql);
        long start = System.currentTimeMillis();
        //处理多条语句
        for (int i = 1; i <= 5000; i++) {
            //赋值
            preparedStatement.setString(1,"jack"+i);
            //将sql语句加入到批处理中     addBatch()方法
            preparedStatement.addBatch();
            //当有1k条记录,再批量执行
            if (i%1000==0){
                //执行
                preparedStatement.executeUpdate();
                //清空
                preparedStatement.clearBatch();
            }
        }
        long stop = System.currentTimeMillis();
        System.out.println("使用时长:" + (stop-start));
        //关闭资源
        JDBC.close(resultSet,preparedStatement,connection);
    }
}

  

posted @ 2023-04-16 15:17  聂泷  阅读(52)  评论(0)    收藏  举报