123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- package qh.lqg.utils.db;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import java.util.List;
- import java.util.TimeZone;
- import com.mysql.jdbc.Connection;
- import com.mysql.jdbc.PreparedStatement;
- public class BatchInsert {
- public void oneJdbcBatch(List<String> domain) throws SQLException,
- ClassNotFoundException {
- // 数据库连接驱动
- Class.forName("com.mysql.jdbc.Driver");
- // 连接数据库字符串
- Connection con = (Connection) DriverManager.getConnection(
- "jdbc:mysql://" + "localhost:3306/db_test", "lyq",
- "123456");
- // 关闭事务自动提交
- con.setAutoCommit(false);
- // 设置时间格式
- SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss:SS");
- // 设置时区,中国上海
- TimeZone t = sdf.getTimeZone();
- t.setRawOffset(0);
- sdf.setTimeZone(t);
- // 获得当前时间,单位为毫秒。
- // 计算公式:1秒1000毫秒,1分60秒,1时60分,1天24时。
- Long startTime = System.currentTimeMillis();
- // com.mysql.jdbc.PreparedStatement 数据库字符串构建类
- PreparedStatement pst = (PreparedStatement) con
- .prepareStatement("UPDATE `tw_domain` SET( `domain_name`) VALUES (?);");
- for (int i = 0; i < domain.size(); i++) {
- pst.setString(1, domain.get(i));// 设置第一个问号用i代替
- // 把一个SQL命令加入命令列表
- pst.addBatch();// mysql批处理函数
- }
- // 这样一个批处理命令就拼接完成
- // 可以打印出来看看
- // System.out.println(pst.toString());
- // 执行批量更新
- pst.executeBatch();
- // 语句执行完毕,提交本事务
- con.commit();
- Long endTime = System.currentTimeMillis();
- System.out.println("------一次插入30000条数据,最后提交命令情况-----");
- System.out.println("用时:" + sdf.format(new Date(endTime - startTime)));
- pst.close();
- con.close();
- }
- public void muchJdbcBatch() throws ClassNotFoundException, SQLException {
- // 数据库连接驱动
- Class.forName("com.mysql.jdbc.Driver");
- // 连接数据库字符串
- Connection con = (Connection) DriverManager.getConnection(
- "jdbc:mysql://" + "localhost:3306/db_test", "root", "123456");
- // 关闭事务自动提交
- con.setAutoCommit(false);
- // 设置时间格式
- SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss:SS");
- // 设置时区,中国上海
- TimeZone t = sdf.getTimeZone();
- t.setRawOffset(0);
- sdf.setTimeZone(t);
- // 获得当前时间,单位为毫秒。
- // 计算公式:1秒1000毫秒,1分60秒,1时60分,1天24时。
- Long startTime = System.currentTimeMillis();
- // com.mysql.jdbc.PreparedStatement 数据库字符串构建类
- PreparedStatement pst = (PreparedStatement) con
- .prepareStatement("insert into jpa_person values (?,'中国')");
- for (int j = 0; j < 300; j++) {
- for (int i = 0; i < 100; i++) {
- pst.setInt(1, i + j);// 设置第一个问号用i代替
- // 把一个SQL命令加入命令列表
- pst.addBatch();// mysql批处理函数
- }
- pst.executeBatch();
- con.commit();
- }
- Long endTime = System.currentTimeMillis();
- System.out.println("------一次插入100条数据,然后提交,如此重复300次情况-----");
- System.out.println("用时:" + sdf.format(new Date(endTime - startTime)));
- pst.close();
- con.close();
- }
- public void oneJpaBatch() {
- }
- public void muchJpaBatch() {
- }
- }
|