pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Boom Roos <boom.roos@gmail.com>
To: pgsql-jdbc@postgresql.org
Subject: Bug report and patch: PreparedStatement.setObject(int, Object) does not infer java.lang.Byte
Date: Thu, 21 Jun 2007 14:42:41 +0200
Message-ID: <123792f50706210542j61db2002jca2cc9dbb71d7a4b@mail.gmail.com> (raw)

Hi,

We are calling the setObject method on the prepared statement with
several different java.lang object wrappers for the different
primitive types (Integer, Double, Byte, etc) and are relying on the
setObject method to interpret the value according to the correct type.

This works on all types and on other jdbc drivers (to other
databases), except for java.lang.Byte on  the postgres database.

I think that setObject(int, Object) should call setByte(n, byteval)
when called with a Byte object.

JDBC driver: latest from CVS HEAD (2007-06-21)
Server version: all (driver problem, not server problem)

Sample code:
//// start of SetByteTest.java //////
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;

public class SetByteTest {
	public static void main(String[] args) {
		try {
			// configuration
			String url = "jdbc:postgresql://localhost/test";
			String driverClass = "org.postgresql.Driver";
			String user = "test";
			String pass = "password";
						// login
			Class.forName(driverClass);
			Driver driver = DriverManager.getDriver(url);
			Properties props = new Properties();
			props.put("user", user);
			props.put("password", pass);
			Connection c = driver.connect(url, props);
			System.out.println("login OK");
			PreparedStatement ps = c.prepareStatement("select ?");
//			ps.setObject(1, new Integer(1));
			ps.setObject(1, Byte.valueOf((byte) 1));
			ResultSet rs = ps.executeQuery();
			if (!rs.next()) System.out.println("NO RESULT?");
			else System.out.println(rs.getByte(1));
			rs.close();
			ps.close();
			c.close();
		} catch (Exception e) {
			e.printStackTrace();
		}	
		}
}
//// end of SetByteTest.java //////

This fails with an exception :

org.postgresql.util.PSQLException: Can't infer the SQL type to use for
an instance of java.lang.Byte. Use setObject() with an explicit Types
value to specify the type to use.
	at org.postgresql.jdbc2.AbstractJdbc2Statement.setObject(AbstractJdbc2Statement.java:1732)
	at SetByteTest.main(SetByteTest.java:27)


I have prepared a patch (see attached) for the statement class
(AbstractJdbcStatement).
I also include a patch for the unit test case (TypesTest) to show this problem.

Please advice

Rob

Attachments:

AbstractJdbc2Statement.java.diff.txt (text/plain, 898B) download Index: org/postgresql/jdbc2/AbstractJdbc2Statement.java =================================================================== RCS file: /usr/local/cvsroot/pgjdbc/pgjdbc/org/postgresql/jdbc2/AbstractJdbc2Statement.java,v retrieving revision 1.100 diff -c -r1.100 AbstractJdbc2Statement.java *** org/postgresql/jdbc2/AbstractJdbc2Statement.java 7 May 2007 13:10:34 -0000 1.100 --- org/postgresql/jdbc2/AbstractJdbc2Statement.java 21 Jun 2007 12:16:44 -0000 *************** *** 1721,1726 **** --- 1721,1728 ---- setTimestamp(parameterIndex, (Timestamp)x); else if (x instanceof Boolean) setBoolean(parameterIndex, ((Boolean)x).booleanValue()); + else if (x instanceof Byte) + setByte(parameterIndex, ((Byte)x).byteValue()); else if (x instanceof Blob) setBlob(parameterIndex, (Blob)x); else if (x instanceof Clob)
TypesTest.java.diff.txt (text/plain, 1.2K) download Index: org/postgresql/test/jdbc3/TypesTest.java =================================================================== RCS file: /usr/local/cvsroot/pgjdbc/pgjdbc/org/postgresql/test/jdbc3/TypesTest.java,v retrieving revision 1.9 diff -c -r1.9 TypesTest.java *** org/postgresql/test/jdbc3/TypesTest.java 15 May 2006 09:35:57 -0000 1.9 --- org/postgresql/test/jdbc3/TypesTest.java 21 Jun 2007 12:17:10 -0000 *************** *** 64,69 **** --- 64,83 ---- } } + public void testPreparedByte() throws SQLException { + PreparedStatement pstmt = _conn.prepareStatement("SELECT ?,?"); + pstmt.setByte(1, (byte)1); + pstmt.setObject(2, Byte.valueOf((byte)2)); + ResultSet rs = pstmt.executeQuery(); + assertTrue(rs.next()); + assertEquals((byte)1, rs.getByte(1)); + assertFalse(rs.wasNull()); + assertEquals((byte)2, rs.getByte(2)); + assertFalse(rs.wasNull()); + rs.close(); + pstmt.close(); + } + public void testCallableBoolean() throws SQLException { CallableStatement cs = _conn.prepareCall("{? = call return_bool(?)}"); cs.registerOutParameter(1, Types.BOOLEAN);

view thread (2+ messages)  latest in thread

Message-ID: <123792f50706210542j61db2002jca2cc9dbb71d7a4b@mail.gmail.com>
Permalink:  ../123792f50706210542j61db2002jca2cc9dbb71d7a4b@mail.gmail.com/
Also on:    postgresql.org/message-id/123792f50706210542j61db2002jca2cc9dbb71d7a4b@mail.gmail.com

 · 

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: boom.roos@gmail.com
  Subject: Re: Bug report and patch: PreparedStatement.setObject(int, Object) does not infer java.lang.Byte
  In-Reply-To: <123792f50706210542j61db2002jca2cc9dbb71d7a4b@mail.gmail.com>
  References: <123792f50706210542j61db2002jca2cc9dbb71d7a4b@mail.gmail.com>

* 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