I have discovered that if I create a table with only one column, such as
CREATE TABLE mo.company (
id BIGINT NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),
PRIMARY KEY (id)
);
id BIGINT NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),
PRIMARY KEY (id)
);
then execute
String sql = "INSERT INTO mo.company VALUES (DEFAULT)";
PreparedStatement preparedStatement = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
preparedStatement.executeUpdate();
ResultSet resultSet = preparedStatement.getGeneratedKeys();
System.out.println("resultSet.next()==" + resultSet.next());
Object object = resultSet.getObject(1);
if (object == null) {
System.out.println("object is null");
} else {
long id = resultSet.getLong(1);
System.out.println("id==" + id);
}
PreparedStatement preparedStatement = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
preparedStatement.executeUpdate();
ResultSet resultSet = preparedStatement.getGeneratedKeys();
System.out.println("resultSet.next()==" + resultSet.next());
Object object = resultSet.getObject(1);
if (object == null) {
System.out.println("object is null");
} else {
long id = resultSet.getLong(1);
System.out.println("id==" + id);
}
I always get null returned from resultSet.getObject(1), whereas if I add a second column to the table (a VARCHAR(100)) then I get the generated key value.
Is this a bug?
Bob Jaster
Do you Yahoo!?
Get on board. You're invited to try the new Yahoo! Mail.
