java - Postgres SQL in clause and setArray() -


i using java 1.7 , jdbc 4 , postgres. trying use preparedstatement array fill sql in clause. but, sql generated seems have "{" , "}" in it. here code:

preparedstatement ptmt =        connection.preparestatement("select * foo id in (?)"); string[] values = new string[3]; values[0] = "a"; values[1] = "b"; values[2] = "c"; ptmt.setarray(1, connection.createarrayof("text", values)); 

the resulting sql looks this:

select * foo id in ('{"a","b","c"}') 

which, can't work. how should look:

select * foo id in ("a","b","c") 

or

select * foo id in ('a','b','c') 

what missing here?

when database field of type array, can use preparedstatement.setarray() send array query. but, in case, it's not array, rather variable no of arguments, , can't that. i.e.

preparedstatement ptmt =  connection.preparestatement("select * foo id in (?)"); 

can take 1 parameter. if want 3 parameters passed, have

preparedstatement ptmt =  connection.preparestatement("select * foo id in (?, ?, ?)"); 

and ptmt.setstring(n, "string") thrice.

if no of arguments aren't constant, construct query dynamically, although, loose efficiency.


Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -