原!!tomcat7.0 配置数据库连接池 SQLServer2000

SQLServer2000所需的3个驱动jar包

msbase.jar
mssqlserver.jar
msutil.jar

放入WEB-INF lib文件夹中

 

1.META-INF 创建一个context.xml文件,内容如下:(我的案例项目)

<Context path="webTest" docBase="webTest"

reloadable="true" crossContext="true">

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

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

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

url="jdbc:microsoft:sqlserver://127.0.0.1:1433;databaseName=webTest"/>

</Context>

 

注意事项:

webTest是数据库名字,username 和password分别是数据库的用户名和密码。

driverClassName:数据库的驱动

url:数据库连接该驱动的地址

maxActive:最大激活连接数

maxIdle:最大保留连接数

maxWait:最大等待时间

 

2.修改项目的web.xml文件 ,添加如下内容:(在<web-app>内)

<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/webTest</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>

注意事项:

webTest:为数据库名字

 

3.测试:

要引入以下两个包:

import javax.sql.*;
import javax.naming.*;
package com.web1.model;
import java.sql.*;
import javax.sql.*;
import javax.naming.*;
public class ConnDB {
    
    private Connection ct=null;
    public Connection getConn()
    {
        /*try {
                Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
                ct=DriverManager.getConnection("jdbc:microsoft:sqlserver://127.0.0.1:1433;databaseName=webTest","wuyun","wuyun");            
                System.out.println("jdbc");
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }*/
        
        //连接池配置      
        try {
            Context initContext = new InitialContext();              
            DataSource ds = (DataSource)initContext.lookup("java:/comp/env/jdbc/webTest");                                    
            ct = ds.getConnection(); 
            System.out.println("连接池 连接成功!");
        } catch (Exception e) {
           e.printStackTrace();
        }    
        return ct;
        
    }    
}

 

我在调试过程中出现的问题:

  DataSource ds = (DataSource)initContext.lookup("java:/comp/env/jdbc/webTest"); 

  The type java.lang.CharSequence cannot be resolved. It is indirectly referenced from required .class files 

解决方法:调整或者添加相应的jre版本。

 

posted @ 2015-09-22 20:15  乌云de博客  阅读(452)  评论(0编辑  收藏  举报