eclipse中tomcat的配置

1.把DataSource设置到我们的WEB项目中,下面详细的介绍下:
第一步:在我们的WEB项目中的META-INF文件夹下建立一个context.xml

Xml代码

<?xmlversion='1.0'encoding='utf-8'?> 

<Context> 

<Resourcename="jdbc/mysql"     

    auth="Container"     

    type="javax.sql.DataSource"     

    driverClassName="com.mysql.jdbc.Driver"     

    url="jdbc:mysql://localhost:3306/dch"     

    username="root"     

    password="root"     

    maxActive="50"     

    maxIdle="20"     

    maxWait="10000"/>     

</Context>

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

 

<Context>

 

    <Resourcename="jdbc/mysql"  

       auth="Container"  

      type="javax.sql.DataSource"  

      driverClassName="com.mysql.jdbc.Driver"  

      url="jdbc:mysql://localhost/bbs"  

       username="root"  

       password="root"  

       maxActive="50"  

       maxIdle="20"  

       maxWait="10000" />  

 

</Context>


第二步:在我们的WEB项目下的WEB-INF文件夹下建立一个web.xml(如果存在了就不用了,直接修改就行了)
(这几天测试了一下,不做这步也可以,O(∩_∩)O哈哈~省事了)

Xml代码

  1. <resource-ref>  
  2.     <description>DB Connection</description>  
  3.     <res-ref-name>jdbc/mysql</res-ref-name>  
  4.     <res-type>javax.sql.DataSource</res-type>  
  5.     <res-auth>Container</res-auth>  
  6. </resource-ref>  

  <resource-ref>

      <description>DB Connection</description>

     <res-ref-name>jdbc/mysql</res-ref-name>

     <res-type>javax.sql.DataSource</res-type>

     <res-auth>Container</res-auth>

  </resource-ref>


第三步:我们就可以用代码来获取Connection对象了

 

<%@pagecontentType="text/html;charset=utf-8"%>

<%@ pageimport ="java.sql.*"%>

<%@ pageimport = "javax.sql.*"%>

<%@ pageimport ="javax.naming.*"%>

 

<html>

<head>

<title></title>

</head>

<body>

<%

    DataSource ds = null;

    Context env = null;

    Connection conn = null;

    Statement stmt=null;

    ResultSet rs=null;

    String sql="select *from person";

    try

    {

        System.out.println(System.getProperty("java.naming.factory.initial"));

        env = (Context) new InitialContext().lookup("java:comp/env"); 

        ds = (DataSource) env.lookup("jdbc/mysql");

        conn = ds.getConnection();

       stmt=conn.createStatement();

       rs=stmt.executeQuery(sql);

        while(rs.next())

        {

        System.out.println("ID>>"+rs.getInt("id")+"  name>>"+rs.getString("name")+"  password>>"+rs.getString("password"));

        }

    }

    catch (Exception e)

    {

        e.printStackTrace();

    }

%>

 

 

</body>

package xushun.util;

 

import java.sql.*;

import javax.sql.*;

import javax.naming.*;

 

public class DBHelper {

   

    public static Connection getConnection()throws SQLException,NamingException

    {

        // 初始化查找命名空间

        Context initContext = newInitialContext();

        Context envContext =(Context)initContext.lookup("java:/comp/env");

        // 找到DataSource

        DataSource ds =(DataSource)envContext.lookup("jdbc/mysql");

        return ds.getConnection();

    }

}


2.把DataSource设置到我们的Tomcat中,下面详细的介绍下(测试用的JAVA代码和上面的一样就不帖出了):
这里我查到的设置方法就有了一点区别了。有的人把DataSource设置在Tomcat的server.xml文件的GlobalNamingResources下面,然后在context.xml中去映射。有的直接就写在context.xml中了
先说下在server.xml添加DataSource
第一步:在Tomcat的conf中的server.xml文件中找到

Xml代码

  1. <GlobalNamingResources>  
  2.   <!-- Editable user database that can also be used by   
  3.        UserDatabaseRealm to authenticate users   
  4.   -->  
  5.   <Resource name="UserDatabase" auth="Container"  
  6.             type="org.apache.catalina.UserDatabase"  
  7.             description="User database that can be updated and saved"  
  8.             factory="org.apache.catalina.users.MemoryUserDatabaseFactory"  
  9.             pathname="conf/tomcat-users.xml" />  
  10. </GlobalNamingResources>  

  <GlobalNamingResources>

    <!-- Editable user database that canalso be used by

         UserDatabaseRealm to authenticateusers

    -->

    <Resource name="UserDatabase"auth="Container"

             type="org.apache.catalina.UserDatabase"

              description="User databasethat can be updated and saved"

             factory="org.apache.catalina.users.MemoryUserDatabaseFactory"

             pathname="conf/tomcat-users.xml" />

  </GlobalNamingResources>

