MySQL 使用Statement.RETURN_GENERATED_KEYS时出错



我继承了一个旧的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.executeUpdateQuery中添加Statement.RETURN_GENERATED_KEYS,所以我做了。但是,我仍然收到错误。更新的代码,错误发生在语句result = stmt.getGeneratedKeys();

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调用中获取行计数

  • 如果未插入行,则不要调用getGeneratedKeys()

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

最新更新