Skip to Content
0
Former Member
Jul 21, 2006 at 06:33 AM

Some problems on JNDI and JDBC

21 Views

We use the JNDI to access the JDBC Datasource.

The sample code is:

public class DemoConn {

public DemoConn() {

}

private static Context getInitialContext{

try {

if (AppContext.getInstance().getUrl().equals("NULL"))

return new InitialContext();

else {

String url = AppContext.getInstance().getUrl();

String provider = AppContext.getInstance().getProvider();

Properties properties = new Properties();

properties.put(Context.INITIAL_CONTEXT_FACTORY, provider);

properties.put(Context.PROVIDER_URL, url);

Context tempContext = new InitialContext(properties);

return tempContext;

}

} catch (Exception e) {

e.printStackTrace();

return null;

}

}

public Connection getConn() throws NamingException {

String m_datasource_name = "jdbc/emdb";

try {

InitialContext jndiCntx = (InitialContext) getInitialContext();

DataSource ds = (DataSource) jndiCntx.lookup(m_datasource_name);

Connection con = null;

if (ds == null)

logger.logDebug(" jndiCntx : ds is null !");

else {

con = ds.getConnection();

logger.logDebug("Have get the con:" + con.toString());

}

return con;

} catch (Exception e) {

e.printStackTrace();

throw new NamingException(e.getMessage());

}

}

}

the properties file contains:

cop.appserver.name=weblogic

cop.jndi.name=jdbc/emdb

cop.jndi.provider=com.inqmy.services.jndi.InitialContextFactoryImpl

cop.url.name=iiop://localhost:3011/

When the application running, it output the infomation :

"Cannot instantiate class: com.inqmy.services.jndi.InitialContextFactoryImpl"

how can I solve the problem?thanks!

Message was edited by: Ji Lei