Results 1 to 2 of 2
  1. #1
    Shryans is offline Member
    Join Date
    Mar 2011
    Posts
    2
    Rep Power
    0

    Default using strings in select statement

    Hi I am a relatively new user of JDBC. I have following code fragment in Postgres:

    st1=con.createStatement();
    String query="SELECT * FROM table WHERE semantics=?";
    PreparedStatement prest = con.prepareStatement(query);
    prest.setString(1, str); //str is a string variable
    ResultSet rs= st1.executeQuery(query);
    if(rs==null) //null implies no entry found that matches the query
    {
    if(st1.executeUpdate("INSERT INTO staypoint...")!=0)
    {
    ....
    System.out.println("Values added successfully");
    }
    else
    {
    ...
    System.out.println("Values could not be added");
    }
    }
    else
    {
    ...
    System.out.println("Values updated");
    }
    ...

    on running this piece of code I get following error:
    Error!! org.postgresql.util.PSQLException: ERROR: operator does not exist: text =?
    Hint: No operator matches the given name and argument type(s). You might need to add explicit type casts.
    Position: 40

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: using strings in select statement

    Java Code:
    prest.setString(1, str); //str is a string variable
    ResultSet rs= st1.executeQuery(query);
    You create a PreparedStatement, then rather than using the object use the same query in a Statement to try and get a ResultSet. Use the PreparedStatement to retrieve the ResultSet

Similar Threads

  1. if statement with strings not working... again
    By hardcorebadger in forum New To Java
    Replies: 4
    Last Post: 01-11-2011, 06:02 AM
  2. Replies: 0
    Last Post: 03-15-2009, 11:53 PM
  3. MySQL/JDBC Prepared Statement Select query
    By thelinuxguy in forum Advanced Java
    Replies: 4
    Last Post: 02-12-2009, 05:29 PM
  4. How to retain value in struts 2 using <s:select></s:select> tag
    By SaiPrasad@Sella in forum Web Frameworks
    Replies: 0
    Last Post: 02-09-2009, 07:23 AM
  5. Replies: 2
    Last Post: 12-06-2008, 06:54 PM

Tags for this Thread

Posting Permissions

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