Java JDBC executeUpdate()只插入1行
我的代码从文本文件读取数据以填充数据库。文本文件包含5行,包括一个空行(第4行)。代码读取文本文件,但只插入数据库中的最后一行。我还需要代码来跳过阅读文本文件中的第一行 我哪里出了问题,有人帮我吗?我是Java编程新手 这是我的密码:Java JDBC executeUpdate()只插入1行,java,database,jdbc,Java,Database,Jdbc,我的代码从文本文件读取数据以填充数据库。文本文件包含5行,包括一个空行(第4行)。代码读取文本文件,但只插入数据库中的最后一行。我还需要代码来跳过阅读文本文件中的第一行 我哪里出了问题,有人帮我吗?我是Java编程新手 这是我的密码: import java.io.*; import java.sql.*; public class Example { public static Connection getConnection() throws Exception {
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 + "'");
}
}
}
这是因为插入只对最后一条记录执行。更好的选择是使用executebatch。首先在批处理中添加所有这些插入脚本,在循环完成后只执行该批处理
Prepared语句是在循环内部创建的,这是错误的。您需要从循环中创建一个准备好的语句。
使用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 + "'");
}
这是因为插入只对最后一条记录执行。更好的选择是使用executebatch。首先在批处理中添加所有这些插入脚本,在循环完成后只执行该批处理
Prepared语句是在循环内部创建的,这是错误的。您需要从循环中创建一个准备好的语句。
使用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 + "'");
}
格式化你的代码。你有从随机位置开始的行和令人困惑的大括号。格式化你的代码。你有从随机位置开始的行和令人困惑的大括号。如果你想使用批处理执行,那么你需要将
executeUpdate
替换为executeBatch
,并且你应该将准备好的语句的创建移出循环。@MarkRotteveel true,我只是在给他指路。更新了代码。谢谢你suggetion@Hemant谢谢我已经将准备好的语句的创建移到了循环之外,并使用了executeBatch,但我仍然得到了插入数据库的最后一条记录My bad。经过一次干净利落的修缮后,它开始工作了。非常感谢您的支持help@Gokuh如果我的答案对您有帮助,请将答案标记为已接受。请单击我答案旁边的“是”标记。如果要使用批处理执行,则需要将executeUpdate
替换为executeBatch
,并且应将准备语句的创建移出循环。@markrotVeel true,我只是在给他指路。更新了代码。谢谢你suggetion@Hemant谢谢我已经将准备好的语句的创建移到了循环之外,并使用了executeBatch,但我仍然得到了插入数据库的最后一条记录My bad。经过一次干净利落的修缮后,它开始工作了。非常感谢您的支持help@Gokuh如果我的答案对您有帮助,请将答案标记为已接受。请点击我答案旁边的“是”标记。