利用JDBC从文件读入数据到数据库

 import java.io.BufferedReader;
 import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.FileReader;
 import java.io.IOException;
 import java.sql.Connection;
 import java.sql.DriverManager;
 import java.sql.PreparedStatement;
 import java.sql.SQLException;
 
 public class InsertCsdnPsw {
     
     private static String driver = "com.mysql.jdbc.Driver";
     private static String user = "root";
     private static String pwd = "root";
     private static String url = "jdbc:mysql://localhost:3306/csdnpsw?useUnicode=true&characterEncoding=UTF-8";
     private static int step = 300000;//步进,每次批量插入数量,这个200000以上需调整JVM内存占用
 
     /**
      * @param args
      */
     public static void main(String[] args) {
         File file = new File("E:\\ubuntushare\\www.csdn.net.sql");
         BufferedReader reader = null;
         try {
             reader = new BufferedReader(new FileReader(file));
         } catch (FileNotFoundException e2) {
             e2.printStackTrace();
         }
         
         Connection con = null;
         PreparedStatement pstmt = null;
         String sql = "insert into userinfo_copy values(?,?,?)";
         try {
             Class.forName(driver);
             con = DriverManager.getConnection(url, user, pwd);
             pstmt = con.prepareStatement(sql);
         } catch (ClassNotFoundException ex) {
             ex.printStackTrace();
         } catch (SQLException e) {
             e.printStackTrace();
         }
         long line = 0;
         long start = System.currentTimeMillis();
         insert(con, line, reader, pstmt, start);
 
         if (reader != null) {
             try {
                 reader.close();
             } catch (IOException e1) {
                 e1.printStackTrace();
             }
         }
     }
     
     public static void insert(Connection con, long line, BufferedReader reader, PreparedStatement pstmt, long start) {
         try {
             con.setAutoCommit(false);
             con.commit();
             try {
                 String tempString = null;
                 // 一次读入一行,直到读入null为文件结束
                 while ((tempString = reader.readLine()) != null) {
                     line++;
                     pstmt.setString(1, tempString.split(" # ")[0]);
                     pstmt.setString(2, tempString.split(" # ")[1]);
                     pstmt.setString(3, tempString.split(" # ")[2]);
                     pstmt.addBatch();
                     if (line % step == 0) {
                         pstmt.executeBatch();
                         con.commit();
                         pstmt.clearParameters();
                         pstmt.clearBatch();
                         System.out.println("添加 " + line + "\t条记录耗时 "+(System.currentTimeMillis() - start)+"\t"/*+";耗时比 : "
                                 + ((line+0.0)/(System.currentTimeMillis() - start))*/);
                     }
                 }
                 reader.close();
             } catch (IOException e) {
                 e.printStackTrace();
             } finally {
                 
             }
             pstmt.executeBatch();
             con.commit();
             System.out.println("添加 \t" + line + "\t条记录耗时 "+(System.currentTimeMillis() - start)+"\t"/*+";耗时比 : "
             + ((line+0.0)/(System.currentTimeMillis() - start))*/);
         } catch (SQLException e2) {
             System.out.println((line-step) + "到" + line + "有问题");
             insert(con, line, reader, pstmt, start);
             //e2.printStackTrace();
         }
     }
 }

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值