Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

JSP Database connection with Bean

Posted on 2002-04-17
4
Medium Priority
?
400 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

618 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