将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?}
轉載于:https://www.cnblogs.com/cokepanm/archive/2011/12/26/2301631.html
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的将CSDN600W用户及密码帐号存入本地MySql数据库的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: redo/undo
- 下一篇: 分享HTML 5的参考手册,演讲稿,电子