我需要将几百万条记录插入到mysql数据库中。我一次批量插入100万。请参阅下面的代码。它似乎很慢。有什么办法可以优化它吗?JDBC批量插入性能
try {
// Disable auto-commit
connection.setAutoCommit(false);
// Create a prepared statement
String sql = "INSERT INTO mytable (xxx), VALUES(?)";
PreparedStatement pstmt = connection.prepareStatement(sql);
Object[] vals=set.toArray();
for (int i=0; i<vals.length; i++) {
pstmt.setString(1, vals[i].toString());
pstmt.addBatch();
}
// Execute the batch
int [] updateCounts = pstmt.executeBatch();
System.out.append("inserted "+updateCounts.length);
你的代码有点损坏(并且过早地被截断) – Uri 2010-06-07 21:19:49
顺便说一句,你正在使用哪个驱动程序?一般的JDBC或JDBC-Mysql连接器? – Uri 2010-06-07 21:20:15
我正在使用com.mysql.jdbc.Driver – user157195 2010-06-07 21:26:25