Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Using CMP, How To Establish Connection to the Oracle Database?

 
Ranch Hand
Posts: 1309
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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" );
}
}
}
}
 
My previous laptop never exploded like that. Read this tiny ad while I sweep up the shards.
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic