I am working on a
J2EE Project on the WebLogic 6.0 platform. I use the container managed persistence (CMP) and each table in my Oracle database is an entity bean.
This is what I have been thinking:
Every time I use an entity bean, I should establish
the connection to the database. Therefore, I have
written a utility class (DBConnection.java)(see the
attached codes) with a
getConnection() method in it. And I should call this
method in the home interface, remote interface, and
implementation class of the entity bean I am using.
However, somebody told me that I should not use
the DBConnection class to do it. I should instead set
up the data source and configure the entity bean.
I am very confused. Where do I get the
instructions to do it in accordance with what
"somebody told me" if my way of doing it is wrong?
Thank you in advance. (Please see the attachment.)
package ConnectionUtil;
import javax.servlet.*;
import javax.servlet.http.*;
public class DBConnection {
private
String host = "192.168.50.20";
private String dbName = "TEAPP";
private Int port = 1521;
private Connection connection = null;
private String URL = "jdbc
racle:thin:@" + host + ":" + port + ":" + dbname;
private String USER = "TIME123";
private String PWD = "TIME123";
public static Connection getConnection() throws DBConnectionFailedException {
try {
public void init( ServletConfig config )
throws ServletException
{
super.init( config );
try {
Class.forName( "oracle.jdbc.driver.oracleDriver" );
connection =
DriverManager.getConnection( URL, USER, PWD );
}
catch ( Exception e ) {
e.printStackTrace();
connection = null;
}
}
}
catch( Exception e ) {
DBConnectionFailedException except = new DBConnectionFailedException( String.valueOf( e ) );
System.out.println( "DBConnection exception: " + e );
throw( except );
}
return connection;
public void destroy()
{
try {
connection.close();
}
catch( Exception e ) {
System.err.println( "Problem closing the database" );
}
}
}
}