不多说废话,数据库采用SQLServer 2008
数据库名TestDB 字段
P17173_Name nvarchar(50)
P17173_MD5Key nvarchar(50)
P17173_SecurityEmail nvarchar(100)
P17173_ICPassCard nvarchar(50)
从一个超级大的txt中读取数据写入到DB中。秒懂这个txt的同学不要说话。纯个人研究。涉及到用java逐行度数据写入数据了知识;运行了一晚上写入了500万条。还没写完。要上班了只得停掉.....太慢了。
上代码
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package tecentdata;
import com.sun.org.apache.xml.internal.security.utils.IgnoreAllErrorHandler;
import java.io.File;
import java.io.FileInputStream;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io;
import java.io.FileReader;
import java.io.FileWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
/**
*
* @author Administrator
*/
public class TecentData {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
File oriFile = new File("d:\\17173.txt");
String tempString = null;
byte[] tempByte = new byte[1024];
String[] arrString = null;
//DB连接
String connectionUrl = "jdbc:sqlserver://localhost:1433;databaseName=TestDB ;user=chenqiang;password=abc123456;";
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = DriverManager.getConnection(connectionUrl);
}catch(Exception ex){
ex.printStackTrace();
}
while(true){
try{
FileInputStream inputStream = new FileInputStream(oriFile);
if(oriFile.isFile())
{
InputStreamReader inputstreamreader = new InputStreamReader(inputStream);
BufferedReader buffer = new BufferedReader(inputstreamreader);
int num = 1000;
while((tempString=buffer.readLine())!=null){
// tempString = buffer.readLine();
// "yu12331929 | c99861c9d695bd16095067c5f58aebdd | 502489299@qq.com | a7733330"
arrString = tempString.split(" | ");
String strSQL = String.format("insert into Game17173_Site_Data " +
" ( " +
"P17173_Name, " +
"P17173_MD5Key, " +
"P17173_SecurityEmail, " +
"P17173_ICPassCard " +
")values( '%s','%s','%s','%s' ) ",arrString[0],arrString[2],arrString[4],arrString[6] );
stmt = con.createStatement();
// stmt.executeQuery(strSQL);
int a = stmt.executeUpdate(strSQL);
}
}
}catch(Exception ex)
{
// goto :Ignore;
//tempString=buffer.readLine();
//ex.printStackTrace();
}
try{
Thread.sleep(10);
}catch(Exception e)
{}
}
}
}
本文介绍了使用Java从一个大型文本文档中逐行读取数据,并将这些数据高效地写入到SQLServer 2008数据库的过程。内容涉及数据库表结构及字段描述,以及Java实现数据批量写入的挑战,作者提到运行一晚仅完成500万条记录的写入,但速度较慢。
1万+

被折叠的 条评论
为什么被折叠?



