Barry Lind wrote:
> Scott,
>
> If you use ps.setInt(1,42) does it work? I want to isolate the problem.
> In just looking at the code, it seems that the setInt() and setObject()
> methods do the same thing.
Ahh, I'm sorry. setObject(1, new Object(42), Types.INTEGER) doesn't do
it after all. I translated inappropriately when I was writing the email.
setObject(1, null, Types.INTEGER) is the problem. Test function and Java
program attached to reproduce.
Scott
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.DriverManager;
import java.sql.Types;
import java.sql.SQLException;
import org.postgresql.PGStatement;
public class PreparedCastTest {
public static void main(String[] args) {
Connection c = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
c = DriverManager.getConnection(args[0], args[1], args[2]);
ps = c.prepareStatement("select foo(?)");
( (PGStatement) ps ).setUseServerPrepare(true);
//ps.setInt(1, 42);
//ps.setObject(1, new Integer(42), Types.INTEGER);
ps.setObject(1, null, Types.INTEGER);
rs = ps.executeQuery();
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
} finally {
if (rs != null) { try { rs.close(); } catch (Throwable t) {} }
if (ps != null) { try { ps.close(); } catch (Throwable t) {} }
if (c != null) { try { c .close(); } catch (Throwable t) {} }
}
}
}
create or replace function foo(integer) returns text as '
declare
theint alias for $1;
begin
return theint::text;
end;' language 'plpgsql';