For my work i need to create several PrepareStatements which are inserted in my table. Normally i have no problem with this, but this problem don’t make any sense to me.
I work in a give environment which already establishes the Connection with the database.
Normally, creating my statements in this connection is simple. But in this case, Update or Inserts are not going through.
After some failures, i made a seperate method just for a testcase, nothing that should interfere with the connection, but even this doesnt work.
public void pleasedotheWorkTes() {
Connection conn = Transaction.getCurrentConnection;
PreparedStatement pepaw = null;
try {
conn.setAutoCommit(false);
String sqlSelect = " Select ID FROM Table1 WHERE ort = 'B' ";
pepaw = conn.prepareStatement(sqlSelect);
Boolean checkSelect = pepaw.execute();
String sqlInsert = " Update Table1 Set ort = 'C' WHERE ID = '10' ";
pepaw = conn.prepareStatement(sqlSelect);
Integer checkInsert = pepaw.executeUpdate();
conn.commit();
pepaw.close();
conn.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
While the Select Statement is running and works without an issue. (Boolean = true)
The Insert Statements throws no error, but only a one as an Integer and doesn’t update anything in the table. The Statement works in my db visualizer.
As far as i know, a Connection needs to be “False” in AutoCommit to have a better control over the statements. The Commit in the end makes sure the changes are transferred, but nothing is happening.
I am a bit clueless what i am doing wrong. I am worried the connection in the background is messing it up, but I created a extra object out of it and close the connection.
1
You have used sqlselect twice — given the corrected code.
Check if this works
String sqlInsert = ” Update Table1 Set ort = ‘C’ WHERE ID = ’10’ “;
pepaw = conn.prepareStatement(sqlInsert);