Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to differentiate SQL-Exceptions?

Status
Not open for further replies.

frag

Programmer
Dec 7, 2000
321
GB
Hello everybody!

I have a JSP-page on which I try to connect to a database (sybase). If an exception is thrwon an error-page is called.

What I now try to do is to set a session-attribute depending
on the sql-exception. But I don't know how to differentiate this. I need to know whether the login-informations (username or password) have been wrong or if the database was unreachable or if the user hasn't got the right to access a certain table.

All I am able to do is to print out the exception with:
<% out.println( exception.toString()); %>

But what I want is something like:

String error = &quot;&quot;;

if (exception = <wrong-login-info>){
error = &quot;wrong username or password&quot;;
}
else if (exception = <db-offline>){
error = &quot;database seems to be offline&quot;;
}
else if (exception = <no-rights>){
error = &quot;no rights for the table&quot;;
}
else{
error = &quot;unexpected exception&quot;;
}

Any ideas??

Cheers

frag patrick.metz@epost.de
 
I haven't used JSP in a while so forgive my ignorance if I say anything blatently foolish, but:
couldn't you get 2 or three keywords/phrases per error, such as the error number, etc and do an indexOf check?
Pseudocode:
Code:
String excepThrown = exception.toString();
if( (exceptThrown.indexOf(&quot;incorrect login&quot;) >= 0) && (exceptThrown.indexOf(&quot;err # here&quot;) >= 0)){
   error=&quot;wrong username or password&quot;
}
else...

So what you can do is choose a couple unique keywords from the error messages that will only appear for each specific error.

-Tarwn
--- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
 
Hi Tarwn,

this was my first thought too. But actually I hoped that
there is a 'nicer' solution to this.

Thanx for your answer.

frag patrick.metz@epost.de
 
I'm not sure if you can, the general exception only has a cause (throwable) and the error, available as a string. Perhaps I'm not hinking sneeky enough today, but I don't see an alternative route on this. :(

-Tarwn --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top