Solved

JSP Database connection with Bean

Posted on 2002-04-17
4
376 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 100 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

930 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now