修改为

Xml代码

  1. <GlobalNamingResources>  
  2.   <!-- Editable user database that can also be used by   
  3.        UserDatabaseRealm to authenticate users   
  4.   -->  
  5.   <Resource name="UserDatabase" auth="Container"  
  6.             type="org.apache.catalina.UserDatabase"  
  7.             description="User database that can be updated and saved"  
  8.             factory="org.apache.catalina.users.MemoryUserDatabaseFactory"  
  9.             pathname="conf/tomcat-users.xml" />  
  10.   <Resource name="jdbc/bbs"        
  11.          auth="Container" type="javax.sql.DataSource"  
  12.          driverClassName="com.mysql.jdbc.Driver"  
  13.          maxIdle="20"  
  14.          maxWait="5000"  
  15.          username="root"  
  16.          password="admin"  
  17.          url="jdbc:mysql://localhost:3306/bbs"        
  18.          maxActive="100"    
  19.          removeAbandoned="true"  
  20.          removeAbandonedTimeout="60"  
  21.          logAbandoned="true"/>  
  22. </GlobalNamingResources>  

  <GlobalNamingResources>

    <!-- Editable user database that canalso be used by

         UserDatabaseRealm to authenticateusers

    -->

    <Resource name="UserDatabase"auth="Container"

              type="org.apache.catalina.UserDatabase"

              description="User databasethat can be updated and saved"

             factory="org.apache.catalina.users.MemoryUserDatabaseFactory"

             pathname="conf/tomcat-users.xml" />

    <Resource name="jdbc/bbs"    

           auth="Container" type="javax.sql.DataSource"

           driverClassName="com.mysql.jdbc.Driver"

           maxIdle="20"

           maxWait="5000"

           username="root"

           password="admin"

           url="jdbc:mysql://localhost:3306/bbs"    

           maxActive="100"

           removeAbandoned="true"

           removeAbandonedTimeout="60"

           logAbandoned="true"/>

  </GlobalNamingResources>


第二步:在Tomcat的conf文件夹下的context.xml中加入

Xml代码

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

<ResourceLinkname="jdbc/bbs" global="jdbc/bbs" type="javax.sql.DataSource"/>


第三步:就是在WEB项目的WEB-INF中的web.xml添加

Xml代码

  1. <resource-ref>  
  2.     <description>DB Connection</description>  
  3.     <res-ref-name>jdbc/mysql</res-ref-name>  
  4.     <res-type>javax.sql.DataSource</res-type>  
  5.     <res-auth>Container</res-auth>  
  6. </resource-ref>  

  <resource-ref>

      <description>DBConnection</description>

      <res-ref-name>jdbc/mysql</res-ref-name>

     <res-type>javax.sql.DataSource</res-type>

     <res-auth>Container</res-auth>

  </resource-ref>


还有就是在Tomcat文档中提到的方法,直接修改context.xml文件了
在Tomcat的conf文件夹下的context.xml中加入

Xml代码

  1. <Resource name="jdbc/bbs"        
  2.               auth="Container" type="javax.sql.DataSource"  
  3.               driverClassName="com.mysql.jdbc.Driver"  
  4.               maxIdle="20"  
  5.               maxWait="5000"  
  6.               username="root"  
  7.               password="admin"  
  8.               url="jdbc:mysql://localhost:3306/bbs"        
  9.               maxActive="100"    
  10.               removeAbandoned="true"  
  11.               removeAbandonedTimeout="60"  
  12.               logAbandoned="true"/>  

<Resourcename="jdbc/bbs"    

            auth="Container"type="javax.sql.DataSource"

           driverClassName="com.mysql.jdbc.Driver"

           maxIdle="20"

           maxWait="5000"

           username="root"

           password="admin"

           url="jdbc:mysql://localhost:3306/bbs"    

            maxActive="100"

           removeAbandoned="true"

           removeAbandonedTimeout="60"

           logAbandoned="true"/>

然后就是在WEB项目的WEB-INF中的web.xml添加

Xml代码

  1. <resource-ref>  
  2.     <description>DB Connection</description>  
  3.     <res-ref-name>jdbc/mysql</res-ref-name>  
  4.     <res-type>javax.sql.DataSource</res-type>  
  5.     <res-auth>Container</res-auth>  
  6. </resource-ref>  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值