Error analysis of queryformap method of jdbctemplate

  The queryformap method of jdbctemplate reported an error   If the queryformap method is not used properly, an error will occur  

The queryForMap method in JdbcTemplate only expects a single row to be returned, The return value for this method will be a map of column names to column values for the single result row.

The size of the result set returned by the queryformap method must be 1. In the returned map, the name of the column is used as the key and the value obtained is used as the value  

public void test() {
   String SQL_GET_ATTRIBUTE = "select * from test where rownum<1";
   Map<Object, Object> map = getJdbcTemplate().queryForMap(SQL_GET_ATTRIBUTE);
   Set<Entry<Object, Object >> entrySet = map.entrySet();
   for (Entry < Object, Object > entry :entrySet){
        System.out.println("key is "  + entry.getKey());
        System.out.println("value is "  + entry.getValue());
   }
}

The output will list all the fields of test as key, and the queried value is vlaue. Here, Oracle database is used, and “where rownum & lt; 1 “to restrict the return of only one line.  
Using spring’s getjdbctemplate(). Queryformap, if an empty set is returned, a  
org.springframework.dao.emptyresultdataaccessexception: incorrect result size: expected 1, actual 0. The solution is to catch this exception and return null.  
java code    
 

 try{  
       return getJdbcTemplate().queryForMap("select s.fb,s.pb,s.tu,s.qt,s.fbc,s.pbc,s.tt,s.ft,s.lt from gbc$view_user s where s.ud = ? and ad= ?", new Object[]{ud,ad});  
    }catch (EmptyResultDataAccessException e) {  
        return null;  
    } 

 

Read More: