Solved

jdbc template query returning null

Posted on 2013-11-28
3
2,506 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 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
fizzArray2 challenge 1 111
Add mobile access to browser application 3 208
java ^ examples 8 75
Problem to page 4 105
Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
This article will show, step by step, how to integrate R code into a R Sweave document
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…
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.

734 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