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

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

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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);
        }
    }
}<br><br>然后下面就是批处理的相关代码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 @   聂泷  阅读(38)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示