pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Bug report and patch: PreparedStatement.setObject(int, Object) does not infer java.lang.Byte
2+ messages / 2 participants
[nested] [flat]

* Bug report and patch: PreparedStatement.setObject(int, Object) does not infer java.lang.Byte
@ 2007-06-21 12:42 Boom Roos <boom.roos@gmail.com>
  2007-06-22 21:38 ` Re: Bug report and patch: PreparedStatement.setObject(int, Object) does not infer java.lang.Byte Kris Jurka <books@ejurka.com>
  0 siblings, 1 reply; 2+ messages in thread

From: Boom Roos @ 2007-06-21 12:42 UTC (permalink / raw)
  To: pgsql-jdbc

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);
^ permalink raw reply [nested|flat] 2+ messages in thread

* Re: Bug report and patch: PreparedStatement.setObject(int, Object) does not infer java.lang.Byte
  2007-06-21 12:42 Bug report and patch: PreparedStatement.setObject(int, Object) does not infer java.lang.Byte Boom Roos <boom.roos@gmail.com>
@ 2007-06-22 21:38 ` Kris Jurka <books@ejurka.com>
  0 siblings, 0 replies; 2+ messages in thread

From: Kris Jurka @ 2007-06-22 21:38 UTC (permalink / raw)
  To: Boom Roos <boom.roos@gmail.com>; +Cc: pgsql-jdbc



On Thu, 21 Jun 2007, Boom Roos wrote:

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

Fix applied to 8.0, 8.1, 8.2, and HEAD CVS and will be in the next 
release.  Thanks.

Kris Jurka




^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2007-06-22 21:38 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2007-06-21 12:42 Bug report and patch: PreparedStatement.setObject(int, Object) does not infer java.lang.Byte Boom Roos <boom.roos@gmail.com>
2007-06-22 21:38 ` Kris Jurka <books@ejurka.com>

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