0

I'm working with a Postgres db in java with java.sql. Can't copy the whole snippet due to nda, but it looks like this:

private final PostgresConnection postgresConnection = PostgresConnection.getInstance();
preparedStatement = postgresConnection.getConnection().
    prepareStatement("Insert into someTable(someColumn) values(?) RETURNING id");
preparedStatement.setString(i, (String) someParam);

resultSet = preparedStatement.executeQuery();
resultSet.next();

ResultSetMetaData meta = resultSet.getMetaData();

LOGGER.info("column count {}", meta.getColumnCount());

LOGGER.info("column type {}", meta.isAutoIncrement(0));

The query runs fine in the query tool (PgAdmin 4). It returns a single column, single row result. However, in java, the resultset refuses to give me anything. in the above code, the last line meta.isAutoIncrement(0), similar to resultSet.getInt(0) gives the same error that does not make sense to me:

 The column index is out of range: 0, number of columns: 1.

It's like the column array is secretly empty even though the resultSet is not aware of this.

hasdrubal
  • 1,024
  • 14
  • 30
  • You aren't executing a query (SELECT), you are executing an update (INSERT/UPDATE). Consider using `resultSet = preparedStatement.execute();` as shown [in this question's answer with the most upvotes](https://stackoverflow.com/questions/23691715/get-returning-value-from-postgresql-via-java), which will be in autocommit-mode if you don't explicitly switch that off. – deHaar Jun 24 '19 at 14:25
  • What is the value of the variable `i`? – Laurenz Albe Jun 24 '19 at 14:32
  • ResultSet column numbers start at 1, so getInt(0) is invalid. – mwarren Jun 24 '19 at 14:41

0 Answers0