Passing Array from Java to Postgres

Поиск
Список
Период
Сортировка
Искать

Passing Array from Java to Postgres

От:
lathasuresh.s@gmail.com
Дата:
Hi,
    I need to pass array from Java to postgres DB.Tried the following
code given in net,but it   doesnt work,please help me to solve this.

  Getting no exceptions but program execution skips when it reaches
the following code
   cstmt.setArray(2, connection.createArrayOf("int4", intarr ));

I have also copied the following code for reference,

           DBConnection dbconn=new DBConnection();
            Connection connection=dbconn.getDBConnection();
            CallableStatement  cstmt = connection.prepareCall("{?=
call testit(?)}");
            cstmt.registerOutParameter(1, Types.INTEGER);
            try{
                    cstmt.setArray(2, connection.createArrayOf("int4",
intarr ));
            }catch(Exception e)    {
            e.printStackTrace();
            }
            cstmt.execute();
            result = cstmt.getInt(1);

Regards,
Latha S

Re: Passing Array from Java to Postgres

От:
Alexander Panzhin <jalexoids@gmail.com>
Дата:
Вложения:
Tried it on PostgreSQL 8.2, PL/pgSQL function.

        Array sqlArray = cn.createArrayOf("int4", new Integer[] { 47,46 });

        CallableStatement cstmt = cn.prepareCall("{? = call testit(?)}");
        cstmt.registerOutParameter(1, Types.INTEGER);
        try {
            cstmt.setArray(2, sqlArray);
        } catch (Exception e) {
            e.printStackTrace();
        }
        cstmt.execute();
        int result = cstmt.getInt(1);
        System.out.println("Result: "+result);


CREATE FUNCTION testit(number integer[]) RETURNS integer
    AS $$BEGIN
   RETURN number[1];
END;$$
    LANGUAGE plpgsql;


Got: "Result: 47"


> Hi,
>     I need to pass array from Java to postgres DB.Tried the following
> code given in net,but it   doesnt work,please help me to solve this.
>
>   Getting no exceptions but program execution skips when it reaches
> the following code
>    cstmt.setArray(2, connection.createArrayOf("int4", intarr ));
>
> I have also copied the following code for reference,
>
>            DBConnection dbconn=new DBConnection();
>             Connection connection=dbconn.getDBConnection();
>             CallableStatement  cstmt = connection.prepareCall("{?=
> call testit(?)}");
>             cstmt.registerOutParameter(1, Types.INTEGER);
>             try{
>                     cstmt.setArray(2, connection.createArrayOf("int4",
> intarr ));
>             }catch(Exception e)    {
>             e.printStackTrace();
>             }
>             cstmt.execute();
>             result = cstmt.getInt(1);
>
> Regards,
> Latha S
>
>   

Re: Passing Array from Java to Postgres

От:
Alexander Panzhin <jalexoids@gmail.com>
Дата:
Вложения:
Witch driver version are you using? I was testing with 
postgresql-8.3-603.jdbc4.jar.
Do you use connection pooling? This might not even be a driver problem, 
since connection pools don't give you the underlying connection created 
by the native driver. If the connection pool facade connection does not 
implement createArrayOf, it will throw AbstractMethodException.
> Hi Alexander ,
>      I tried it,but still get the same problem.(program execution
> skips when it reaches the following code)
> sqlArray = connection.createArrayOf("int4", new Integer[] { 47,46 });
>
> Using GlassFish server and Netbeans IDE.
> DB-Postgresql 8.2
>
> Also pasted DB connection info.
> String driver="org.postgresql.Driver";
> String URL="jdbc:postgresql://localhost:5432/dbhmisdemo_sep20f";
>
> Also pasted code and the output in console
>      System.out.println("\n inside executeProc");
>             DBConnection dbconn=new DBConnection();
>             Connection connection=dbconn.getDBConnection();
>             System.out.println("\n before creating sqlArray ");
>             Array sqlArray = null;
>             sqlArray = connection.createArrayOf("int4", new Integer[]
> { 47,46 });
>             System.out.println("\n After creating sqlArray");
>             CallableStatement  cstmt = connection.prepareCall("{?=
> call testit(?)}");
>             System.out.println("\n after CallableStatement");
>             cstmt.registerOutParameter(1, Types.INTEGER);
>             System.out.println("\n before SetArray");
>             try{
>                     cstmt.setArray(2, sqlArray);
>             }catch(Exception e)    {
>             e.printStackTrace();
>             }
>             System.out.println("\n After Array");
>             System.out.println("\n before execute");
>             cstmt.execute();
>             System.out.println("\n after execute");
>             result = cstmt.getInt(1);
>             System.out.println("\nThe result is " + result);
>
> Server Log
>  inside executeProc
>  before creating sqlArray
>
> Thanks in advance
>
>
>
>
>
> On Nov 4, 9:38 pm, jalexo...@gmail.com (Alexander Panzhin) wrote:
>   
>> Tried it on PostgreSQL 8.2, PL/pgSQL function.
>>
>>         Array sqlArray = cn.createArrayOf("int4", new Integer[] { 47,46 });
>>
>>         CallableStatement cstmt = cn.prepareCall("{? = call testit(?)}");
>>         cstmt.registerOutParameter(1, Types.INTEGER);
>>         try {
>>             cstmt.setArray(2, sqlArray);
>>         } catch (Exception e) {
>>             e.printStackTrace();
>>         }
>>         cstmt.execute();
>>         int result = cstmt.getInt(1);
>>         System.out.println("Result: "+result);
>>
>> CREATE FUNCTION testit(number integer[]) RETURNS integer
>>     AS $$BEGIN
>>    RETURN number[1];
>> END;$$
>>     LANGUAGE plpgsql;
>>
>> Got: "Result: 47"
>>
>>
>>
>>     
>>> Hi,
>>>     I need to pass array from Java to postgres DB.Tried the following
>>> code given in net,but it   doesnt work,please help me to solve this.
>>>       
>>>   Getting no exceptions but program execution skips when it reaches
>>> the following code
>>>    cstmt.setArray(2, connection.createArrayOf("int4", intarr ));
>>>       
>>> I have also copied the following code for reference,
>>>       
>>>            DBConnection dbconn=new DBConnection();
>>>             Connection connection=dbconn.getDBConnection();
>>>             CallableStatement  cstmt = connection.prepareCall("{?=
>>> call testit(?)}");
>>>             cstmt.registerOutParameter(1, Types.INTEGER);
>>>             try{
>>>                     cstmt.setArray(2, connection.createArrayOf("int4",
>>> intarr ));
>>>             }catch(Exception e)    {
>>>             e.printStackTrace();
>>>             }
>>>             cstmt.execute();
>>>             result = cstmt.getInt(1);
>>>       
>>> Regards,
>>> Latha S
>>>       
>>
>>  smime.p7s
>> 4KViewDownload- Hide quoted text -
>>
>> - Show quoted text -
>>     
>
>
>   

FAQ