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) {
log("Failed to lookup JDBC Datasource. Please double check that");
log("the JNDI name defined in the resource-description of the ");
log("EJB's weblogic-ejb-jar.xml file is the same as the JNDI name ");
log("for the Datasource defined in your config.xml.");
throw new EJBException(ne);
} finally {
try {
if(initCtx != null) initCtx.close();
} catch(NamingException ne) {
log("Error closing context: " + ne);
throw new EJBException(ne);
}
}
try {
initCtx = new InitialContext();
DataSource ds = (javax.sql.DataSource)
initCtx.lookup("java:comp/env/jdbc/demoPool");
return ds.getConnection();
} catch(NamingException ne) {
log("Failed to lookup JDBC Datasource. Please double check that");
log("the JNDI name defined in the resource-description of the ");
log("EJB's weblogic-ejb-jar.xml file is the same as the JNDI name ");
log("for the Datasource defined in your config.xml.");
throw new EJBException(ne);
} finally {
try {
if(initCtx != null) initCtx.close();
} catch(NamingException ne) {
log("Error closing context: " + ne);
throw new EJBException(ne);
}
}
DataSource dataSource = (javax.sql.DataSource)
ctx.lookup("jdbc/dsName");
Connection conn = dataSource.getConnection();
或
InitialContext ctx = new InitialContext();
DataSource dataSource = (javax.sql.DataSource)
ctx.lookup("java:comp/env/jdbc/yourDSName");
//需要在发布描述文件中声明资源引用
Connection conn = dataSource.getConnection();