1
我有一个代码从文件中读取数据并将其插入到数据库中。为什么java抛出异常java.lang.ArrayIndexOutOfBoundsException:1
该代码将数据写入文件。
public void save(Collection<Book> b) {
try (PrintWriter print = new PrintWriter(this.file);) {
for (Book book : b) {
String str = book.getName() + "," + book.getAuthor() + ","
+ book.getDate() + "\n";
print.println(str);
}
} catch (Exception e) {
}
}
此代码写入文件中的数据并将其插入到db中。
try(Reader reader = new FileReader(this.file);
BufferedReader br = new BufferedReader(reader)) {
connection = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/myBook", this.userName,
this.pass);
Statement statement = connection.createStatement();
String str;
while((str = br.readLine()) != null){
String[] array = str.split(",");
statement.executeUpdate("Insert Into myBook.book (name,author,pubDate) values('"+array[0]+"', '"+ array[1]+"', '"+array[2]+"')");
}
}
但它抛出异常
java.lang.ArrayIndexOutOfBoundsException
有什么不对?
因为'array'只有一个元素 – gefei 2015-04-01 14:05:06
输入不包含'“,”'。它应该有两个或更多的能够使用'array [1]'和'array [2]'。 – Bubletan 2015-04-01 14:06:12
不,它包含绝对是我的行在文件中 Chenty,Rafi,01.25.1850 Kaytser,Rafi,01.25.1855 – Arno 2015-04-01 14:07:36