Announcement Announcement Module
Collapse
No announcement yet.
simple newbie queries with JdbcTemplate Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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, 03:33 PM.

  • #2
    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

    Comment


    • #3
      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, 04:54 PM.

      Comment

      Working...
      X