我的代码从文本文件中读取数据以填充数据库。文本文件包含5行,包括空行(第4行)。代码读取文本文件,但只插入数据库中的最后一行。我还需要代码跳过阅读文本文件中的第一行。
我出错的任何帮助?我是Java Programming的新手。
这是我的代码:
import java.io.*;
import java.sql.*;
public class Example {
public static Connection getConnection() throws Exception {
String driver = "org.apache.derby.jdbc.ClientDriver";
String url = "jdbc:derby://localhost:1527/Orders";
String username = "user";
String password = "password";
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, username, password);
return conn;
}
public static void main(String[] args)throws Exception {
String id ="";
String firstName ="";
String lastName ="";
String street="";
String city ="";
String fileName = "src/Person.data";
String line = null;
PreparedStatement pstmt = null;
Connection conn = null;
try {
FileReader fileReader =
new FileReader(fileName);
BufferedReader bufferedReader =
new BufferedReader(fileReader);
while((line = bufferedReader.readLine()) != null) {
if ( line.trim().length() == 0 ) {
continue; // Skip blank lines
}
String[] splited = line.split(",");
//System.out.println(line);
id=splited[0];
firstName=splited[1];
lastName=splited[2];
street=splited[3];
city=splited[4];
System.out.println(line);
System.out.println(splited[4]);
conn = getConnection();
conn.setAutoCommit(false);
pstmt = conn.prepareStatement("insert into APP.Person(PERSON_ID, LAST_NAME, FIRST_NAME,STREET,CITY)values (?,?,?,?,?)");
pstmt.setString(1, id);
pstmt.setString(2, firstName);
pstmt.setString(3, lastName);
pstmt.setString(4, street);
pstmt.setString(5, city);
pstmt.executeUpdate();
conn.commit();
}
bufferedReader.close();
}
catch(FileNotFoundException ex) {
System.out.println(
"Unable to open file '" +
fileName + "'");
ex.printStackTrace();
}
catch(IOException ex) {
System.out.println(
"Error reading file '"
+ fileName + "'");
}
}
}
答案 0 :(得分:2)
这是因为插入仅针对最后一条记录执行。更好的选择是使用executebatch。首先批量添加所有这些插入脚本,并在完成循环后执行该批处理。
Prepared statement is created inside the loop which is wrong. You need to create a Prepared statement out of the loop.
使用try catch块如下:
try {
FileReader fileReader =
new FileReader(fileName);
BufferedReader bufferedReader =
new BufferedReader(fileReader);
conn = getConnection();
conn.setAutoCommit(false);
pstmt = conn.prepareStatement("insert into APP.Person(PERSON_ID, LAST_NAME, FIRST_NAME,STREET,CITY)values (?,?,?,?,?)");
while((line = bufferedReader.readLine()) != null) {
if ( line.trim().length() == 0 ) {
continue; // Skip blank lines
}
String[] splited = line.split(",");
//System.out.println(line);
id=splited[0];
firstName=splited[1];
lastName=splited[2];
street=splited[3];
city=splited[4];
System.out.println(line);
System.out.println(splited[4]);
pstmt.setString(1, id);
pstmt.setString(2, firstName);
pstmt.setString(3, lastName);
pstmt.setString(4, street);
pstmt.setString(5, city);
pstmt.addBatch();
}
pstmt.executeBatch();
conn.commit();
bufferedReader.close();
}
catch(FileNotFoundException ex) {
System.out.println(
"Unable to open file '" +
fileName + "'");
ex.printStackTrace();
}
catch(IOException ex) {
System.out.println(
"Error reading file '"
+ fileName + "'");
}