File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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 Customer Requirements for Developers this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "handling simpleJdbcTemplate error" Watch "handling simpleJdbcTemplate error" New topic

handling simpleJdbcTemplate error

Aaron Jeffries

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.


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.jdbc.core.JdbcTemplate.queryForObject(
at org.springframework.jdbc.core.simple.SimpleJdbcTemplate.queryForObject(
at com.acwebsitedesign.uRateDrugs.domain.DrugRepositoryImpl.getDrugByName(
at com.acwebsitedesign.uRateDrugs.domain.DrugServiceImpl.getDrugByName(
at com.acwebsitedesign.uRateDrugs.web.SearchDrugsController.searchByResultsDrugName(
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(
at sun.reflect.DelegatingMethodAccessorImpl.invoke(
at java.lang.reflect.Method.invoke(
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(
at org.springframework.web.servlet.DispatcherServlet.doDispatch(
at org.springframework.web.servlet.DispatcherServlet.doService(
at org.springframework.web.servlet.FrameworkServlet.processRequest(
at org.springframework.web.servlet.FrameworkServlet.doPost(
at javax.servlet.http.HttpServlet.service(
at javax.servlet.http.HttpServlet.service(
at org.mortbay.jetty.servlet.ServletHolder.handle(
at org.mortbay.jetty.servlet.ServletHandler.handle(
at org.mortbay.jetty.servlet.SessionHandler.handle(
at org.mortbay.jetty.handler.ContextHandler.handle(
at org.mortbay.jetty.webapp.WebAppContext.handle(
at org.mortbay.jetty.handler.ContextHandlerCollection.handle(
at org.mortbay.jetty.handler.HandlerCollection.handle(
at org.mortbay.jetty.handler.HandlerWrapper.handle(
at org.mortbay.jetty.Server.handle(
at org.mortbay.jetty.HttpConnection.handleRequest(
at org.mortbay.jetty.HttpConnection$RequestHandler.content(
at org.mortbay.jetty.HttpParser.parseNext(
at org.mortbay.jetty.HttpParser.parseAvailable(
at org.mortbay.jetty.HttpConnection.handle(
at org.mortbay.thread.QueuedThreadPool$
Mark Spritzler

Joined: Feb 05, 2001
Posts: 17271

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

in the catch what we do is just return null;


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

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

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

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

Joined: Mar 07, 2010
Posts: 1682

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:
subject: handling simpleJdbcTemplate error
It's not a secret anymore!