Solved

jdbc template query returning null

Posted on 2013-11-28
3
1,920 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Writing a parser for java language 4 61
Opening a Access Database 5 121
wordlen challenge 3 49
Not needed 13 58
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…
Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
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 viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

743 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now