Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Prepared Statements with unknown number of inputs

  • 04-08-2006 11:40am
    #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