Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

JSP Database connection with Bean

Posted on 2002-04-17
4
Medium Priority
?
403 Views
Last Modified: 2013-12-29
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
0
Comment
Question by:barnarp
  • 2
4 Comments
 
LVL 9

Expert Comment

by:amx
ID: 6949325
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.
0
 
LVL 3

Accepted Solution

by:
rjackman earned 400 total points
ID: 6949629
hi
Pls refer to ur Bean code where u have a method
public Connection Connect (String dbname,String user,String pass) throws ClassNotFoundException,SQLException

but in the method u are catching the SQLException so if the exception occurs it is being catched by the code
what u have to do is catch the exception and rethrow it
e.g
try
{
   .......ur code here
}
catch(SQLException sq)
{
   throw new SQLException("SQL error occured");
   //or u can rethrow the sq like following
   //throw sq;
}

I think this may solve ur prob
cheers
RJ
0
 
LVL 3

Expert Comment

by:rjackman
ID: 6949630
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
0
 

Author Comment

by:barnarp
ID: 6949718
Thanks rjackman,

Problem solved!

Regards

Pierre
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month21 days, 3 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question