Обсуждение: Exception while doing ResultSetMetadata.getColumnName() after select setval()

Поиск
Список
Период
Сортировка

Exception while doing ResultSetMetadata.getColumnName() after select setval()

От
Stephane Bailliez
Дата:
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

Re: Exception while doing ResultSetMetadata.getColumnName() after select setval()

От
Kris Jurka
Дата:

On Tue, 18 Sep 2007, Stephane Bailliez wrote:

> 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
>
>
>            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");

In JDBC columns go from 1 -> count, not 0 -> count-1.

Kris Jurka

Re: Exception while doing ResultSetMetadata.getColumnName() after select setval()

От
Stephane Bailliez
Дата:
Kris Jurka wrote:
>
> In JDBC columns go from 1 -> count, not 0 -> count-1.

ah.. one more reason to get rid of this script I suppose. Thanks for the
heads up.


for the records, the previous workaround was of course not working..(I
put that on the lack of sleep) it needed to be wrapped into insert in a
temp table to avoid the result set issue.

cheers,

-- stephane