Skip site navigation (1) Skip section navigation (2)

Re: Passing Array from Java to Postgres

From: Alexander Panzhin <jalexoids(at)gmail(dot)com>
To: pgsql-jdbc(at)postgresql(dot)org
Subject: Re: Passing Array from Java to Postgres
Date: 2008-11-04 16:38:21
Message-ID: 49107A7D.3050206@gmail.com (view raw or flat)
Thread:
Lists: pgsql-jdbc
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
>
>   

In response to

Responses

pgsql-jdbc by date

Next:From: gcjDate: 2008-11-04 21:49:46
Subject: Re: Retrieve auto-generated key.
Previous:From: Albe LaurenzDate: 2008-11-04 16:01:56
Subject: Re: JNDI Datasource in tomcat

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group