Results 1 to 6 of 6
  1. #1
    AndreB's Avatar
    AndreB is offline Senior Member
    Join Date
    Dec 2009
    Location
    Stuttgart, Germany
    Posts
    114
    Rep Power
    0

    Default PreparedStatement and Arrays

    Okay, now this is tricky and google didnt help much (only to relize this is a difficult problem)

    I use postgresql so i will do it in that dislaect.
    Here the query:

    I have this simple query (in pgadmin3)
    Java Code:
    select '{1,3}'::int[]
    it returns an array and it is great ;-)

    but in my code i want to use a prepared statement
    Java Code:
    PreparedStatement s = connection.prepareStatement("select ?::int[]");
    now the question: which method should i use to set the value?
    if i e.g. use s.setString(1,"{1,3}") i have a cast exception.
    so which one is right?

    thanks in advance
    "There is no foolproof thing; fools are too smart."
    "Why can't you solve my Problem ?"

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Did you try the setArray method?

  3. #3
    AndreB's Avatar
    AndreB is offline Senior Member
    Join Date
    Dec 2009
    Location
    Stuttgart, Germany
    Posts
    114
    Rep Power
    0

    Default

    no, like i said the setArray method does not really work, as i have to implement the java.sql.Array myself.

    but i found the solution:
    Java Code:
    select string_to_array('1,2,3',',')
    this converts a string to an array and therefore all i have to to do is
    Java Code:
    PreparedStatement s = connection.prepareStatement("select string_to_array(?,' '));
    "There is no foolproof thing; fools are too smart."
    "Why can't you solve my Problem ?"

  4. #4
    AndreB's Avatar
    AndreB is offline Senior Member
    Join Date
    Dec 2009
    Location
    Stuttgart, Germany
    Posts
    114
    Rep Power
    0

    Default

    btw: how do i mark as solved ?
    "There is no foolproof thing; fools are too smart."
    "Why can't you solve my Problem ?"

  5. #5
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Quote Originally Posted by AndreB View Post
    no, like i said the setArray method does not really work, ...
    Where did you mention that you tried setArray and it didn't work?

  6. #6
    AndreB's Avatar
    AndreB is offline Senior Member
    Join Date
    Dec 2009
    Location
    Stuttgart, Germany
    Posts
    114
    Rep Power
    0

    Default

    sry, my bad :-) forget to mention. but many google hits said that setArray is optional and is only possible for oracle db without own implementation of the Array interface.
    "There is no foolproof thing; fools are too smart."
    "Why can't you solve my Problem ?"

Similar Threads

  1. Regarding PreparedStatement
    By adeeb in forum JDBC
    Replies: 0
    Last Post: 06-09-2008, 10:07 PM
  2. Using PreparedStatement
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-22-2007, 12:24 PM
  3. PreparedStatement
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-05-2007, 04:56 PM
  4. Help me. PreparedStatement
    By Felissa in forum JDBC
    Replies: 2
    Last Post: 06-28-2007, 06:03 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •