下面程序适用于将大量数据插入数据库:
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.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ReadWriteFile {
// 读写文本文件
public static void readWriteFile(String fromDir)
{
ZhxxbDao zd = new
ZhxxbDao();
BufferedReader br = null;
Connection connection =
JdbcConnection.getConnetion();
try {
connection.setAutoCommit(false);
} catch (SQLException e2)
{
e2.printStackTrace();
}
PreparedStatement statement =
null;
String patternStr = null;
Pattern pattern = null;
CharSequence cs = null;
Matcher m = null;
String[] strs = new
String[6];
DateFormat format = new
SimpleDateFormat("yyyyMMddHH:mm:ss");
String oldDate1=null;
Date oldDate2=null;
int value = 0;
try {
File read =
new File(fromDir);
br = new
BufferedReader(new FileReader(read));
// 读文件
String
str;
String sql =
"insert into
zhxxb_010(YH_ZH,YH_BASIP,YH_SBVLAN,YH_DKVLAN,YH_BASCW,YH_BASDK,YH_RQ)
values(?,?,?,?,?,?,?)";
statement =
connection.prepareStatement(sql);
int i =
0;
while (null
!= (str = br.readLine())) {
i++;
patternStr
= "(\\d+),(\\d+.\\d+.\\d+.\\d+),(\\d+),(\\d+),(\\d+),(\\d+),(.*)";
pattern
= Pattern.compile(patternStr);
cs
= str.subSequence(0, str.length());
m
= pattern.matcher(cs);
while
(m.find()) {
//格式化日期
oldDate1=m.group(7);
try
{
oldDate2
= format.parse(oldDate1);
}
catch (ParseException e) {
System.out.println(e.getMessage());
}
statement.setString(1,
m.group(1));
statement.setString(2,
m.group(2));
statement.setInt(3,
Integer.valueOf(m.group(6)));
statement.setInt(4,
Integer.valueOf(m.group(5)));
statement.setInt(5,
Integer.valueOf(m.group(3)));
statement.setInt(6,
Integer.valueOf(m.group(4)));
//java.util.Date类型转换为java.sql.Date可识别的类型(即java.sql.Timestamp)方法
statement.setTimestamp(7,new
Timestamp(oldDate2.getTime())); statement.addBatch();
if
(i % 1000 == 1) {
System.out.println("==="+i+"=="+oldDate2);
statement.executeBatch();
connection.commit();
}
//
strs[0]=m.group(1);
//
strs[1]=m.group(2);
//
strs[2]=m.group(3);
//
strs[3]=m.group(4);
//
strs[4]=m.group(5);
//
strs[5]=m.group(6);
//
value=zd.insert(JdbcConnection.getConnetion(), strs);
}
}
statement.executeBatch();
} catch (FileNotFoundException
e) { // 文件未找到
System.out.println(e);
} catch (IOException e) {
System.out.println(e);
} catch (SQLException e)
{
e.printStackTrace();
} finally {
try {
if
(null != statement)
statement.executeBatch();
} catch
(SQLException e1) {
e1.printStackTrace();
}
if(null!=connection){
try
{
connection.commit();
}
catch (SQLException e) {
e.printStackTrace();
}
try
{
connection.close();
}
catch (SQLException e) {
e.printStackTrace();
}
}
try {
if
(br != null)
br.close();
} catch
(IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
// 测试
System.out.println("begin");
String fromDir =
"f:\\10.txt";
readWriteFile(fromDir);
System.out.println("success.................................................................................................................");
}
}