Results 1 to 3 of 3

Thread: simple newbie queries with JdbcTemplate

  1. #1

    Default simple newbie queries with JdbcTemplate

    Hi,

    I'm a jdbc/spring newbie and trying to perform some simple queries:

    1) I have a CAR_TABLE with two columns CAR_MAKE and CAR_MODEL both strings. How do I get a list of Car objects..

    I tried this:

    HTML Code:
    public List<Car> getCars() {
            String sql = "select CAR_MAKE, CAR_MODEL from CAR_TABLE";
            RowMapper mapper = new RowMapper() {
                public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
                    Car car = new Car();
                    car.setMake(rs.getString("CAR_MAKE"));
                    car.setModel(rs.getString("CAR_MODEL"));
                    return car;
                }
            };
            return (List<Car>)jdbcTemplate.queryForObject(sql, mapper);
        }
    but this gives me:

    Exception in thread "main" org.springframework.dao.IncorrectResultSizeDataAcc essException: Incorrect result size: expected 1, actual 4515


    also 2) How do I get just a list of strings for all elements in the CAR_MAKE column?

    Thanks very much, Jason
    Last edited by jason_novotny; Jul 21st, 2007 at 02:33 PM.

  2. #2
    Join Date
    Dec 2006
    Location
    Jakarta
    Posts
    45

    Default

    you are using
    Code:
    jdbcTemplate.queryForObject(sql, mapper);
    if i'm not mistaken it's used to return only 1 model

    try to use :
    Code:
    jdbcTemplate.queryForList
    method

    hope this can help
    Isyak Melton
    Senior IT Consultant

  3. #3
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    Indeed, this all depends on what it is you are trying to do. If you are only trying to find a single item then you are using the correct method but you aren't constraining the search results to bring back a unique item. You you do want to find all items, then make the change suggested previously.
    Last edited by karldmoore; Aug 27th, 2007 at 03:54 PM.
    Barracuda Networks SSL VPN Lead Developer
    http://pramatr.wordpress.com
    http://twitter.com/karldmoore
    http://www.linkedin.com/in/karldmoore
    Any postings are my own opinion, and should not be attributed to my employer or clients.

Posting Permissions

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