Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3217
  • Last Modified:

jdbc template query returning null

Hi,
I have the following piece of code :
The query if i run on the DB runs fine with 1 row as output. But by running in the code
i am always getting cacheData = null ??

String query = "SELECT * FROM fresco_cache " +
                        "WHERE cachekey = ? " +
                        "AND DATE_ADD(update_time_stamp, INTERVAL ttl SECOND) >= now() " +
                        "AND active=1";
            //@SuppressWarnings("unchecked")
            //List<FrescoCache> cacheData = hibernateTemplate.find(query, key);
            MapSqlParameterSource keyvalue= new MapSqlParameterSource();;
            keyvalue.addValue("cachekey",key);
            ColumnMapper cm= new ColumnMapper();
            List<Map<String,Object>> cacheData= jdbcTemplate.query(query, keyvalue, cm);
            
            if(cacheData==null || cacheData.size()==0){
                  return null;
            }
            return (String) cacheData.get(0).get("data");

Please help.

Thanks
0
Rohit Bajaj
Asked:
Rohit Bajaj
  • 2
1 Solution
 
Rohit BajajAuthor Commented:
public class ColumnMapper implements RowMapper<Map<String,Object>>{
            public Map<String, Object> mapRow(ResultSet rs, int rownum) throws SQLException {
                  Map<String, Object> map=new HashMap<String,Object>();
                  for(int i=0;i<rs.getMetaData().getColumnCount();i++){
                        map.put(rs.getMetaData().getColumnLabel(i+1), rs.getObject(i+1));

                  }
                  return map;
            }
      }
0
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
A couple of questions just to clarify a few points...

Are you sure you are actually getting cacheData == null? Could it be that cacheData.size() == 0 and so your method is returning null? Because the .query() methods on jdbcTemplate that return a List should never return null, they at the least return an empty List or otherwise throw an Exception.

What version of Spring JDBC are you using?

What is the actual Class that is your jdbcTemplate variable? Because there are actually a number of variations, JdbcTemplate, SimpleJdbcTemplate & NamedParameterJdbcTemplate.
0
 
Rohit BajajAuthor Commented:
Hi,
I was using named Parameter JDBC template and using ? in the query.
Thanks
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now