将CSDN600W用户及密码帐号存入本地MySql数据库
1 import java.io.BufferedReader;
2 import java.io.File;
3 import java.io.FileNotFoundException;
4 import java.io.FileReader;
5 import java.io.IOException;
6 import java.sql.Connection;
7 import java.sql.DriverManager;
8 import java.sql.PreparedStatement;
9 import java.sql.SQLException;
10
11 public class InsertCsdnPsw {
12
13 private static String driver = "com.mysql.jdbc.Driver";
14 private static String user = "root";
15 private static String pwd = "root";
16 private static String url = "jdbc:mysql://localhost:3306/csdnpsw?useUnicode=true&characterEncoding=UTF-8";
17 private static int step = 300000;//步进,每次批量插入数量,这个200000以上需调整JVM内存占用
18
19 /**
20 * @param args
21 */
22 public static void main(String[] args) {
23 File file = new File("E:\\ubuntushare\\www.csdn.net.sql");
24 BufferedReader reader = null;
25 try {
26 reader = new BufferedReader(new FileReader(file));
27 } catch (FileNotFoundException e2) {
28 e2.printStackTrace();
29 }
30
31 Connection con = null;
32 PreparedStatement pstmt = null;
33 String sql = "insert into userinfo_copy values(?,?,?)";
34 try {
35 Class.forName(driver);
36 con = DriverManager.getConnection(url, user, pwd);
37 pstmt = con.prepareStatement(sql);
38 } catch (ClassNotFoundException ex) {
39 ex.printStackTrace();
40 } catch (SQLException e) {
41 e.printStackTrace();
42 }
43 long line = 0;
44 long start = System.currentTimeMillis();
45 insert(con, line, reader, pstmt, start);
46
47 if (reader != null) {
48 try {
49 reader.close();
50 } catch (IOException e1) {
51 e1.printStackTrace();
52 }
53 }
54 }
55
56 public static void insert(Connection con, long line, BufferedReader reader, PreparedStatement pstmt, long start) {
57 try {
58 con.setAutoCommit(false);
59 con.commit();
60 try {
61 String tempString = null;
62 // 一次读入一行,直到读入null为文件结束
63 while ((tempString = reader.readLine()) != null) {
64 line++;
65 pstmt.setString(1, tempString.split(" # ")[0]);
66 pstmt.setString(2, tempString.split(" # ")[1]);
67 pstmt.setString(3, tempString.split(" # ")[2]);
68 pstmt.addBatch();
69 if (line % step == 0) {
70 pstmt.executeBatch();
71 con.commit();
72 pstmt.clearParameters();
73 pstmt.clearBatch();
74 System.out.println("添加 " + line + "\t条记录耗时 "+(System.currentTimeMillis() - start)+"\t"/*+";耗时比 : "
75 + ((line+0.0)/(System.currentTimeMillis() - start))*/);
76 }
77 }
78 reader.close();
79 } catch (IOException e) {
80 e.printStackTrace();
81 } finally {
82
83 }
84 pstmt.executeBatch();
85 con.commit();
86 System.out.println("添加 \t" + line + "\t条记录耗时 "+(System.currentTimeMillis() - start)+"\t"/*+";耗时比 : "
87 + ((line+0.0)/(System.currentTimeMillis() - start))*/);
88 } catch (SQLException e2) {
89 System.out.println((line-step) + "到" + line + "有问题");
90 insert(con, line, reader, pstmt, start);
91 //e2.printStackTrace();
92 }
93 }
94 }