java mysql插入string_将String类型时间插入数据库问题的解决方法

下面程序适用于将大量数据插入数据库:

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.................................................................................................................");

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值