Thread: Bug report and patch: PreparedStatement.setObject(int, Object) does not infer java.lang.Byte
Bug report and patch: PreparedStatement.setObject(int, Object) does not infer java.lang.Byte
From
"Boom Roos"
Date:
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
Re: Bug report and patch: PreparedStatement.setObject(int, Object) does not infer java.lang.Byte
From
Kris Jurka
Date:
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