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","pa ss");
}
catch(SQLException e)
{
}
The Bean code looks something like this:
public Connection Connect (String dbname,String user,String pass) throws ClassNotFoundException,SQL Exception
{
try
{
conn Connection = null;
Class.forName(<oracle driver>);
conn = DriverManager.getConnectio n(
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
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","pa
}
catch(SQLException e)
{
}
The Bean code looks something like this:
public Connection Connect (String dbname,String user,String pass) throws ClassNotFoundException,SQL
{
try
{
conn Connection = null;
Class.forName(<oracle driver>);
conn = DriverManager.getConnectio
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
ASKER
Thanks rjackman,
Problem solved!
Regards
Pierre
Problem solved!
Regards
Pierre
Have a look at the api for the SQL Exception class.