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.youkuaiyun.com.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();
}
}
}