Solved

jdbc template query returning null

Posted on 2013-11-28
3
2,758 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
[X]
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
  • 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 36

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

617 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