Results 1 to 7 of 7
  1. #1
    flok is offline Member
    Join Date
    Sep 2009
    Location
    Gouda, the Netherlands
    Posts
    24
    Rep Power
    0

    Default SELECT * FROM bla WHERE username IN (?, ?, ...) using PreparedStatement

    Hi,

    I have the following query:
    Java Code:
    SELECT * FROM bla WHERE username IN ('username1', 'username2', ...)
    So I'm trying to select all rows where username is 'username1', 'username2' and some more. Now I would like to do this using a PreparedStatement. But how do I set multiple fields? as the part 'IN (...)' has a flexible number of elements.
    How do I do this?

  2. #2
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    843
    Rep Power
    6

    Default

    could you please explain the question clearly to give the solution.
    Ramya:cool:

  3. #3
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    7

    Default

    If you know the number of wildcards (?) you can just set the values in a loop.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  4. #4
    flok is offline Member
    Join Date
    Sep 2009
    Location
    Gouda, the Netherlands
    Posts
    24
    Rep Power
    0

    Default

    Quote Originally Posted by PhHein View Post
    If you know the number of wildcards (?) you can just set the values in a loop.
    Yes, but how do I tell 'PreparedStatement' that the number of variables in the 'WHERE username IN (...)' is flexible?

  5. #5
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    7

    Default

    In a loop:
    Java Code:
    String sql = "SELECT * FROM bla WHERE username IN (";
    StringBuffer bf = new StringBuffer(sql);
    int numberOfArgs = 10;
    for(int i = 0; i < numberOfArgs; i++){
    	bf.append("?");
    	if(i< numberOfArgs-1){
    		bf.append(", ");
    	}
    }
    bf.append(")");
    sql = bf.toString();
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  6. #6
    flok is offline Member
    Join Date
    Sep 2009
    Location
    Gouda, the Netherlands
    Posts
    24
    Rep Power
    0

    Default

    Quote Originally Posted by PhHein View Post
    In a loop:
    Java Code:
    String sql = "SELECT * FROM bla WHERE username IN (";
    ...
    sql = bf.toString();
    Hmmm ok.
    I was hoping maybe there was some kind of IN (...) and that you could do something like setString(x, stringarray).

  7. #7
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    7

    Default

    Nope, that would be in the next loop
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

Similar Threads

  1. Asking for a username with conditions
    By ScentOfAWookie in forum New To Java
    Replies: 2
    Last Post: 03-20-2009, 06:53 AM
  2. 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, 08:23 AM
  3. password username and databases
    By chalo in forum JCreator
    Replies: 0
    Last Post: 12-02-2008, 09:11 AM
  4. username password verification
    By bheezee in forum JDBC
    Replies: 0
    Last Post: 11-25-2008, 07:55 PM
  5. Help, created a username and password box
    By cachi in forum AWT / Swing
    Replies: 1
    Last Post: 08-07-2007, 05:21 AM

Posting Permissions

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