Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Prepared Statements with unknown number of inputs

  • 04-08-2006 12:40PM
    #1
    Closed Accounts Posts: 4


    If I have this query = "select * from cars where make = ?"

    I can do this in a prepared statement
    PreparedStatement p = con.preparedS..(Query)
    p.setString(1,make);
    

    Any idea how to do it if I have multiple makes ?

    "select * from cars where make = ? or make = ? or make =? or ..unknown number"

    I was thinking of counting the number of inputs and having an array of query strings
    String[] queries = {
    "",
    "select * from cars where make = ?",
    "select * from cars where make = ? or make = ?",
    "select * from cars where make = ? or make = ? or make = ?",
    "select * from cars where make = ? or make = ? or make = ? or make = ?",
    etc};
    

    then using
    String[] inputs = inputs in array;
    int input = inputs.length;
    PreparedStatement p = con.preparedS..(queries[input]);
    for(int i=1;i<=inputs;i++)
    	p.setString(i,inputs[i]);
    


Comments

  • Closed Accounts Posts: 503 ✭✭✭OMcGovern


    You might be able to use this instead...

    select * from cars where make in ( ? )

    And the ? is a comma-separated list of values.
    Eg. 'ford', 'nissan', 'mazda'

    regards,
    Owen


  • Closed Accounts Posts: 4 hothouse


    OMcGovern wrote:
    You might be able to use this instead...

    select * from cars where make in ( ? )

    And the ? is a comma-separated list of values.
    Eg. 'ford', 'nissan', 'mazda'

    regards,
    Owen

    Works great, much more simple

    thanks
    Owen


Advertisement