1、将JDBC的数据库的驱动程序放置在Tomcat的lib目录下;
2、在应用工作的META_INF下创建一个XML文件,如下:
<?xml version="1.0" encoding="utf-8"?>
<Context>
<Resource name="jdbc/工程名"
type="javax.sql.DataSource"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/数据库名"
username="root"
password="root"
maxActive="40"
maxIdle="2"
minIdle="1"
maxWait="500"/>
</Context>
3、在WEB应用的web.xml中加入以下内容:
<resource-ref>
<description>JNDI JDBC DataSource</description>
<res-ref-name>jdbc/工程名</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
4、在程序中使用已配置的连接池:
DataSource ds = null;
Connection conn = null;
Context c = new InitContext();
ds = (BasicDataSource)c.lookup("java:/comp/env/jdbc/工程名");
conn = ds.getConnection();
JSP中的连接池问题
最新推荐文章于 2020-12-31 17:42:24 发布