我正在尝试从特定文件夹中读取csv文件中的数据,并根据第一个参数将数据插入表中。数据库是postgres。但我得到java.sql.BatchUpdateException。有人可以帮帮我吗以下是我的代码。
public class IOReader {
Connection con;
static PreparedStatement statement= null;
String url, user, password= null;
public IOReader() throws IOException {
url = Config.DB_URL2;
user= Config.DB_USERNAME2;
password= Config.DB_PASSWORD2;
}
public void insert() throws SQLException
{
File dir = new File("C:\\Users\\Desktop\\data");
String currentLine;
con= DriverManager.getConnection(url, user, password);
con.setAutoCommit(false);
String query_s = "INSERT INTO Vehicle"
+ "(vehicle_id, vehicle_timestamp, message_id," +
"begin_timestamp,recd_timestamp," +
"model) VALUES (?,?,?,?,?,?)";
try {
statement= con.prepareStatement(query_s);
con.setAutoCommit(false);
final int batchSize =1000;
int count=0;
if (!dir.isDirectory())
throw new IllegalStateException();
for (File file : dir.listFiles()) {
BufferedReader br;
try {
br = new BufferedReader(new FileReader(file));
while ((currentLine = br.readLine()) != null) {
if(currentLine.contains(",")){
List<String> values = Arrays.asList(currentLine.split(","));
if(values.get(0).contentEquals("AAA")){
dbSave(values, con);
}
}
}
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(++count % batchSize ==0){
statement.executeBatch();
}
}
statement.executeBatch();
con.commit();
} catch (SQLException e) {
e.printStackTrace();
System.out.println(e.getMessage());
con.rollback();
} finally {
if (statement != null) {
statement.close();
}
if (con != null) {
con.close();
}
}
}
private void dbSave(List<String> values, Connection connection) throws SQLException {
try {
statement.setString(1, values.get(0));
statement.setLong(2, Long.valueOf(values.get(1)));
statement.setInt(3, Integer.valueOf(values.get(2)));
statement.setLong(4, Long.valueOf(values.get(3)));
statement.setLong(5, Long.valueOf(values.get(4)));
statement.setString(6,values.get(5));
statement.addBatch();
} catch (SQLException e) {
e.printStackTrace();
System.out.println("Could not save to DB, error: " + e.getMessage());
}
}
}
我得到的例外是:
java.sql.BatchUpdateException Caused by: java.sql.BatchUpdateException: Batch entry14.170 INSERT INTO Vehicle(vehicle_id, vehicle_timestamp, message_id,begin_timestamp,recd_timestamp,model) VALUES ('AAA','1370253541234','190049','1753891','1753891','PPP') was aborted. Call getNextException to see the cause.
java.sql.BatchUpdateException: Batch-Eintrag 14.170 INSERT INTO Vehicle(vehicle_id, vehicle_timestamp, message_id,begin_timestamp,recd_timestamp,model) VALUES ('AAA','1370253541234','190049','1753891','1753891','PPP') wurde abgebrochen. Rufen Sie 'getNextException' auf, um die Ursache zu erfahren.
Execution successful
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeBatch(AbstractJdbc2Statement.java:2754)
at src.io.IOReader.readSave(IOReader.java:151)
at src.startup.Initiator.main(Initiator.java:40)
我的表格结构如下:
Table Vehicle
vehicle_id character varying NOT NULL,
vehicle_timestamp bigint NOT NULL,
message_id integer NOT NULL,
begin_timestamp bigint NOT NULL,
recd_timestamp bigint NOT NULL,
model character varying NOT NULL)
primary key: vehicle_timestamp,vehicle_id