使用JDBC在MySQL数据库中快速批量插入数据
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了使用JDBC在MySQL数据库中快速批量插入数据,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3661字,纯文字阅读大概需要6分钟。
内容图文
print?- 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();
- }
- }
- All down : 103498
- Convert finished.
- All spend time/s : 47
- All down : 103498
- Convert finished.
- All spend time/s : 10
使用JDBC在MySQL数据库中快速批量插入数据
标签:final res ase about bar readline log number 编程
本文系统来源:http://www.cnblogs.com/adolfmc/p/7599835.html
内容总结
以上是互联网集市为您收集整理的使用JDBC在MySQL数据库中快速批量插入数据全部内容,希望文章能够帮你解决使用JDBC在MySQL数据库中快速批量插入数据所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。