JNDI数据库连接池配置

Tomcat 目录下conf中server.xml中配置:


<Context path="/demo" docBase="项目本地路径\WebRoot" reloadable="true">
<Resource name="jdbc/jsp05"
scope="Shareable"
auth="Container"
type="javax.sql.DataSource"
factory="org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory"
url="jdbc:mysql://127.0.0.1:3306/test"
driverClassName="com.mysql.jdbc.Driver"
username="root"
password="123"
maxActive="50"
maxIdle="10"
maxWait="-1"/>
</Context>


项目下web.xml中配置:

<resource-ref>
<description>
Resource reference to a factory for java.sql.Connection
instances that may be used for talking to a particular
database that is configured in the server.xml file.
</description>
<res-ref-name>jdbc/jsp05</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>



连接类:

import java.sql.Connection;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class JNDIConnection {
public static Connection getConnection(){
Connection conn = null;
try {
InitialContext initCtx = new InitialContext();
Object obj = initCtx.lookup("java:comp/env/jdbc/jsp05");
DataSource ds = (DataSource)obj;
conn = ds.getConnection();
System.out.println("JNDI连接成功");
return conn;
} catch (NamingException e) {
e.printStackTrace();
} catch (Exception e){
e.printStackTrace();
}
return conn;
}
}




数据库驱动包放在\Tomcat 6.0\lib下
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值