JDBC mysql批量插入

使用JDBC连接MySQL数据库进行数据插入的时候,特别是大批量数据连续插入(10W+),如何提高效率呢?

在JDBC编程接口中Statement 有两个方法特别值得注意:

?
1
void addBatch() throws SQLException

Adds a set of parameters to this PreparedStatement object's batch of commands.

?
1
int [] executeBatch() throws SQLException

Submits a batch of commands to the database for execution and if all commands execute successfully, returns an array of update counts. The int elements of the array that is returned are ordered to correspond to the commands in the batch, which are ordered according to the order in which they were added to the batch.
通过使用addBatch()和executeBatch()这一对方法可以实现批量处理数据。
不过值得注意的是,首先需要在数据库链接中设置手动提交,connection.setAutoCommit(false),然后在执行Statement之后执行connection.commit()。

?
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
package cyl.demo.ipsearcher;
 
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql. Connection ;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
 
public class DbStoreHelper {
 
     private String insert_sql;
     private String charset;
     private boolean debug;
 
     private String connectStr;
     private String username;
     private String password ;
 
     public DbStoreHelper() {
         connectStr = "jdbc:mysql://localhost:3306/db_ip" ;
         // connectStr += "?useServerPrepStmts=false&rewriteBatchedStatements=true" ;
         insert_sql = "INSERT INTO tb_ipinfos (iplong1,iplong2,ipstr1,ipstr2,ipdesc) VALUES (?,?,?,?,?)" ;
         charset = "gbk" ;
         debug = true ;
         username = "root" ;
         password = "***" ;
     }
 
     public void storeToDb(String srcFile) throws IOException {
         BufferedReader bfr = new BufferedReader(new InputStreamReader(new FileInputStream(srcFile), charset));
         try {
             doStore(bfr);
         } catch (Exception e) {
             e.printStackTrace();
         } finally {
             bfr. close ();
         }
     }
 
     private void doStore(BufferedReader bfr) throws ClassNotFoundException, SQLException, IOException {
         Class.forName( "com.mysql.jdbc.Driver" );
         Connection conn = DriverManager.getConnection(connectStr, username, password );
         conn.setAutoCommit( false ); // 设置手动提交
         int count = 0;
         PreparedStatement psts = conn.prepareStatement(insert_sql);
         String line = null ;
         while ( null != (line = bfr.readLine())) {
             String[] infos = line.split( ";" );
             if (infos.length < 5)   continue ;
             if (debug) {
                 System. out .println(line);
             }
             psts.setLong(1, Long.valueOf(infos[0]));
             psts.setLong(2, Long.valueOf(infos[1]));
             psts.setString(3, infos[2]);
             psts.setString(4, infos[3]);
             psts.setString(5, infos[4]);
             psts.addBatch();          // 加入批量处理
             count ++;           
         }
         psts.executeBatch(); // 执行批量处理
         conn. commit ();  // 提交
         System. out .println( "All down : " + count );
         conn. close ();
     }
 
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值