MySQL 使用 Statement.RETURN_GENERATED_KEYS 时出错

MySQL Error when using 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.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 连接器库,所以我从版本 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);

航班的 table 定义:

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