Results 1 to 2 of 2
  1. #1
    Join Date
    Oct 2002
    Posts
    4

    Unhappy Unanswered: need urgent help with PreparedStatement

    i use db2 as database, jdbc 2.0 driver and jsp.
    i have a prepared statement which looks like this :

    query = "select id, name from t_department where id in (?) order by name";

    i have a StringBuffer (selectedIDs) which consists of comma sepperated integers like :
    216, 220, 230, 323, 432

    it is not a string so the statement below gives an error :

    pst.setString(1, selectedIDs.toString());

    using a Statement instead would solve the problem :
    query = "select id, name from t_department where id in ("+selectedIDs.toString()+") order by name";

    but wouldn't that compile a new statement every time, because the id-list (the comma sepperated values) changes with every request.

    how can i solve this problem by using preparedstatement ?
    any help will be appreciated

    Mehmet Gunacti
    mserkan@gmx.net

  2. #2
    Join Date
    Jan 2003
    Posts
    13
    You can use the PreparedStatemet if you
    always use the same number of parameters.
    For instance if you know you always have 4 parmeters:

    query = "select id, name from t_department where id in (?, ?, ?, ?) order by name";
    pst = ...
    pst.setInt(1, 216);
    pst.setInt(2, 220);
    pst.setInt(3, 323);
    pst.setInt(4, 432);

Posting Permissions

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