我试图从文本文件中获取数据并插入到mysql数据库中..某些行已成功插入但在运行程序时显示此错误消息..
java.io.IOException: Read error
at java.io.FileInputStream.readBytes(Native Method)
at java.io.FileInputStream.read(FileInputStream.java:199)
at java.io.DataInputStream.read(DataInputStream.java:132)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:264)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:306)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:158)
at java.io.InputStreamReader.read(InputStreamReader.java:167)
at java.io.BufferedReader.fill(BufferedReader.java:136)
at java.io.BufferedReader.readLine(BufferedReader.java:299)
at java.io.BufferedReader.readLine(BufferedReader.java:362)
at InsertDB.inserdata(InsertDB.java:53)
at InsertDB.main(InsertDB.java:96)
这是我的程序
public class InsertDB {
Connection conn ;
String url = "jdbc:mysql://localhost:3306/";
String dbName = "dna";
String driver = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "123";
public void inserdata()
{
String[] RowItems = new String[11];
int[] intRowItems=new int[10];
try{
conn = DriverManager.getConnection(url+dbName,userName,password);
Statement st = conn.createStatement();
// Open the file that is the first
// command line parameter
FileInputStream fstream = new FileInputStream("result.txt");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
StringTokenizer stringTokenizer ;
while ((strLine = br.readLine()) != null) {
stringTokenizer=new StringTokenizer(strLine);
int j=0;
while(stringTokenizer.hasMoreTokens()){
RowItems[j]=stringTokenizer.nextToken();
j++;
}
int c2,c3,c4,c5,c6,c7,c8,c9,c10,c11;
c2=Integer.valueOf(RowItems[1]).intValue();
c3=Integer.valueOf(RowItems[2]).intValue();
c4=Integer.valueOf(RowItems[3]).intValue();
c5=Integer.valueOf(RowItems[4]).intValue();
c6=Integer.valueOf(RowItems[5]).intValue();
c7=Integer.valueOf(RowItems[6]).intValue();
c8=Integer.valueOf(RowItems[7]).intValue();
c9=Integer.valueOf(RowItems[8]).intValue();
c10=Integer.valueOf(RowItems[9]).intValue();
c11=Integer.valueOf(RowItems[10]).intValue();
String temp=RowItems[0];
String insert="INSERT into protein_predicted VALUES('"+temp+"','"+c2+"','"+c3+"','"+c4+"','"+c5+"','"+c6+"','"+c7+"','"+c8+"','"+c9+"',' "+c10+"','"+c11+"')";
st.executeUpdate(insert);
in.close();
}
System.out.println("Insertted successfully");
}
catch (SQLException e) {
throw new RuntimeException(e);
}
catch (Exception e){//Catch exception if any
//System.err.println("Error: " + e.getMessage());
e.printStackTrace();
//Close the input stream
}
}
public static void main(String args[])
{
InsertDB a=new InsertDB();
a.inserdata();
}
我的文本文件包含多个列和多行,如下所示
1BG8A 76 33 0 18 1 0 1 5 2 16
请帮我解决这个问题..谢谢你
答案 0 :(得分:1)
您正在关闭正在阅读它的DataInputStream
循环内的while
。在您使用in.close()
BufferedReader
行并在while循环之外关闭br.close();
如果您使用的是Java 6或更低版本,理想情况下,您应在BufferedReader
... try
之外声明catch
变量并添加finally
子句如果发生读取错误,请关闭阅读器。在Java 7中,有一个AutoCloseable接口可以帮助自动处理。