Solved

JSP Database connection with Bean

Posted on 2002-04-17
4
375 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks rjackman,

Problem solved!

Regards

Pierre
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…

762 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

9 Experts available now in Live!

Get 1:1 Help Now