我继承了一个旧的Servlet/JSP网站,该网站已更新为Java 1.8.0_201和MySQL 5.7.28。最近,我在向数据库添加新记录时开始出现此错误:
Generated keys not requested. You need to specify Statement.RETURN_GENERATED_KEYS to Statement.executeUpdate(), Statement.executeLargeUpdate() or Connection.prepareStatement().
我用谷歌搜索了发生的事情,发现我需要将 Statement.RETURN_GENERATED_KEYS 添加到我的 Statement.executeUpdateQuery 中,所以我这样做了。但是,我仍然收到错误。更新后的代码,错误发生在语句result = stmt.getGenerateKeys();
:
stmt = con.createStatement();
switch(queryType) {
case INSERT_QUERY:
stmt.executeUpdate(query, Statement.RETURN_GENERATED_KEYS);
int autoIncKey = -1;
result = stmt.getGeneratedKeys();
if (result.next()) {
autoIncKey = result.getInt(1);
}
rows = stmt.getUpdateCount();
svr.setGeneratedKey(autoIncKey);
obj.setGeneratedKey(autoIncKey);
svr.setRows(rows); //Insert/Update/Delete
if (rows > 0)
svr.setSuccess(true);
else
svr.setSuccess(false);
break;
但是,插入有效并且数据已放入数据库中。
然后我想我应该更新Mysql Connector库,所以我从版本5.4更新到mysql-connector-java-8.0.18.jar。仍然遇到同样的错误。
我没有使用任何准备好的语句,只是使用一个字符串作为查询文本。这是查询字符串:
INSERT INTO Flights(rocket_name, weight, angle, baseline, egg_id, shockcord_id, notes, date, rocketModelID, mission_specialists, flight_engineers, teacher_id) VALUES ('asdfasdfasd', 98.0, 60.0, 60.0, 2, 2, 'sfdg sfdg sdg sfdg sdfg sfdg sfdg', '2020-01-07', 4,'asdfasdf', 'asdfasdfas', 13);
航类的表定义:
| Flights | CREATE TABLE `Flights` (
`flight_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`teacher_id` int(11) NOT NULL DEFAULT '0',
`egg_id` int(10) unsigned NOT NULL DEFAULT '0',
`shockcord_id` int(10) unsigned NOT NULL DEFAULT '0',
`rocket_name` varchar(100) NOT NULL DEFAULT '',
`weight` decimal(10,4) unsigned NOT NULL DEFAULT '0.0000',
`angle` decimal(10,5) NOT NULL DEFAULT '0.00000',
`baseline` decimal(10,5) NOT NULL DEFAULT '0.00000',
`date` date NOT NULL DEFAULT '0000-00-00',
`rocketModelID` int(11) unsigned NOT NULL DEFAULT '0',
`flight_engineers` varchar(100) NOT NULL DEFAULT '',
`mission_specialists` varchar(100) NOT NULL DEFAULT '',
`notes` text,
PRIMARY KEY (`flight_id`),
FULLTEXT KEY `search1` (`mission_specialists`),
FULLTEXT KEY `search2` (`flight_engineers`),
FULLTEXT KEY `search3` (`flight_engineers`,`mission_specialists`)
) ENGINE=MyISAM AUTO_INCREMENT=562 DEFAULT CHARSET=latin1
我不知道如何继续。任何建议将不胜感激!
标记
最佳答案
建议按如下方式更改您的代码:
为键列命名
通过
executeUpdate
调用获取行数如果没有插入行,请勿调用
getGenerateKeys()
rows = stmt.executeUpdate(query, new String[] { "flight_id" });
int autoIncKey = -1;
if (rows > 0) {
result = stmt.getGeneratedKeys();
if (result.next()) {
autoIncKey = result.getInt(1);
}
}
svr.setGeneratedKey(autoIncKey);
obj.setGeneratedKey(autoIncKey);
svr.setRows(rows); //Insert/Update/Delete
svr.setSuccess(rows > 0);
尽管如此,使用 VALUES
的单个 INSERT
语句始终会恰好插入一行,因此完全不需要检查行数。如果未插入该行,则会引发异常,因此您的代码可以简化为:
stmt.executeUpdate(query, new String[] { "flight_id" });
try (ResultSet result = stmt.getGeneratedKeys()) {
result.next();
int autoIncKey = result.getInt(1);
svr.setGeneratedKey(autoIncKey);
obj.setGeneratedKey(autoIncKey);
}
svr.setRows(1);
svr.setSuccess(true);
关于java - MySQL使用Statement.RETURN_GENERATED_KEYS时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59737397/