Link to home
Start Free TrialLog in
Avatar of barnarp
barnarp

asked on

JSP Database connection with Bean

Hi,

I currently use the following code to connect to an oracle database from a JSP page:

<jsp:useBean id="db" scope="session" class="mypackage.mybean" />
Connection Conn = null;
try
{
Conn = db.Connect("db","user","pass");
}
catch(SQLException e)
{
}

The Bean code looks something like this:

public Connection Connect (String dbname,String user,String pass) throws ClassNotFoundException,SQLException

{
try
{
conn Connection = null;
Class.forName(<oracle driver>);
 
conn = DriverManager.getConnection(
                          user, pass);      

                          dbname,}
catch(SQLException e)
{
}
catch(Exception ex)
{
}

return conn;    
}

This works fine, except, I need both the Oracle errormessage (if one exists) and the connection object returned from the bean.

Currently with above code, I get the connection object back, which I need to pass on to other pages where the user need to be identified, but what if an error occurs?, then I only get the default Tomcat error "null pointer exception" instead of an oracle error which I can display.

I have also tried doing the whole connection in the bean and return a string object to JSP, in which case if I get an empty string back, I know the connection was made, and if not, I get the bean to pouplate the errormessage in the return string. But then I have no connection object for other pages that the user might browse to.

Am I missing something obvious?

Regards

Pierre
Avatar of amx
amx
Flag of United Kingdom of Great Britain and Northern Ireland image

i think that the SQL exception class has a method to print out the error message produced from the database.

Have a look at the api for the SQL Exception class.
ASKER CERTIFIED SOLUTION
Avatar of rjackman
rjackman

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rjackman
rjackman

hi again
more over in ur jsp rethrow the exception so that it will give u the exact exception message if some thing goes wrong
cheers
RJ
Avatar of barnarp

ASKER

Thanks rjackman,

Problem solved!

Regards

Pierre