• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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" );
}
}
}
}
 
Ranch Hand
Posts: 228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Pen,
Why do u have to go thru the hassel of writing all the connectivity code. Use Connection Pool instead and simply map that in the DD of u'r CMP.
Thanks
Sahil
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic