Solved

JSP Database connection with Bean

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

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…
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…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

738 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