tomcat连接Oracle数据库server文件的配置

tomcat配置文件中server.xml文件连接oracle数据库时的常规配置:

<?xml version='1.0' encoding='utf-8'?>


<Server port="8012" shutdown="SHUTDOWN">
  <!--APR library loader. Documentation at /docs/apr.html -->
  <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
  <!--Initialize Jasper prior to webapps are loaded. Documentation at /docs/jasper-howto.html -->
  <Listener className="org.apache.catalina.core.JasperListener" />
  <!-- Prevent memory leaks due to use of particular java/javax APIs-->
  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
  <!-- JMX Support for the Tomcat server. Documentation at /docs/non-existent.html -->
  <Listener className="org.apache.catalina.mbeans.ServerLifecycleListener" />
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
 
  <!-- Global JNDI resources Documentation at /docs/jndi-resources-howto.html  -->    
  <GlobalNamingResources>
    <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" />
  </GlobalNamingResources>

<Service name="Catalina">
<Connector port="8383" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />
<!--<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />-->
<Engine name="Catalina" defaultHost="localhost">  
  <Realm className="org.apache.catalina.realm.LockOutRealm">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/>
  </Realm>
 <Host name="localhost"  appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">

             <Context path="" docBase="D:\apachetomcat\webapps\发布文件的名称" URIEncoding="UTF-8" debug="0" privileged="true"  reloadable="true"><!-- docBase配置为发布的项目的路径-->
<Resource name="jdbc/recruitDS" <!--资源名称-->
 type="javax.sql.DataSource" 
 driverClassName="oracle.jdbc.OracleDriver" 
 maxActive="150" 
 maxIdle="20" 
 maxWait="10000"
 username="连接数据库的用户名"
 password="连接数据库的密码"
 url="连接数据的URL" 
 />
</Context>

 </Host>
</Engine>
</Service>
 
</Server>

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
1. 下载ojdbc.jar文件。这是Oracle提供的Java驱动程序,用于连接Oracle数据库。 2. 将ojdbc.jar文件复制到Tomcat的lib目录下。 3. 在Tomcat的conf目录下的server.xml文件中,找到以下内容: ``` <GlobalNamingResources> ``` 4. 在该标签下添加以下内容: ``` <Resource name="jdbc/Oracle" auth="Container" type="javax.sql.DataSource" maxActive="100" maxIdle="30" maxWait="10000" username="your_username" password="your_password" driverClassName="oracle.jdbc.driver.OracleDriver" url="jdbc:oracle:thin:@your_host:your_port:your_database"/> ``` 其中,name属性为数据源的名称,可自定义;auth属性为验证方式,这里使用Container;type属性为数据源类型,这里使用javax.sql.DataSource;maxActive属性为最大连接数;maxIdle属性为最大空闲连接数;maxWait属性为获取连接的最大等待时间;username和password属性为连接数据库的用户名和密码;driverClassName属性为驱动程序的类名;url属性为连接数据库的URL。 5. 在Tomcat的WEB-INF目录下创建context.xml文件,并添加以下内容: ``` <Context> <ResourceLink name="jdbc/Oracle" global="jdbc/Oracle" type="javax.sql.DataSource"/> </Context> ``` 6. 在Java代码中获取数据库连接: ``` Context initContext = new InitialContext(); Context envContext = (Context) initContext.lookup("java:/comp/env"); DataSource ds = (DataSource) envContext.lookup("jdbc/Oracle"); Connection conn = ds.getConnection(); ``` 其中,java:/comp/env是固定的路径,jdbc/Oracle是数据源的名称。getConnection()方法用于获取数据库连接

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值