i'm trying to return the just created table id as a int with the GetGeneratedKey method. But this does not work.
My code
public int saveWorkout(String titel, String beschrijving, int categorie_id, int persoon_id) throws SQLException {
int result = 0;
try (Connection con = super.getConnection()) {
String query = "INSERT INTO \"Workout\" VALUES(?,?,?,?)";
String query2 = "INSERT INTO \"Workout_Oefening\" VALUES(?,?)";
PreparedStatement pstmt = con.prepareStatement(query);
pstmt.setString(1, titel);
pstmt.setString(2, beschrijving);
pstmt.setInt(3, persoon_id);
pstmt.setInt(4, categorie_id);
pstmt.executeUpdate();
ResultSet keys = pstmt.getGeneratedKeys();
if(keys.next()) {
result = keys.getInt(1);
System.out.println(keys.getInt(1));
}
} catch (SQLException sqle) {
sqle.printStackTrace();
}
return result;
}
It always returns 0! if i print the keys.getint(1) thing it returns the following
org.apache.tomcat.dbcp.dbcp2.DelegatingResultSet@8a14f70