setAutoCommit
false であり、接続を閉じる前に例外がスローされますが、それでもトランザクションはコミットされます。これは奇妙な動作ではありませんか?
public static void insertEmployee(){
String query1 = "INSERT INTO EMPLOYEE VALUES(80, 'from code')";
String query2 = "INSERT INTO EMPLOYEE VALUES(81, 'from code')";
Connection connection = null;
Statement statement = null;
try {
connection = DriverManager.getConnection(url, username, password);
connection.setAutoCommit(false);
statement = connection.createStatement();
statement.executeUpdate(query1);
ResultSet resultSet = statement.executeQuery(query2);
while(resultSet.next()) //this code throws the exception kept like this intentionally
{
int empNo = resultSet.getInt("EMPLOYEE_ID");
String eName = resultSet.getString("EMPLOYEE_NAME");
System.out.println("eName = " + eName);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
statement.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}