我无法使用PostgreSQL JDBC驱动程序获取getGeneratedKeys

时间:2016-04-24 19:35:42

标签: postgresql jdbc resultset

我在其中一个功能中遇到了一个愚蠢的问题。 我想获取我的一个Sql查询的generatedKey。我不能,我不明白为什么。问题是resultSet.next()即使在检查表中的数据时插入了行,它也会返回false。

这是我的代码:

Statement statement = connection.createStatement();
statement.executeUpdate("INSERT INTO calamar.calamar.application (nom,criticite,autorise) VALUES ('"+nom+"','"+criticite+"','"+false+"');");

 ResultSet resultSet = statement.getGeneratedKeys();
 resultSet.next();

解决方案:添加Statement.RETURN_GENERATED_KEYS

Statement statement = connection.createStatement();
statement.executeUpdate("INSERT INTO calamar.calamar.application (nom,criticite,autorise) VALUES ('"+nom+"','"+criticite+"','"+false+"');", Statement.RETURN_GENERATED_KEYS);

ResultSet resultSet = statement.getGeneratedKeys();
resultSet.next();

0 个答案:

没有答案
相关问题