So, I'm trying to learn. I apologize for the slopinnes. This code goes on transactions, so the autocommit must be false, but it throwns an unhandled SQLException on conn.rollback() and I don't understand why if it's inside a catch(exception) already... Shall I wrap everything on another try-catch?
try {
conn.setAutoCommit(false);
String sql="SELECT l.toy_id FROM LETTER l WHERE toy_id=?";
PreparedStatement selectStatement = conn.prepareStatement(sql);
String sqlD="DELETE FROM TOY WHERE toy_id=?";
PreparedStatement deleteStatement = conn.prepareStatement(sqlD);
String sqlU="INSERT INTO TOY (toy_id, toy_name, price,toy_type, manufacturer) VALUES (?,?,?,?,?)";
PreparedStatement UpdateStatement = conn.prepareStatement(sqlU);
// TODO Update or delete Toy for every row in file
for (List<String> row : fileContents) {//!!!!!!!no borrar!!!
int toy_id=getToyId(row);
//should another try go here??
selectStatement.setInt(1, toy_id);
ResultSet rs = selectStatement.executeQuery(sql);
if (!rs.first()) {//HERE WE DELETE
deleteStatement.setInt(1, toy_id);
deleteStatement.executeUpdate();
}else {
while (rs.next()) {//HERE WE UPDATE
UpdateStatement.setInt(1, toy_id);
UpdateStatement.executeUpdate();
}
}
rs.close();
}
conn.commit();
}
catch (SQLException e){
System.out.println("ERRRROOOOOOORRRRR");
conn.rollback();
}