问题解决了,代码如下:求高性能的解决方案:
需求:
解决:
代码:
package com.records.info;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class DBconn {
ArrayList<String> column3string=new ArrayList<String>();
ArrayList<String> column13string=new ArrayList<String>();
ArrayList<String> test2col1=new ArrayList<String>();
ArrayList<Integer> test2col2=new ArrayList<Integer>();
ArrayList<String> test2col3=new ArrayList<String>();
String driver = "com.mysql.jdbc.Driver";
static String dbName = "dyform";
static String password = "root";
static String userName = "root";
static String url = "jdbc:mysql://localhost:3307/" + dbName;
static String sql = "select * from workinfo";
Connection conn=null;
public static Connection getConnection(){
String encoding="utf-8";
Connection conn=null;
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
conn = DriverManager.getConnection(url, userName, password);
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public void readFile(String filename) throws SQLException{
File file = new File(filename);
if (file.isFile() && file.exists()) {
InputStreamReader read = null;
try {
read = new InputStreamReader( new FileInputStream(file),"utf-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BufferedReader bufferedReader = new BufferedReader(read);
String lineTXT = null;
try {
while ((lineTXT = bufferedReader.readLine()) != null) {
String str="";
str+=" "+ lineTXT;
List<String> info=new ArrayList<String>();
String[] temp = info.toArray(new String[info.size()]);
if (temp!=null||temp.length!=0) {
temp = lineTXT.split(" ");
String sql = "insert into workinfo(column3,column13) values(?,?)";
int count = 0;//计数器
Connection conn =getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql);
conn.setAutoCommit(false);//设置数据手动提交,自己管理事务
pstmt.setString(1, temp[0]);
pstmt.setString(2, temp[1]);
pstmt.addBatch();//用PreparedStatement的批量处理
if(count%2000==0){//当增加了500个批处理的时候再提交
pstmt.executeBatch();//执行批处理
conn.commit();//提交
conn.close();//关闭数据库
conn =getConnection();//重新获取一次连接
conn.setAutoCommit(false);
pstmt = conn.prepareStatement(sql);
}
pstmt.executeBatch();
conn.commit();
pstmt.close();
conn.close();
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void show(){
System.out.println("This is string:");
for(int i=0;i<column3string.size();i++){
System.out.println(column3string.get(i));
}
System.out.println("This is integer:");
for(int i=0;i<column13string.size();i++){
System.out.println(column13string.get(i));
}
}
public static void main(String[] args) {
DBconn test=new DBconn ();
test.show();
long timeTestStart=System.currentTimeMillis();//记录开始时间
try {
test.readFile("D:\\ProgramFiles\\tomcat7\\webapps\\ExcelDemo1\\upload\\1_attlog.dat");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("succeed");
long timeTestEnd=System.currentTimeMillis();//记录结束时间
long time=timeTestEnd-timeTestStart;
long secondTime=time/1000;
System.out.println("Time:"+secondTime+" seconds");
}
}
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。