pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Saneesh Apte <san@calccit.org>
To: pgsql-bugs@postgresql.org
Subject: JDBC: 2 bugs: Getting a smallint array actually gets an integer array and   return type of a boolean array is bit.
Date: Mon, 28 Jun 2010 19:05:55 -0700
Message-ID: <4C295503.5020506@calccit.org> (raw)

Hi,

	I think I found two bugs (and I hope I am not wasting everyone's time).
	One is minor: the base type of a boolean[] is java.sql.Types.BIT instead or 
java.sql.Types.BOOLEAN.  At the very least shouldn't these be aliases for 
the same type?

	And secondly the returned type from a smallint[] is an Integer[] instead of 
a Short[].



	So running this code:

------------------------------------------------------------------

public class NewClass {
     public static void main(String[] args) {
         try {

             Class.forName("org.postgresql.Driver");
             java.sql.Connection conn = java.sql.DriverManager.getConnection(
                 "jdbc:postgresql://localhost:5432/dev", "dev", "devmm" );
             java.util.Enumeration<java.sql.Driver> drivers
                 = java.sql.DriverManager.getDrivers();
             while( drivers.hasMoreElements() ) {
                 java.sql.Driver d = drivers.nextElement();
                 System.out.println(
                     d.toString() + ": " +
                     d.getMajorVersion() + "." +
                     d.getMinorVersion() );
             }

             java.sql.PreparedStatement ps_ver = conn.prepareStatement(
                 "SELECT version()" );
             java.sql.ResultSet rs = ps_ver.executeQuery();
             while(rs.next()) System.out.println(rs.getString(1));


             java.sql.PreparedStatement ps_create = conn.prepareStatement(
                 "CREATE TABLE public.aab (" +
                 "ia integer[], " +
                 "sa smallint[], " +
                 "ba boolean[] )" );
             ps_create.executeUpdate();


             java.sql.PreparedStatement ps_insert = conn.prepareStatement(
                 "INSERT INTO public.aab (ia,sa,ba) VALUES (?,?,?)" );
             ps_insert.setArray( 1,
                 conn.createArrayOf( "int4", new Integer[] { 1,   2 } ) );
             ps_insert.setArray( 2,
                 conn.createArrayOf( "int2", new Short[]   { 100, 200 } ) );
             ps_insert.setArray( 3,
                 conn.createArrayOf( "bool", new Boolean[] {false,true} ) );
             ps_insert.executeUpdate();


             java.sql.PreparedStatement ps_select = conn.prepareStatement(
                 "SELECT ia,sa,ba FROM public.aab" );

             rs = ps_select.executeQuery();

             java.sql.Array jdbcArr;

             while(rs.next()) {

                 System.out.println("Integer[]");
                 jdbcArr = rs.getArray("ia");
                 Integer[] javaIntArr = (Integer[]) jdbcArr.getArray();
                 System.out.println(javaIntArr[0] + " " + javaIntArr[1]);
                 System.out.println(String.format(
                         "%s: %d %d",
                         jdbcArr.getBaseTypeName(),
                         java.sql.Types.INTEGER,
                         jdbcArr.getBaseType()));
                 System.out.println("END  Integer[]");


                 System.out.println("Boolean[]");
                 jdbcArr = rs.getArray("ba");
                 Boolean[] javaBooArr = (Boolean[]) jdbcArr.getArray();
                 System.out.println(javaBooArr[0] + " " + javaBooArr[1]);
                 System.out.println(String.format(
                         "%s: %d %d   %d",
                         jdbcArr.getBaseTypeName(),
                         java.sql.Types.BOOLEAN,
                         jdbcArr.getBaseType(),
                         java.sql.Types.BIT));
                 System.out.println("END  Boolean[]");


                 System.out.println("Short[]");
                 jdbcArr = rs.getArray("sa");
                 Short[] javaShoArr = (Short[]) jdbcArr.getArray();
                 System.out.println(javaShoArr[0] + " " + javaShoArr[1]);
                 System.out.println(String.format(
                         "%s: %d %d",
                         jdbcArr.getBaseTypeName(),
                         java.sql.Types.SMALLINT,
                         jdbcArr.getBaseType()));
                 System.out.println("END  Short[]");
	
	} catch( Exception e ) { ; }
	} // main
} // class

------------------------------------------------------------------


	Prints the following for me:


------------------------------------------------------------------

sun.jdbc.odbc.JdbcOdbcDriver@de6f34: 2.1
org.postgresql.Driver@47b480: 8.4
PostgreSQL 8.4.2 on x86_64-redhat-linux-gnu, compiled by GCC gcc (GCC) 4.1.2 
20071124 (Red Hat 4.1.2-42), 64-bit
Integer[]
1 2
int4: 4 4
END  Integer[]
Boolean[]
false true
bool: 16 -7   -7
END  Boolean[]
Short[]
java.lang.ClassCastException: [Ljava.lang.Integer; cannot be cast to 
[Ljava.lang.Short;
         at NewClass.main(NewClass.java:90)
[Ljava.lang.Integer; cannot be cast to [Ljava.lang.Short;

------------------------------------------------------------------









	Thanks for any help,


-- 
Saneesh Apte
510-642-5478
http://www.calccit.org
California Center for Innovative Transportation
University of California, Berkeley, MC3830
2105 Bancroft Way, Suite 300
Berkeley, CA 94720-3830
Phone: 510-642-4522
Fax: 510-642-0910



view thread (5+ messages)  latest in thread

Message-ID: <4C295503.5020506@calccit.org>
Permalink:  ../4C295503.5020506@calccit.org/
Also on:    postgresql.org/message-id/4C295503.5020506@calccit.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: san@calccit.org, pgsql-bugs@postgresql.org
  Subject: Re: JDBC: 2 bugs: Getting a smallint array actually gets an integer array and   return type of a boolean array is bit.
  In-Reply-To: <4C295503.5020506@calccit.org>
  References: <4C295503.5020506@calccit.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