代码实现部分:
//批量插入数据
String sql = "insert into person values(?,?,?)";
// String sql = "delete from person";
//?rewriteBatchedStatements=ture;
String url = "jdbc:mysql://localhost:3306/huanlesong?rewriteBatchedStatements=true";
String classname = "com.mysql.jdbc.Driver";
String user = "root";
String password = "root";
try {
Class.forName(classname);
//PreparedStatement ps = DBHelper.getConnection().prepareStatement(sql);
PreparedStatement ps = DriverManager.getConnection(url, user, password).prepareStatement(sql);
for(int i=0;i<20000;i++){
ps.setString(1, UUID.randomUUID().toString());
ps.setString(2, UUID.randomUUID().toString());
ps.setString(3, UUID.randomUUID().toString());
ps.addBatch();
}
ps.executeBatch();
// ps.executeUpdate();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}mysql快速批量插入url
String url = "jdbc:mysql://localhost:3306/huanlesong?rewriteBatchedStatements=true";
本文介绍了一种使用Java和MySQL进行批量数据插入的方法。通过设置URL参数rewriteBatchedStatements为true,并利用PreparedStatement对象的addBatch方法,实现了高效地将大量记录一次性写入数据库的功能。
2万+

被折叠的 条评论
为什么被折叠?



