Howdy,
>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 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 is a good goal. I don't think it's particularly bad importing just
SQLException, or even catching the general Exception and re-throwing it
as a servlet exception. The latter means your servlets don't have to
import SQLException. But for the sake of Tuesday morning boredom, let's
look at some alternatives ;)
>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?
There are always other ways ;)
>Can I maybe define my own exception class to "translate"
>between SQLException and ServletException, and if so what would that
look
>like? Would it inherit from ServletException or SQLException?
Why not?
class MyException {
MyException(String message) {
super(message);
}
MyException(Exception cause) {
super(cause);
}
}
In your JDBCAccess module, everything will throws a MyException instead
of a SQLException, i.e.
try { ... } catch(SQLException sqle) { throw new MyException(sqle); }
In your servlets, you just catch MyException, not SQLException.
So to answer your question, you don't have to inherit from either
ServletException nor SQLException to do this. In fact, you don't want
to inherit in this case unless you have to for other reasons.
Yoav Shapira
This e-mail, including any attachments, is a confidential business communication, and
may contain information that is confidential, proprietary and/or privileged. This
e-mail is intended only for the individual(s) to whom it is addressed, and may not be
saved, copied, printed, disclosed or used by anyone else. If you are not the(an)
intended recipient, please immediately delete this e-mail from your computer system
and notify the sender. Thank you.
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]