码迷,mamicode.com
首页 > 数据库 > 详细

Tomcat上配置连接池{ connect error=Name [jdbc/OracleDB] is not bound in this Context. Unable to find [jdbc]}

时间:2016-09-23 14:48:00      阅读:268      评论:0      收藏:0      [点我收藏+]

标签:

. 在学习期间,从未实践过在tomcat上配置连接池,今天终于实现一次,在tomcat玩了一把,不知道你是否现在有和我一样的困境。废话少说直接上代码
 
java 
 public static Connection getConnection_tomcat() {
	        Connection conn = null;
	        try {
	            Context initCtx = new InitialContext();
	            Context ctx = (Context) initCtx.lookup("java:comp/env");
	       
	            Object obj = (Object) ctx.lookup("jdbc/OracleDB");
	            javax.sql.DataSource ds = (javax.sql.DataSource) obj;
	            return conn = ds.getConnection();
	        }
	        catch (Exception e) {
	           
	            System.out.println("connect error=" + e.getMessage());
	            return null;
	        }
	    }

在tomcat->conf->server.xml

  <GlobalNamingResources>
    <!-- Editable user database that can also be used by
         UserDatabaseRealm to authenticate users
    -->
    <Resource name="UserDatabase" auth="Container"
              type="org.apache.catalina.UserDatabase"
              description="User database that can be updated and saved"
              factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
              pathname="conf/tomcat-users.xml" />
		
<!-- 在tomcat 配置连接池 -->		
			  
	<Resource name="jdbc/OracleDB"  auth="Container"           
              type="javax.sql.DataSource"  
			  driverClassName="oracle.jdbc.driver.OracleDriver"    
			  url=""  
			  maxActive="50"    
			  maxldle="10"    
			  maxWait="5000"  
			  username=""  
			  password="" />  
  </GlobalNamingResources>

在tomcat->conf->context.xml 

<Context>

    <WatchedResource>WEB-INF/web.xml</WatchedResource>


	<!-- 在配置一个连接池 -->
	<ResourceLink global="jdbc/OracleDB" name="jdbc/OracleDB" type="javax.sql.DataSource"/>  
</Context>

这样就大功告成了!  

Tomcat上配置连接池{ connect error=Name [jdbc/OracleDB] is not bound in this Context. Unable to find [jdbc]}

标签:

原文地址:http://www.cnblogs.com/xkl520xka/p/5899876.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!