设备Tomcat数据源   
               添加时间:2013-7-16 点击量: 
 
              1、体式格式一:在server.xml中设备
  1)tomcat安装路径下conf目次下的server.xml,在<GlobalNamingResources>和</GlobalNamingResources>标签之间参加下面的内容:
<Resource name=jdbc/appDS auth=Container
               type=javax.sql.DataSource
               driverClassName=com.mysql.jdbc.Driver
               url=jdbc:mysql://localhost:3306/app?rewriteBatchedStatements=true
               username=root
               password=root
               maxActive=100
               maxIdle=20
               maxWait=10000/> 
  2)tomcat安装路径下conf目次下的context.xml,在<Context>和</Context>标签之间参加如下内容:
<ResourceLink name=jdbc/appDS global=jdbc/appDS  type=javax.sql.DataSource/>
  3)web.xml设备
<resource-ref>
        <description>app_DataSource</description>
        <res-ref-name>jdbc/appDS</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
    </resource-ref> 
2、体式格式二:在context.xml中设备
  1)tomcat安装路径下conf目次下的context.xml,在<Context>和</Context>标签之间参加如下内容:
<Resource name=jdbc/appDS auth=Container
               type=javax.sql.DataSource
               driverClassName=com.mysql.jdbc.Driver
               url=jdbc:mysql://localhost:3306/app?rewriteBatchedStatements=true
               username=root
               password=root
               maxActive=100
               maxIdle=20
               maxWait=10000/> 
  2)web.xml设备
<resource-ref>
        <description>app_DataSource</description>
        <res-ref-name>jdbc/appDS</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
    </resource-ref> 
3、体式格式三:在项目中的/WebRoot/META-INF/目次下创建一个context.xml文件,内容如下:
<?xml version=1.0 encoding=utf-8?>
<Context>
    <Resource name=jdbc/appDS auth=Container
                type=javax.sql.DataSource
                driverClassName=com.mysql.jdbc.Driver
                url=jdbc:mysql://localhost:3306/app?rewriteBatchedStatements=true
                username=root
                password=root
                maxActive=100
                maxIdle=20
                maxWait=10000/> 
</Context>
   web.xml设备如下:
 <resource-ref>
        <description>app_DataSource</description>
        <res-ref-name>jdbc/appDS</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
    </resource-ref> 
4、如何应用
  1)在法度中应用
Context initContext = new InitialContext();
            Context ctx  = (Context)initContext.lookup(java:/comp/env);
            DataSource ds = (DataSource)ctx.lookup(jdbc/appDS); 
            Connection conn = ds.getConnection(); //数据操纵
            //...
            
            //封闭资料
  2)在spring设备文件中应用
 <bean id=dataSource class=org.springframework.jndi.JndiObjectFactoryBean>
              <property name=jndiName><value>java:comp/env/jdbc/appDS</value></property>
      </bean>
无论对感情还是对生活,“只要甜不要苦”都是任性而孩子气的,因为我们也不完美,我们也会伤害人。正因为我们都不完美,也因为生活从不是事事如意,所以对这些“瑕疵”的收纳才让我们对生活、对他人的爱变得日益真实而具体。—— 汪冰《世界再亏欠你,也要敢于拥抱幸福》
                     
                  
     
  
 
    
    
1、体式格式一:在server.xml中设备
1)tomcat安装路径下conf目次下的server.xml,在<GlobalNamingResources>和</GlobalNamingResources>标签之间参加下面的内容:
<Resource name=jdbc/appDS auth=Container
type=javax.sql.DataSource
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/app?rewriteBatchedStatements=true
username=root
password=root
maxActive=100
maxIdle=20
maxWait=10000/>
2)tomcat安装路径下conf目次下的context.xml,在<Context>和</Context>标签之间参加如下内容:
<ResourceLink name=jdbc/appDS global=jdbc/appDS type=javax.sql.DataSource/>
3)web.xml设备
<resource-ref>
<description>app_DataSource</description>
<res-ref-name>jdbc/appDS</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
2、体式格式二:在context.xml中设备
1)tomcat安装路径下conf目次下的context.xml,在<Context>和</Context>标签之间参加如下内容:
<Resource name=jdbc/appDS auth=Container
type=javax.sql.DataSource
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/app?rewriteBatchedStatements=true
username=root
password=root
maxActive=100
maxIdle=20
maxWait=10000/>
2)web.xml设备
<resource-ref>
<description>app_DataSource</description>
<res-ref-name>jdbc/appDS</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
3、体式格式三:在项目中的/WebRoot/META-INF/目次下创建一个context.xml文件,内容如下:
<?xml version=1.0 encoding=utf-8?>
<Context>
<Resource name=jdbc/appDS auth=Container
type=javax.sql.DataSource
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/app?rewriteBatchedStatements=true
username=root
password=root
maxActive=100
maxIdle=20
maxWait=10000/>
</Context>
web.xml设备如下:
<resource-ref>
<description>app_DataSource</description>
<res-ref-name>jdbc/appDS</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
4、如何应用
1)在法度中应用
Context initContext = new InitialContext();
Context ctx = (Context)initContext.lookup(java:/comp/env);
DataSource ds = (DataSource)ctx.lookup(jdbc/appDS);
Connection conn = ds.getConnection(); //数据操纵
//...
//封闭资料
2)在spring设备文件中应用
<bean id=dataSource class=org.springframework.jndi.JndiObjectFactoryBean>
<property name=jndiName><value>java:comp/env/jdbc/appDS</value></property>
</bean>
无论对感情还是对生活,“只要甜不要苦”都是任性而孩子气的,因为我们也不完美,我们也会伤害人。正因为我们都不完美,也因为生活从不是事事如意,所以对这些“瑕疵”的收纳才让我们对生活、对他人的爱变得日益真实而具体。—— 汪冰《世界再亏欠你,也要敢于拥抱幸福》




