P E N C I L
Login

JdbcTempalte

  1. We don't need spring context and whole hibernate
  2. Query
    1. http://k2java.blogspot.ch/2011/04/mapping-object-to-row-in-spring-using.html
    2. jdbcTemplate.queryForObject() - query for one row only
    3. Arbitrary map column
      jdbc.queryForObject(sql, new RowMapper<Quote>() {
           mapRow(ResultSet rs, int rowNum) {
            rs.getDouble("MY_COLUMN");
           }
      });
    4. BeanPropertyRowMapper
      1. jdbc.queryForObject("select userid, client from mytable where userid=:userid", new BeanPropertyRowMapper(
                      MyClass.class), id);
        Note: MyClass needs setters.
        If there is no results, throw exception.
      2. queryForObject(sql, Class<T> type) is for ONE row + ONE column so it's not for query a structured object
      3. List<o> list = jdbc.query("select * from mytable where userid=:userid", new BeanPropertyRowMapper( MyClass.class), id);
    5. DataAccessUtils.singleresult()
    6. Jdbc.update();
    7. Query for object
      String v = JdbcTemplate.queryForObject("select mycol from mytable where client=:client", String.class, client)
      .queryForObject("select * from devices where id=:id",  new BeanPropertyRowMapper<Device>(Device.class), id);
    8. JdbcTemplate.queryOne("sql")