How to use Spring jdbc templates (jdbcTemplate or namedParameterJDBCTem) to retrieve values from database

java, spring, spring-jdbc

Solution

The other answers are sensible: you should create a DTO bean, or use the `BeanPropertyRowMapper`.

But if you want to be able to have more control than the BeanPropertyRowMapper, (or reflection makes it too slow), you can use the

`queryForMap`

method, which will return you a list of Maps (one per row) with the returned columns as keys. Because you can call `get(/* key that is not there */)` on a Map without throwing an exception (it will just return null), you can use the same code to populate your object irrespective of which columns you selected.

Problem

Few days into Spring now. Integrating Spring-JDBC into my web application. I was successfully able to preform CRUD operations on my DB, impressed with boiler-plate code reduction. But I am failing to use the `query*()` methods provided in `NamedParameterJDBCTemplate`. Most of the examples on the internet provide the usage of either `RowMapper` or `ResultSetExtractor`. Though both uses are fine, it forces me to create classes which have to implement these interfaces. I have to create bean for every type of data I am loading for the DB (or maybe I am mistaken). Problem arises in code section where I have used something like this: ``` String query="select username, password from usertable where username=?" ps=conn.prepareStatement(query); ps.setString(username); rs=ps.executeQuery(); if(rs.next()){ String username=rs.getString("username"); String password=rs.getString("password") //Performs operation on them } ``` As these values are not stored in any bean and used directly, I am not able to integrate jdbcTemplate in these kind of situations. Another situation arises when I am extracting only part of properties present in bean from my database. Example: ``` public class MangaBean{ private String author; private String title; private String isbn; private String releaseDate; private String rating; //getters and setters } ``` Mapper: ``` public class MangaBeanMapper implements RowMapper<MangaBean>{ @Override public MangaBean mapRow(ResultSet rs, int arg1) throws SQLException { MangaBean mb=new MangaBean(); mb.setAuthor(rs.getString("author")); mb.setTitle(rs.getString("title")); mb.setIsbn(rs.getString("isbn")); mb.setReleaseDate(rs.getString("releaseDate")); mb.setRating(rs.getString("rating")); return mb; } } ``` The above arrangement runs fine like this: ``` String query="select * from manga_data where isbn=:isbn" Map<String, String> paramMap=new HashMap<String, String>(); paramMap.put("isbn", someBean.getIsbn()); return template.query(query, paramMap, new MangaBeanMapper()); ``` However, if I only want to retrieve two/three values from my db, I cannot use the above pattern as it generates a `BadSqlGrammarException: releaseDate does not exist in ResultSet` . Example : ``` String query="select title, author where isbn=:isbn" Map<String, String> paramMap=new HashMap<String, String>(); paramMap.put("isbn", someBean.getIsbn()); return template.query(query, paramMap, new MangaBeanMapper()); ``` Template is an instance of `NamedParameterJDBCTemplate`. Please advice me solutions for these situations.

Original source