Hi @HAIBING Qiao ,
Thank you for reaching out to the Microsoft Q & A Forum.
JDBC drivers enforce stricter rules for PreparedStatement execution. They often disallow multiple SQL statements in one call for security and consistency reasons. In older versions, the driver parsed and executed all statements sequentially, returning the last ResultSet. In 12.10, the driver likely ignores subsequent statements after the first DML operation or treats them as invalid for PreparedStatement.
ps.execute() now returns false indicating no ResultSet because the first statement (DELETE) does not produce a result set. The SELECT at the end is never executed, so resultset becomes null.
Please try below workarounds.
- Split the statements into separate calls.
PreparedStatement deleteStmt = conn.prepareStatement("DELETE FROM TestTable");
deleteStmt.executeUpdate();
PreparedStatement insertStmt = conn.prepareStatement("INSERT INTO TestTable (c1,c2) VALUES (?, ?)");
insertStmt.setString(1, value1);
insertStmt.setString(2, value2);
insertStmt.executeUpdate();
PreparedStatement selectStmt = conn.prepareStatement("SELECT * FROM TestTable");
ResultSet rs = selectStmt.executeQuery();
- Use Statement with execute() for multiple statements.
Statement stmt = conn.createStatement();
boolean hasResultSet = stmt.execute("DELETE FROM TestTable; INSERT INTO TestTable (c1,c2) VALUES ('a','b'); SELECT * FROM TestTable");
while (true) {
if (hasResultSet) {
ResultSet rs = stmt.getResultSet();
} else {
int updateCount = stmt.getUpdateCount();
if (updateCount == -1) break;
}
hasResultSet = stmt.getMoreResults();
}
- Batch updates for DML, then a separate query for SELECT.
I hope this information helps. Please do let us know if you have any further queries.
Regards,
Dinesh