当前位置: 技术问答>java相关
那位哥们能举个通过jndi连接连接池的例子??多谢!!!
来源: 互联网 发布时间:2015-05-13
本文导语: 那位哥们能举个通过jndi连接连接池的例子??多谢!!! | InitialContext initCtx = null; try { initCtx = new InitialContext(); DataSource ds = (javax.sql.DataSource) initCtx...
那位哥们能举个通过jndi连接连接池的例子??多谢!!!
|
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);
}
}
|
InitialContext ctx = new InitialContext();
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();
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();
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。