使用Statement.RETURN_GENERATED_KEYS时的MySQL错误

问题描述 投票:0回答:1

我继承了一个旧的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().

我用Google搜索发生了什么,发现我需要将Statement.RETURN_GENERATED_KEYS添加到Statement.executeUpdateQuery中,所以我这样做了。但是,我仍然会收到错误消息。更新的代码,并且错误发生在语句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 

我不确定如何继续。任何建议将不胜感激!

标记

java mysql jsp jdbc myisam
1个回答
0
投票

建议如下更改代码:

  • 命名键列

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

尽管,实际上,使用INSERT的单个VALUES语句将始终只插入一行,因此完全不需要检查行数。如果未插入该行,则将引发异常,因此您的代码可以简化为:

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);
© www.soinside.com 2019 - 2024. All rights reserved.