[
https://issues.apache.org/jira/browse/DERBY-6849?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15076597#comment-15076597
]
John Hendrikx commented on DERBY-6849:
--------------------------------------
You've actually reproduced the problem already.
>From Java docs, getGeneratedKeys
>(https://docs.oracle.com/javase/7/docs/api/java/sql/Statement.html#getGeneratedKeys()):
{quote}Retrieves any auto-generated keys created as a result of executing this
Statement object. If this Statement object did not generate any keys, an
*empty* ResultSet object is returned.{quote}
The table has no generated keys at all, yet, rs.next() returns true and returns
us a record with a null value in it. An empty ResultSet would be one that
returns false right away on the first rs.next() call. I'd expect you're sample
program to not print anything at all as it should not enter the if block with
the print line.
> Statement.RETURN_GENERATED_KEYS returns "keys" even if there are no
> auto-generated fields
> -----------------------------------------------------------------------------------------
>
> Key: DERBY-6849
> URL: https://issues.apache.org/jira/browse/DERBY-6849
> Project: Derby
> Issue Type: Bug
> Components: JDBC
> Affects Versions: 10.9.1.0
> Reporter: John Hendrikx
> Attachments: DERBY6849Repro.java
>
>
> I have a very simple table:
> {noformat}
> CREATE TABLE images (
> url varchar(1000) NOT NULL,
> image blob NOT NULL,
>
> CONSTRAINT images_url PRIMARY KEY (url)
> );
> {noformat}
> No auto-generated fields. However when I do an insert, JDBC tells me there
> are auto-generated keys (rs.next() does not return false and a LONG value is
> returned):
> {noformat}
> try(PreparedStatement statement = connection.prepareStatement(sql,
> Statement.RETURN_GENERATED_KEYS)) {
> setParameters(parameterValues, statement);
> statement.execute();
> try(ResultSet rs = statement.getGeneratedKeys()) {
> if(rs.next()) {
> return rs.getObject(1);
> }
> return null;
> }
> }
> catch(SQLException e) {
> throw new DatabaseException(this, sql + ": " + parameters, e);
> }
> {noformat}
> This sounds like a bug to me. For comparison, PostgreSQL does not have the
> same behaviour.
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)