This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Spring and the fly likes handling simpleJdbcTemplate error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "handling simpleJdbcTemplate error" Watch "handling simpleJdbcTemplate error" New topic
Author

handling simpleJdbcTemplate error

Aaron Jeffries
Greenhorn

Joined: Oct 20, 2009
Posts: 9
Hello everyone! I have posted a working method below that uses a SimpleJdbcTemplate to retrieve an object from the database (below). What I need to learn is how to handle situations where there is no match found.

When I use SimpleJdbcTemplate to return a list, it will return the list empty if no match is found and I can redirect the user. When SimpleJdbcTemplate searches for a single object I get a stack trace (also below).

How do you redirect the user to a "item not found" page in this case? Thanks for your help.



HTTP ERROR 500

Problem accessing /uRateDrugs-webapp/searchResultsByDrugName.html. Reason:

Incorrect result size: expected 1, actual 0

Caused by:

org.springframework.dao.EmptyResultDataAccessException: Incorrect result size: expected 1, actual 0
at org.springframework.dao.support.DataAccessUtils.requiredSingleResult(DataAccessUtils.java:71)
at org.springframework.jdbc.core.JdbcTemplate.queryForObject(JdbcTemplate.java:722)
at org.springframework.jdbc.core.simple.SimpleJdbcTemplate.queryForObject(SimpleJdbcTemplate.java:169)
at com.acwebsitedesign.uRateDrugs.domain.DrugRepositoryImpl.getDrugByName(DrugRepositoryImpl.java:37)
at com.acwebsitedesign.uRateDrugs.domain.DrugServiceImpl.getDrugByName(DrugServiceImpl.java:14)
at com.acwebsitedesign.uRateDrugs.web.SearchDrugsController.searchByResultsDrugName(SearchDrugsController.java:33)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.doInvokeMethod(HandlerMethodInvoker.java:421)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:136)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:326)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:313)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:875)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:807)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:571)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:511)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:502)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:390)
at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)
at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418)
at org.mortbay.jetty.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:230)
at org.mortbay.jetty.handler.HandlerCollection.handle(HandlerCollection.java:114)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.Server.handle(Server.java:326)
at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:536)
at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:928)
at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:747)
at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:218)
at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:405)
at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:409)
at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Simple you wrap your query call in a try catch block and catch EmptyResultDataAccessException

in the catch what we do is just return null;

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Aaron Jeffries
Greenhorn

Joined: Oct 20, 2009
Posts: 9
Thanks Mark.
Kamala Boddapati
Greenhorn

Joined: Dec 02, 2007
Posts: 1
Thanks, Mark. that helped my issue.
vijay satlawar
Greenhorn

Joined: Sep 05, 2012
Posts: 3
it works perfect .

But you should only catch 'EmptyResultDataAccessException' . This exception occurs only when there are no results

..Vijay Satlawar
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1648
    
    7

queryForObject was designed to enforce this. It is assumed that if no results are returned there is a problem. If not finding an object is not an exceptional case and you don't like catching the exception and returning null, consider using query instead.


[How To Ask Questions][Read before you PM me]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: handling simpleJdbcTemplate error
 
Similar Threads
org.springframework.web.HttpSessionRequiredException: Session attribute 'user' required - not found
com.sun.mail.smtp.SMTPSendFailedException: 451 4.7.1 Please try again later
NullPointerException
Sql Exception: inconsistent datatypes, need help
wild cards in MySQL using jdbc