pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Stephane Bailliez <sbailliez@gmail.com>
To: pgsql-jdbc@postgresql.org
Subject: Exception while doing ResultSetMetadata.getColumnName() after select setval()
Date: Tue, 18 Sep 2007 00:30:57 +0200
Message-ID: <fcmv9h$eki$1@sea.gmane.org> (raw)

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




view thread (3+ messages)  latest in thread

Message-ID: <fcmv9h$eki$1@sea.gmane.org>
Permalink:  ../fcmv9h$eki$1@sea.gmane.org/
Also on:    postgresql.org/message-id/fcmv9h$eki$1@sea.gmane.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-jdbc@postgresql.org
  Cc: sbailliez@gmail.com
  Subject: Re: Exception while doing ResultSetMetadata.getColumnName() after select setval()
  In-Reply-To: <fcmv9h$eki$1@sea.gmane.org>
  References: <fcmv9h$eki$1@sea.gmane.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox