4
回答
java读取文本,插到mysql表中,出现问题。
华为云实践训练营,热门技术免费实践!>>>   
package read;
import java.io.BufferedInputStream;
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.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;


public class DBconn {
int bufferSize=20*1024*1024;
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="test";
     static String password="6983124ACD";
     static String userName="root";
     static String url="jdbc:mysql://localhost:3306/" + 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");
conn=DriverManager.getConnection(url,userName,password);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();

          catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
     }

     public void readFile(String filename) throws FileNotFoundException{
    File file = new File(filename);
           if (file.isFile() && file.exists()) {
BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file));
InputStreamReader inputStreamReader = new InputStreamReader(bufferedInputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader, bufferSize);
String lineTXT = null;
PreparedStatement pstmt = null;
Connection conn = getConnection();
//String sql = "insert into workinfo(column3,column13) values(1,2)(2,3)(4,5)";
String sql = "insert into workinfo(column3,column13) values(?,?)";

try {
while((lineTXT=bufferedReader.readLine())!=null){
String[] temp = null;
temp = lineTXT.split("  ");
int count = 0;// 计数器
pstmt = conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
conn.setAutoCommit(false);// 设置数据手动提交,自己管理事务
pstmt.setString(1, temp[0]);
pstmt.setString(2, temp[0]);
pstmt.addBatch();// 用PreparedStatement的批量处理

if (count % 2000 == 0) {// 当增加了500个批处理的时候再提交
         pstmt.executeBatch();// 执行批处理

}
}
conn.commit();
pstmt.close();
conn.close();

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException 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) throws FileNotFoundException{
    DBconn test=new DBconn();
    long timeTsetStart =System.currentTimeMillis();//记录时间
     
     test.readFile("D:\\java\\Work_Test\\ddd.dat");
     
     System.out.println("数据插入成功!");
     long timeTestEnd = System.currentTimeMillis();// 记录结束时间
     long time = timeTestEnd - timeTsetStart;
     long secondTime = time / 1000;
     System.out.println("解析时间::" + secondTime + " seconds");
     
     }
}

结果为Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1

主要是while((lineTXT=bufferedReader.readLine())!=null){
String[] temp = null;
temp = lineTXT.split("  ");
int count = 0;// 计数器
pstmt = conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
conn.setAutoCommit(false);
pstmt.setString(1, temp[0]);
pstmt.setString(2, temp[1]);
pstmt.addBatch();有问题。





举报
jinkuake
发帖于2年前 4回/300阅
顶部