连接池配置-tomcat

两种方式:
一、Tomcat服务器配置:
1、 把 <Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"

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

username="root" password="sa" driverClassName="com.mysql.jdbc.Driver"

url="jdbc:mysql://localhost:3306/employee_manager?autoReconnect=true"/>

粘贴到Tomcat 6.0/conf context.xml 文件中的<Context></Context>

2、把 <resource-ref>

<description>DB Connection</description>

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

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

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

</resource-ref>

粘贴到web.xml 文件中的<web-app></ web-app >

3、把数据库驱动文件放在Tomcat 6.0/lib 中

4、程序中应用:

public static Connection getConnection() {

DataSource ds;

InitialContext cxt ;

try{

cxt = new InitialContext();

ds = (DataSource) cxt.lookup( "java:/comp/env/jdbc/TestDB" );

con = ds.getConnection();

}catch(Exception e){

e.printStackTrace();

}

SqlServer2000和以上步骤一样:但注意

url="jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=employee_manager"

?autoReconnect=true 应该删除!


二、在工程中配置:
1、在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:@127.0.0.1:1521:orcl"
username="scott"
password="tiger"
maxActive="100"
maxIdle="30"
maxWait="10000" />
</Context>
2、使用代码如下:
try{
InitialContext initContext = new InitialContext();
Context context = (Context)initContext.lookup("java:comp/env");
DataSource ds = (DataSource) context.lookup("jdbc/ORACLE");

conn = ds.getConnection();
System.out.println("通过连接池连接到数据库。");
}catch(Exception e){
throw new ServletException(e);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值