在Tomcat6中配置数据源

本文介绍了如何在Tomcat服务器中配置MySQL数据源,包括设置META-INF/context.xml和WEB-INF/web.xml文件,通过JNDI获取数据源以及连接数据库的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[size=large]1.设置META-INF/context.xml[/size]

<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" path="/datasourcetest">
<Resource name="jdbc/testdb"
auth="Container"
type="javax.sql.DataSource"
username="username"
password="password"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/testdb"
maxActive="8"
maxIdle="4" />
</Context>

[size=large]2.设置WEN-INF/web.xml,添加资源引用[/size]

<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<resource-ref>
<res-ref-name>jdbc/testdb</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
</web-app>

[size=large]3.使用jndi获得数据源[/size]

Context context = new InitialContext();
DataSource source = (DataSource) context.lookup("java:comp/env/jdbc/testdb");
Connection conn = source.getConnection();

[size=large]4.mysql驱动必须放在tomcat/lib中[/size]
记此以备忘
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值