天天看點

tomcat 配置ORACLE XE資料庫連接配接池

1、把相關的jar檔案複制到tomcat/lib目錄中。

主要就是jdbc/lib下的jar包,還有LIB下的jar包。

2、修改工程目錄的WebRoot/WEB-INF/web.xml檔案,加入下面的配置:

<resource-ref>

     <description>Oracle Datasource example</description>

     <res-ref-name>jdbc/oracle</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/oracle"

              auth="Container"

               type="javax.sql.DataSource"

              driverClassName="oracle.jdbc.driver.OracleDriver"

              url="jdbc:oracle:thin:@localhost:1521:XE"

              username="mss"

              password="mss"

              maxActive="50"

              maxIdle="2"

              maxWait="10000" />

</Context>

附一個圖來說明這兩個檔案的位置。

tomcat 配置ORACLE XE資料庫連接配接池

配置就這麼多了,下面寫簡單寫個測試代碼

package com.asi.mss.Utility;

import java.sql.*;

import javax.naming.*;

import javax.sql.*;

import org.apache.commons.logging.*;

public class OracleClient

{

    private static Log log = LogFactory.getLog(OracleClient.class);

    public static Connection getConnection()

    {

        Context initContext;

        Context envContext;

        DataSource ds;

        try

        {

            initContext = new InitialContext();

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

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

            Connection conn    = ds.getConnection();

            return conn;

        }

        catch (NamingException e)

        {

            log.info(e.getMessage());

            e.printStackTrace();

            return null;

        }     

        catch (SQLException e)

        {

            log.info(e.getMessage());

            e.printStackTrace();

            return null;

        }        

    }

}

Over,收隊。

二、傳統的連接配接池配制方法:

1.在tomcat的安裝目錄下修改conf檔案下的server.xml檔案在server.xml中的之間增加以下配置:

       < Context path="/liangku" docBase="liangku"

               debug="5" reloadable="true" crossContext="true">

               < Resource name="jdbc/DBUtil" auth="Container" type="javax.sql.DataSource"

               maxActive="100" maxIdle="30" maxWait="10000"

               username="sa" password="" driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver"

               url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=barn" />

      < /Context>

2.在webapps/項目名/WEB-INF中的web.xml中之前增加以下配置:

      < resource-ref>

         < description>SQL Server Datasource

         < res-ref-name>jdbc/DBUtil

        < res-type>javax.sql.DataSource

        < res-auth>Container

< /resource-ref>        

3.把以下幾個.jar檔案複制到tomcat安裝目錄下的lib檔案夾中(jar可到網上下載下傳)

      commons-collections-3.1.jar

      commons-dbcp-1.2.1.jar

     commons-pool-1.2.jar

     msutil.jar

     msbase.jar

      mssqlserver.jar     

4.例子:      

import java.sql.Connection;

import javax.naming.Context;

import javax.naming.InitialContext;

import javax.sql.DataSource;

public class DBUtil {

public static Connection getConnection() {

   try{

            Context initContext = new InitialContext();

            if(initContext == null )

                throw new Exception("No Context");

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

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

            if (ds != null) {

                Connection conn = ds.getConnection();     

                if(conn != null) {

                    return conn;

                }

            }

        }catch(Exception e) {

            e.printStackTrace();

        }

        return null;

}

public static void main(String[] args) {

   Connection con = DBUtil.getConnection();

   if(con == null){

    System.out.println("連接配接不成功");  

   }

   else{

    System.out.println("連接配接成功");

   }

}