Exception while doing ResultSetMetadata.getColumnName() after select setval() - Mailing list pgsql-jdbc

From Stephane Bailliez
Subject Exception while doing ResultSetMetadata.getColumnName() after select setval()
Date
Msg-id fcmv9h$eki$1@sea.gmane.org
Whole thread Raw
Responses Re: Exception while doing ResultSetMetadata.getColumnName() after select setval()
List pgsql-jdbc
jdbc driver: postgresql-8.1-410.jdbc3.jar
Sun jdk 1.6.0_01 windows xp
postgresql 8.1.5 on windows xp and postgresql 8.1.9 on ubuntu


Came across what looks like a bug in the driver when I wanted to realign
sequences.

Works fine when I inject everything from pgadmin, but I have a java code
that insert every statement one by one and the 'verification' done by
the code throws up an exception.

sql is typically:

create table mytable (  serial id ... );
insert table (id, ...) values (101, ...);
..

-- this lines blows up with exception
select setval ('mytable_id', max(id)) from mytable;


It works fine if I wrap the select around another select ie something
like this:

select 1 from (select setval ('mytable_id', max(id)) from mytable) as
workaround;


Stacktrace is:
org.postgresql.util.PSQLException: The column index is out of range: 0,
number of columns: 1. at
org.postgresql.jdbc2.AbstractJdbc2ResultSetMetaData.getField(AbstractJdbc2ResultSetMetaData.java:639)
at
org.postgresql.jdbc2.AbstractJdbc2ResultSetMetaData.getColumnLabel(AbstractJdbc2ResultSetMetaData.java:279)
at
org.postgresql.jdbc2.AbstractJdbc2ResultSetMetaData.getColumnName(AbstractJdbc2ResultSetMetaData.java:294)
at com.ibatis.common.jdbc.ScriptRunner.runScript(ScriptRunner.java:175)
at com.ibatis.common.jdbc.ScriptRunner.runScript(ScriptRunner.java:107)


The code in iBatis does (see XXX note for line 175 reference) :

[...]
           Statement statement = conn.createStatement();

           println(command);

           boolean hasResults = false;
           if (stopOnError) {
             hasResults = statement.execute(command.toString());
           } else {
             try {
               statement.execute(command.toString());
             } catch (SQLException e) {
               e.fillInStackTrace();
               printlnError("Error executing: " + command);
               printlnError(e);
             }
           }

           if (autoCommit && !conn.getAutoCommit()) {
             conn.commit();
           }

           ResultSet rs = statement.getResultSet();
           if (hasResults && rs != null) {
             ResultSetMetaData md = rs.getMetaData();
             int cols = md.getColumnCount();
             for (int i = 0; i < cols; i++) {
               String name = md.getColumnName(i); // XXX Exception here
               print(name + "\t");
             }
             println("");
             while (rs.next()) {
               for (int i = 0; i < cols; i++) {
                 String value = rs.getString(i);
                 print(value + "\t");
               }
               println("");
             }
           }

           command = null;
           try {
             statement.close();
           } catch (Exception e) {
             // Ignore to workaround a bug in Jakarta DBCP
           }
[...]

cheers,

-- stephane

pgsql-jdbc by date:

Previous
From: Kris Jurka
Date:
Subject: Re: Batch INSERT failing with error 22P02
Next
From: Kris Jurka
Date:
Subject: Re: Exception while doing ResultSetMetadata.getColumnName() after select setval()