我正在嘗試使用GetGeneratedKey方法將剛創(chuàng)建的表ID作為一個整數(shù)返回。但這是行不通的。我的密碼 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;}它總是返回0!如果我打印keys.getint(1)它返回以下內(nèi)容org.apache.tomcat.dbcp.dbcp2.DelegatingResultSet@8a14f70
添加回答
舉報
0/150
提交
取消