translating SQLException to ServletException

M

Mohun Biswas

This feels like something that would come up often but, not being a Java
guru, I'm not sure of the solution. I have a web app with a number of
servlets accessing a database via JDBC. I encapsulate all the JDBC work
within a class called JDBCAccess. In order to allow code to be tested
from the command line (or from other non-servlet-engine environments) I
don't want JDBCAccess to use any javax.servlet.* classes. Thus my
encapsulation is strict both ways: the servlets don't use any java.sql.*
classes (directly) and the database code doesn't use any javax.servlet.*
classes.

This works fine with one irritating little problem: since each
JDBCAccess method can throw a SQLException, each use of them within a
servlet must do a boilerplate try/catch and re-throw it as
ServletException, e.g.

try {
JDBC.doSomething();
} catch (SQLException e) {
throw new ServletException(e);
}

I can't declare my servlets to throw SQLException, partly because I
don't want to and of course because the spec doesn't allow it anyway. Is
there a simpler way? Can I maybe define my own exception class to
"translate" from SQLException to ServletException, and if so what would
that look like? Would it inherit from ServletException or SQLException?

Thanks,
MB
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top