1 回答
![?](http://img1.sycdn.imooc.com/54584c910001b8d902200220-100-100.jpg)
TA贡献1785条经验 获得超4个赞
您可以使用以下方法来获取生成的密钥:
try (Connection con = super.getConnection()) {
String query = "INSERT INTO \"Workout\" VALUES(?,?,?,?)";
String query2 = "INSERT INTO Workout_exercise ... ";
PreparedStatement pstmt = con.prepareStatement(query,PreparedStatement.RETURN_GENERATED_KEYS);
pstmt.setString(1, titel);
pstmt.setString(2, beschrijving);
pstmt.setInt(3, persoon_id);
pstmt.setInt(4, categorie_id);
pstmt.executeUpdate();
result = true;
try (ResultSet rs = pstmt.getGeneratedKeys()) {
if (rs != null && rs.next()) {
workout_pkey = rs.getInt(1);
PreparedStatement pstmt2=con.prepareStatement(query2);
...
for (Workout_exercise ex: exs) {
pstmt2.setInt(1, workout_pkey);
...
pstmt2.addBatch();
}
pstmt2.executeBatch();
}
}
} catch (SQLException sqle) {
sqle.printStackTrace();
}
添加回答
举报