Bug report and patch: PreparedStatement.setObject(int, Object) does not infer java.lang.Byte

From: "Boom Roos" <boom(dot)roos(at)gmail(dot)com>
To: pgsql-jdbc(at)postgresql(dot)org
Subject: Bug report and patch: PreparedStatement.setObject(int, Object) does not infer java.lang.Byte
Date: 2007-06-21 12:42:41
Message-ID: 123792f50706210542j61db2002jca2cc9dbb71d7a4b@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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

Attachment Content-Type Size
AbstractJdbc2Statement.java.diff.txt text/plain 897 bytes
TypesTest.java.diff.txt text/plain 1.2 KB

Responses

Browse pgsql-jdbc by date

  From Date Subject
Next Message EBIHARA, Yuichiro 2007-06-22 04:09:27 Re: standard LOB support
Previous Message EBIHARA, Yuichiro 2007-06-21 05:21:03 Re: standard LOB support