Results 1 to 3 of 3

Thread: JdbcTemplate with expr IN (value,...)

  1. #1
    Join Date
    May 2008
    Posts
    5

    Default JdbcTemplate with expr IN (value,...)

    Hi all

    I'm using the JdbCTemplate method

    Code:
    query(String sql, Object[] args, RowMapper rowMapper)
    to return a List of objects from a MySQL database. I'd like to pass a set of strings to the query a.k.a.

    Code:
    SELECT * FROM myTable WHERE item IN ('one','two','three'...etc)
    but can't find anything about how to do this with Spring. The list of strings is provided at runtime based on selections from a user, so there's no knowing how many variables will be in this list.

    My only solution so far is to build the query in a string buffer and manually inject the search parameters, but it means I'll have to manually trap SQL injections and just doesn't look very nice!

    Is it possible to replace the list with a ? and bind the value(s) at runtime? Any help or pointers much appreciated!

    Thanks
    --Jon

  2. #2
    Join Date
    Aug 2004
    Posts
    1,104

    Default

    You can if you use the named parameter support in the SmpleJdbcTemplate or the NamedParameterJdbcTemplate. See this post for more info - http://forum.springframework.org/sho...13&postcount=5
    Thomas Risberg
    SpringSource by Pivotal
    http://www.springsource.org

  3. #3
    Join Date
    May 2008
    Posts
    5

    Default

    Thanks trisberg, exactly what I was looking for! I've switched my code over to the NamedParameterJdbcTemplate and it's working great.

    Thanks again

Posting Permissions

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