BUG #3751: Conversion error using PreparedStatement.setObject() - Mailing list pgsql-bugs

From Lance Andersen
Subject BUG #3751: Conversion error using PreparedStatement.setObject()
Date
Msg-id 200711152201.lAFM1X1M080066@wwwmaster.postgresql.org
Whole thread Raw
Responses Re: BUG #3751: Conversion error using PreparedStatement.setObject()
List pgsql-bugs
The following bug has been logged online:

Bug reference:      3751
Logged by:          Lance Andersen
Email address:      lance.andersen@sun.com
PostgreSQL version: 8.2.x
Operating system:   N/A
Description:        Conversion error using PreparedStatement.setObject()
Details:

A PreparedStatement.setObject(1, "1", Types.Bit) will fail with the
following Exception:

org.postgresql.util.PSQLException: ERROR: column "max_val" is of type bit
but expression is of type integer
        at
org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorI
mpl.java:1548)
        at
org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.ja
va:1316)
        at
org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:191)

        at
org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.j
ava:452)
        at
org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2St
atement.java:337)
        at
org.postgresql.jdbc2.AbstractJdbc2Statement.executeUpdate(AbstractJdbc2State
ment.java:283)
        at com.sun.jdbc.samples.BitTest.testSetObject48(BitTest.java:108)
        at com.sun.jdbc.samples.BitTest.runTest(BitTest.java:61)
        at com.sun.jdbc.samples.BitTest.main(BitTest.java:16)

Here is a simple repro:

package com.sun.jdbc.samples;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.math.BigDecimal;
import java.math.*;
import java.util.StringTokenizer;


public class BitTest {
    List<Drivers>  drivers;

    public static void main(String args[]){
        BitTest test = new BitTest();
        test.runTest();

    }

    public BitTest() {
        drivers = new ArrayList<Drivers>();



        // Postgresql Drivers
        drivers.add(new Drivers("org.postgresql.Driver",
"jdbc:postgresql://jsepc18.east:5432/CTS5", "cts1", "cts1" ));



    }

    public void runTest() {
        Connection conn = null;

        for(Drivers driver : drivers){
            try {
                Class.forName(driver.getDriver());
                conn = DriverManager.getConnection(driver.getUrl(),
driver.getUser(), driver.getPassword());
                DatabaseMetaData dbmeta = conn.getMetaData();
                dumpVersionInfo(dbmeta);
                testSetObject48(conn);
                conn.close();
            }catch( Exception e ) {
                e.printStackTrace();
            }

        }



    }


    public static void dumpVersionInfo(DatabaseMetaData dbmeta) {

        try {

System.out.println("\n***********************************************");

            String productName = dbmeta.getDatabaseProductName();
            String productVersion = dbmeta.getDatabaseProductVersion();
            String driverName = dbmeta.getDriverName();
            String driverVersion = dbmeta.getDriverVersion();

            System.out.println("productName: " + productName);
            System.out.println("productVersion: " + productVersion);
            System.out.println("driverName: " + driverName);
            System.out.println("Version: " + driverVersion);


System.out.println("***********************************************");

        } catch( Exception e ) {
            e.printStackTrace();
        }
    }

    private static void testSetObject48(Connection conn) {
        ResultSet rs;
        String Min_Val_Query= "SELECT MIN_VAL from Bit_Tab";
        String sMaxBooleanVal = "1";
        //sMaxBooleanVal = "true";
        Boolean bool = Boolean.valueOf("true");
        String Min_Insert= "insert into Bit_Tab values(1,0,null)";
        //System.out.println("Value to insert=" +
extractVal(Min_Insert,1));
        CallableStatement cstmt;
        try {
            Statement stmt = conn.createStatement();
            stmt.executeUpdate("delete from Bit_Tab");
            stmt.executeUpdate(Min_Insert);
            cstmt = conn.prepareCall("{call Bit_In_Min(?)}");
            cstmt.setObject(1,sMaxBooleanVal,java.sql.Types.BIT);
            //cstmt.setObject(1,bool,java.sql.Types.BIT);
            cstmt.executeUpdate();

            rs = stmt.executeQuery(Min_Val_Query);
            while(rs.next()){
                System.out.println("Expected value=" + sMaxBooleanVal + ",
Returned value as Boolean= " +rs.getBoolean(1)
                + ", as String=" + rs.getString(1));
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }


    }



}

pgsql-bugs by date:

Previous
From: Tom Lane
Date:
Subject: Re: Strange problem with an index.
Next
From: Kris Jurka
Date:
Subject: Re: BUG #3751: Conversion error using PreparedStatement.setObject()