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
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