虚拟化容器,大数据,DBA,中间件,监控。

Tomcat 7 配置MySQL/Oracle JDBC驱动

25 04月
作者:admin|分类:应用管理

1. Tomcat 7 配置MySQL JDBC

Step1:

修改apache-tomcat-7.0.75 conf/ server.xml 放入  <GlobalNamingResources> 标签中:

    <Resource name="jdbc/Demo"
      auth="Container"
      type="javax.sql.DataSource"
      factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
      testWhileIdle="true"
      testOnBorrow="true"
      testOnReturn="false"
      validationInterval="30000"
      timeBetweenEvictionRunsMillis="30000"
      maxActive="100"
      minIdle="10"
      maxWait="10000"
      initialSize="10"
      removeAbandonedTimeout="60"
      removeAbandoned="true"
      logAbandoned="true"
      minEvictableIdleTimeMillis="30000"
      jmxEnabled="true"
      jdbcInterceptors="org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer"
      username="demo"
      password="demo"
      driverClassName="com.mysql.jdbc.Driver"
      url="jdbc:mysql://sjXXXX:3306/test"/>

Step2:

修改apache-tomcat-7.0.75 conf/ context.xml

<ResourceLink global="jdbc/Demo" name="jdbc/Demo" type="javax.sql.DataSource"/>

Step3:

修改web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  <resource-ref>

<!-- add-->
    <description>MySQL DB Connection Pool</description>
    <res-ref-name>jdbc/Demo</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
    <res-sharing-scope>Shareable</res-sharing-scope>
  </resource-ref>
</web-app>

Step4: (不一定都需要)

把MYSQL 相应的驱动包 放到Tomcat/ lib 目录下 例如:  mysql-connector-java-5.1.20-bin.jar


2. Tomcat 7 配置Oracle JDBC

Step1:

修改apache-tomcat-7.0.75 conf/ server.xml 放入  <GlobalNamingResources> 标签中:

    <Resource name="jdbc/QA" auth="Container"
              type="javax.sql.DataSource" driverClassName="oracle.jdbc.OracleDriver"
              url="jdbc:oracle:thin:@//10.9.***.***:XXXX/oracleDBName"
              username="admin" password="admin" maxActive="20" maxIdle="10"
              maxWait="-1"/>

Step2:

修改apache-tomcat-7.0.75 conf/ context.xml

<ResourceLink global="jdbc/QA" name="jdbc/QA" type="javax.sql.DataSource"/>

Step3:

修改web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  <resource-ref>
    <description>QA DB Connection Pool</description>
    <res-ref-name>jdbc/QA</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
    <res-sharing-scope>Shareable</res-sharing-scope>
  </resource-ref>

</web-app>

Step4: (不一定都需要)

把Oracle 相应的驱动包 放到Tomcat/ lib 目录下 例如:  ojdbc6.jar


Test JDBC connection code example:

if(env!=null){
DataSource ds = (DataSource)env.lookup("jdbc/"+ dbName);
if(ds==null) throw new Exception("Can't find the DataSource for "+ envsiteID);
return ds.getConnection();
}

————————————————
版权声明:本文为CSDN博主「com542948180」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/com542948180/java/article/details/70240977

浏览870 评论0
返回
目录
返回
首页
simple-log博客换成zblog博客关于mysql中HTML转义字符的处理 CentOS 7.x 安装 Docker-Compose