更新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);