How to connect data base using jndi datasource in weblogic
How to connect data base using jndi datasource in weblogic. i am using following code but it is giving null value for connection
Context ctx = null;
Hashtable evn = new Hashtable();
evn.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
evn.put(Context.PROVIDER_URL,"t3://localhost:7001");
Connection conn = null;
try {
ctx = new InitialContext(evn);
javax.sql.DataSource ds
= (javax.sql.DataSource) ctx.lookup ("mydatasource");
conn = ds.getConnection();
}catch (Exception e) {
System.out.println();
开发者_JS百科 // TODO: handle exception
}
Append this to clarify more about exception reason
catch(Exception sqlExp)
{
throw new SQLException("getConnection :: Exception"+sqlExp);
}
- post the exception as suggested by Stano
on weblogic console test the connection http://docs.oracle.com/cd/E23943_01/web.1111/e13737/jdbc_datasources.htm#CHDIIFHH
did you target the datasource to weblogic server where the application works?
//try this code:
Connection con = null;
DataSource datasource = null;
Context initialContext = new InitialContext();
// "jdbc/MyDBname" >> is a JNDI Name of DataSource on weblogic
datasource = (DataSource) initialContext.lookup("jdbc/MyDBname");
con = datasource.getConnection();
精彩评论