【发布时间】:2014-08-26 14:34:52
【问题描述】:
这个异常发生在我的代码中提到的部分:
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
String query = "Insert into ...";
try {
con = DriverManager.getConnection(...);
ps = con.prepareStatement(query, java.sql.Statement.RETURN_GENERATED_KEYS);
ps.executeUpdate(query);
rs = ps.getGeneratedKeys(); // Exception is here
}
while (resultset.next()) {
id = String.valueOf(resultset.getInt(1));
}
例外:
未请求生成的密钥。您需要将 Statement.RETURN_GENERATED_KEYS 指定给 Statement.executeUpdate() 或 Connection.prepareStatement()
我的目的是插入一条新记录并将第一个字段(id)(即auto_increment)保存到变量id。
【问题讨论】:
-
@a_horse_with_no_name 我用mysql
-
@a_horse_with_no_name 查看更新。
-
第二个示例不包含
prepareStatement()调用的java.sql.Statement.RETURN_GENERATED_KEYS参数 -
@a_horse_with_no_name 见更新2
-
@a_horse_with_no_name 不,我的查询是
Insert语句,而不是UPDATE。
标签: java mysql sql jdbc sqlexception