我正在尝试使用GetGeneratedKey方法将刚创建的表ID作为一个整数返回。但这是行不通的。我的密码 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)它返回以下内容org.apache.tomcat.dbcp.dbcp2.DelegatingResultSet@8a14f70
添加回答
举报
0/150
提交
取消