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:
- Difference between isempty method and isblank method in stringutils
- “Method does not override method from its superclass”
- java.lang.IllegalStateException Exception: cause analysis and solution
- C#: Analysis of the difference between write() and writeline()
- OpenGL programming error analysis
- Analysis of compilation errors of “error conflicting types for function”
- Analysis of “RSL error 1 of 1”
- Source code analysis of macOS startup process
- Analysis of shadows a parameter exception in C + +
- Analysis and solution of RSA premaster secret error
- Bitcoin source code analysis – load Wallet
- Analysis of jitter using atop and perf
- [Linux] [kernel] bug: scheduling while atomic problem analysis
- Statistical learning: ANOVA (analysis of variance) (1)
- Online problem solving analysis
- Performance Clarke error grid analysis
- Error analysis of swap file “*. SWP” already exists! When editing files with VIM
- tf.layers.conv1d Function analysis (one dimensional convolution)
- Override the equals method and override the toString method
- Keepalived add service self-start error analysis [How to Solve]