Unix Technical Forum

IN clause with PreparedStatement

This is a discussion on IN clause with PreparedStatement within the pgsql Interfaces jdbc forums, part of the PostgreSQL category; --> Hello, I want to submit a query like SELECT * FROM table1 WHERE id IN (1, 2, 3); whereas ...


Go Back   Unix Technical Forum > Database Server Software > PostgreSQL > pgsql Interfaces jdbc

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-16-2008, 12:49 AM
=?ISO-8859-15?Q?Ingmar_L=F6tzsch?=
 
Posts: n/a
Default IN clause with PreparedStatement

Hello,

I want to submit a query like

SELECT *
FROM table1
WHERE id IN (1, 2, 3);

whereas the list 1, 2, 3 must be created dynamically. The goal is to
avoid n queries for single objects instead of one query. I can
concatenate the statement and use Statement. But I can't figure out, how
to use PreperedStatement. Can you help me?

Thank you, Ingmar


---------------------------(end of broadcast)---------------------------
TIP 3: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faq

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-16-2008, 12:49 AM
Kris Jurka
 
Posts: n/a
Default Re: IN clause with PreparedStatement



On Wed, 18 Jul 2007, Ingmar Lötzsch wrote:

> SELECT *
> FROM table1
> WHERE id IN (1, 2, 3);
>
> whereas the list 1, 2, 3 must be created dynamically. The goal is to
> avoid n queries for single objects instead of one query. I can
> concatenate the statement and use Statement. But I can't figure out, how
> to use PreperedStatement. Can you help me?
>


This identical thread concluded with a perl example that is relevant.

http://archives.postgresql.org/pgsql...6/msg00015.php

Kris Jurka

---------------------------(end of broadcast)---------------------------
TIP 7: You can help support the PostgreSQL project by donating at

http://www.postgresql.org/about/donate

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-16-2008, 12:49 AM
=?ISO-8859-15?Q?Ingmar_L=F6tzsch?=
 
Posts: n/a
Default Re: IN clause with PreparedStatement

>> SELECT *
>> FROM table1
>> WHERE id IN (1, 2, 3);


> This identical thread concluded with a perl example that is relevant.
>
> http://archives.postgresql.org/pgsql...6/msg00015.php


Thank you very much.

They suggest two approaches

1.
int[] num = new int[]{1,2,3};
StringBuffer sb = new StringBuffer();
sb.append("SELECT * FROM items WHERE id IN(").append(generateCSV("?",
num.length)).append(")");
for (int i = 0; i < num.length; i++) {
ps.setInt(i, num[i]);
}
ps.executeQuery();

2.
$sth = $dbh->prepare("select * from foo where bar =ANY(?::int[])")
$sth->execute('{' . join(@array, ',') . '}');

What do you think about combining the ANY key word with the
PreparedStatement the following way?

class IntArray

package test;

import java.sql.Array;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Map;

public class IntArray implements Array
{
private int[] array;

public IntArray(int[] array)
{
if (array == null)
{
throw new IllegalArgumentException("parameter array should not be null");
}
this.array = array;
}

public Object getArray() throws SQLException
{
return null;
}

public Object getArray(Map map) throws SQLException
{
return null;
}

public Object getArray(long index, int count) throws SQLException
{
return null;
}

public Object getArray(long index, int count, Map map) throws SQLException
{
return null;
}

public int getBaseType() throws SQLException
{
return Types.INTEGER;
}

/**
* This method is called by driver ver. 8 but not by ver. 7.
*/
public String getBaseTypeName() throws SQLException
{
return "int4";
}

public ResultSet getResultSet() throws SQLException
{
return null;
}

public ResultSet getResultSet(long index, int count) throws SQLException
{
return null;
}

public ResultSet getResultSet(long index, int count, Map map) throws
SQLException
{
return null;
}

public ResultSet getResultSet(Map map) throws SQLException
{
return null;
}

/**
* This method is called by both drivers ver. 8 and 7.
*/
public String toString()
{
String result = "{";
for (int i = 0; i < this.array.length; ++i)
{
if (i > 0)
{
result += ",";
}
result += this.array[i];
}
result += "}";
return result;
}
}

class ArrayTest

package test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class ArrayTest
{
public static void main(String[] args)
{
int[] array = new int[]{1, 2, 3};
IntArray intArray = new IntArray(array);
try
{
Class.forName("org.postgresql.Driver");
String url = "jdbcostgresql://localhost/test";
Connection con = DriverManager.getConnection(url, "postgres", "admin");
String sql = "SELECT *\n" +
"FROM table1\n" +
"WHERE id = ANY (?::int[]);";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setArray(1, intArray);
ResultSet rs = pstmt.executeQuery();
while (rs.next())
{
int id = rs.getInt("id");
String str1 = rs.getString("str1");
System.out.println(id + ", " + str1);
}
rs.close();
pstmt.close();
con.close();
}
catch (Exception e)
{
e.printStackTrace(System.out);
}
}
}

This works with postgresql-8.1-404.jdbc3.jar and pg74.216.jdbc3.jar. I
don't know how to implement the getArray() and getResultSet() methods.
Both drivers only call the toString() method and the driver version 8
additionaly calls getBaseTypeName().

Thank you


---------------------------(end of broadcast)---------------------------
TIP 6: explain analyze is your friend

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 08:36 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com