将csdn600w用户及密码帐号存入本地mysql数据库 -ag凯发k8国际
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年技术见证,附赠技术全景图总结
以上是ag凯发k8国际为你收集整理的将csdn600w用户及密码帐号存入本地mysql数据库的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: redo/undo
- 下一篇: 分享html 5的参考手册,演讲稿,电子