java.sql.SQLException: Generated keys not requested. You need to ... 错误
来源:互联网 发布:windows ce应用小游戏 编辑:程序博客网 时间:2024/06/16 12:53
主要方法
/**
* 添加一条数据
* @param bean
*/
public void add(Category bean){
String sql=”insert into category values(null,?)”;
try (Connection connection = DBUtil.getConnction();
PreparedStatement preparedStatement=connection.prepareStatement(sql);
) {
preparedStatement.setString(1,bean.getName());
preparedStatement.execute();
//获得主键id号
ResultSet resultSet=preparedStatement.getGeneratedKeys();
if(resultSet.next()){
int id=resultSet.getInt(1);
bean.setId(1);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
错误详情
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。
PreparedStatement preparedStatement=connection.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
修改代码
/**
* 添加一条数据
* @param bean
*/
public void add(Category bean){
String sql=”insert into category values(null,?)”;
try (Connection connection = DBUtil.getConnction();
//修改部分
PreparedStatement preparedStatement=connection.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
) {
preparedStatement.setString(1,bean.getName());
preparedStatement.execute();
//获得主键id号
ResultSet resultSet=preparedStatement.getGeneratedKeys();
if(resultSet.next()){
int id=resultSet.getInt(1);
bean.setId(id);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
成功运行。
- java.sql.SQLException: Generated keys not requested. You need to ... 错误
- 【JavaEE】SQLException:Generated keys not requested. You need to specify Statement.RETURN_GE
- Caused by: java.sql.SQLException: Generated keys not requested
- 更新mysql驱动5.0-6.0 Generated keys not requested. You need to specify Statement.RETURN_GENERATED_KEYS
- MYSQL驱动包升级到5.1.17版本之后会出现的问题:Generated keys not requested. You need to specify Statement.RETURN_GENERATED_KEYS to Statement.execu
- java.sql.SQLException: Io : NL Exception was generated错误解决(jdbc数据源问题)
- java.sql.SQLException错误!
- java.sql.SQLException错误
- ShortCut keys for Codeblocks you need know
- java.sql.SQLException: Userdoes not have access to metadata required to determine stored procedurepa
- MacOS出现 You need to restart your computer....错误
- Couldn't find a tree builder with the features you requested: lxml. Do you need to install a parser
- java.sql.SQLException: User not found: SA
- java.sql.SQLException: Locale not recognized处理
- java.sql.SQLException: ORA-01799: a column may not be outer-joined to a subquery
- java.sql.SQLException: null, message from server: "Host 'xxx' is not allowed to connect
- java.sql.SQLException: null, message from server: "Host 'xxx' is not allowed to connect
- java.sql.SQLException: Connection is read-only. Queries leading to data modification are not allowed
- 开发工具——SharedPerference使用
- 【python爬虫笔记】一:先爬一个小页面
- Hibernate 常见异常
- Linux系统结构 详解
- javascript常用实例的实现与封装1
- java.sql.SQLException: Generated keys not requested. You need to ... 错误
- mybatis中 插入字符串空值无法修改
- js回调函数在项目中的运用
- python用dlib关键点将人脸背景区域去除
- 顺序表的创建,插入,修改,删除,查询代码
- ArcGIS JS 实现前端添加shape并保存到服务器
- pc端防止百度转码
- 7-表格显示案例
- 代码检测工具 joern 安装