hello,
i need a JDBC call CMP to lookup to some Data.
my coding looks like:
protected java.math.BigDecimal getNextKey()
throws RemoteException, FinderException, javax.naming.NamingException {
java.math.BigDecimal nextKey = new java.math.BigDecimal(0);
PreparedStatement pstmt = null;
Connection con = null;
try {
//Properties env;
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.ibm.ejs.ns.jndi.CNInitialContextFactory");
env.put(Context.PROVIDER_URL, "IIOP://localhost:900/");
InitialContext ic = new InitialContext(env);
Class.forName("oracle.jdbc.driver.OracleDriver");
con=DriverManager.getConnection("jdbc:oracle:thin:@195.82.85.101:1521:dc02","uap","uap");
pstmt = con.prepareStatement("select uap_logs_seq.nextval as key from dual");
ResultSet rs = pstmt.executeQuery();
rs.next();
String id = rs.getString("key");
nextKey = new java.math.BigDecimal(id);
pstmt.close();
con.close();
return nextKey;
}
catch (Exception e) {
throw new FinderException(e.toString());
}
finally {
//Release DB Connection
try {
pstmt.close();
con.close();
}
catch (Exception e) { }
}
}
The problem is that the database_url ist hardcoded, i don't know how to implement it flexible, e.g. if the database changes. My problem is that i don't know how to get the currently used databaseconnection from the EJBServer.
Does anyone give me a hint.
-
JDBC calls Within CMP (6 messages)
- Posted by: christine mandelbaum
- Posted on: April 19 2001 11:04 EDT
Threaded Messages (6)
- JDBC calls Within CMP by Stefan Tilkov on April 19 2001 14:31 EDT
- JDBC calls Within CMP by Kapil Israni on April 19 2001 15:15 EDT
- JDBC calls Within CMP by Raghuram Ethirajan on April 21 2001 08:09 EDT
- JDBC calls Within CMP by christine mandelbaum on April 24 2001 04:27 EDT
- JDBC calls Within CMP by christine mandelbaum on April 24 2001 06:28 EDT
- JDBC calls Within CMP by Jacques Desmazieres on April 25 2001 08:56 EDT
- JDBC calls Within CMP by Kapil Israni on April 19 2001 15:15 EDT
-
JDBC calls Within CMP[ Go to top ]
- Posted by: Stefan Tilkov
- Posted on: April 19 2001 14:31 EDT
- in response to christine mandelbaum
Which app server do you use? If it's not completely outdated, you should be able to use a JNDI lookup to get access to a DataSource object. You can ask that for a connection from the connection pool you've set up for your CMP beans.
Something like
ctx = new InitialContext();
javax.sql.DataSource ds = (javax.sql.DataSource)
ctx.lookup ("myDataSource");
java.sql.Connection conn = ds.getConnection();
Regards,
Stefan -
JDBC calls Within CMP[ Go to top ]
- Posted by: Kapil Israni
- Posted on: April 19 2001 15:15 EDT
- in response to Stefan Tilkov
above is a perfect solution for J2EE access to database connection.
or else u cud even write a helper class which acts a factory for database connection. -
JDBC calls Within CMP[ Go to top ]
- Posted by: Raghuram Ethirajan
- Posted on: April 21 2001 20:09 EDT
- in response to Kapil Israni
That's right!!! Hard coding the connection in the Bean doesn't preserve its portability.As mentioned above, you can
use the getConnection method. Here is a sample and it works fine.
private Connection getConnection()
throws SQLException
{
InitialContext initCtx = null;
try {
initCtx = new InitialContext();
DataSource ds = (javax.sql.DataSource)
initCtx.lookup("java:comp/env/jdbc/demoPool");
return ds.getConnection();
} catch(NamingException ne) {
throw new EJBException(ne);
} finally {
try {
if(initCtx != null) initCtx.close();
} catch(NamingException ne) {
throw new EJBException(ne);
}
}
}
After adding this code , you have mention your database URL and driver in the deployment descriptor and in the weblogic.properties file as well.
Cheers,
Ragu. -
JDBC calls Within CMP[ Go to top ]
- Posted by: christine mandelbaum
- Posted on: April 24 2001 04:27 EDT
- in response to Stefan Tilkov
I'am using WebSphere Applikation Server 3.5 -
JDBC calls Within CMP[ Go to top ]
- Posted by: christine mandelbaum
- Posted on: April 24 2001 06:28 EDT
- in response to Stefan Tilkov
thanks for replay.
i've implemented connection pooling in VAJ WTE v3.5
However, I still have a problem. When I start WTE Persistent Name Server
and
check DataSource Configuration, I see that my datasource named
'SUN01dc02' to
Oracle database exists there. But, when I execute this code:
i've got a problem when i'd like to the current DBconnection
private java.sql.Connection getConnection()
throws SQLException,EJBException {
java.sql.Connection conn=null;
InitialContext initCtx = null;
try {
initCtx = new InitialContext();
javax.sql.DataSource ds = (javax.sql.DataSource)
initCtx.lookup("jdbc/SUN01dc02");
conn = ds.getConnection();
return conn;
}
catch(NamingException ne) {
throw new EJBException(ne.toString());
}
catch (SQLException se){
throw new EJBException(se.toString());
}
catch (Exception e){
throw new EJBException(e.toString());
}
finally {
try {
if(initCtx != null) initCtx.close();
}
catch(NamingException ne) {
throw new EJBException(ne);
}
}//finally
}
the Datasource looks fine but when i trie to getConnection
an SQLException throw in line conn = ds.getConnection();
java.sql.SQLException: Ungültige Argumente in Aufruf
I trie to translate it to english 'Invalid argument in call'
Christine
-
JDBC calls Within CMP[ Go to top ]
- Posted by: Jacques Desmazieres
- Posted on: April 25 2001 08:56 EDT
- in response to christine mandelbaum
When you declared your datasource in websphere, did you provide the username/password? My guess is no, thanks to the sample code you provided in your first post. So when connecting the data source you must provide username/password: getConnection( userName, password ) or include these piece of information within the data source specification : jdbc:oracle:thin:uap/uap@195.82.85.101:1521:dc02
I hope this will sort your problem
Jacques