您的位置:首页 > 数据库 > Oracle

Tomcat 7 配置MySQL/Oracle JDBC

2017-04-19 15:06 288 查看
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
9eff
>

    <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();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