更新mysql驅動5.1-47 Generated keys not requested. 錯誤
阿新 • • 發佈:2020-12-21
更新mysql驅動5.1-47後出現錯誤:Generated keys not requested. You need to specify Statement.RETURN_GENERATED_KEY
java.sql.SQLException: Generated keys not requested. You need to specify Statement.RETURN_GENERATED_KEYS to Statement.executeUpdate(), Statement.executeLargeUpdate() or Connection.prepareStatement().
經過檢查發現從5.1.7版本之後的mysql-connector增加了返回GeneratedKeys的條件,如果需要返回GeneratedKeys,則PreparedStatement需要顯式新增一個引數Statement.RETURN_GENERATED_KEYS,如下:
connection = JDBCUtil.getConnection();
preparedStatement = connection.prepareStatement(sql.toString(),Statement.RETURN_GENERATED_KEYS);