Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

jdbc template query returning null

Posted on 2013-11-28
3
2,223 Views
Last Modified: 2013-12-02
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
Comment
Question by:Rohit Bajaj
  • 2
3 Comments
 

Author Comment

by:Rohit Bajaj
ID: 39683507
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
 
LVL 35

Accepted Solution

by:
mccarl earned 500 total points
ID: 39684336
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
 

Author Comment

by:Rohit Bajaj
ID: 39691755
Hi,
I was using named Parameter JDBC template and using ? in the query.
Thanks
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
numbers ascending pyramid 101 212
Re-position sub-options beneath the TAB 7 98
Interview question Javascript, database 12 82
Python 3.5.2 32 virtualenv problems 3 64
There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

789 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question