什么是数据库连接池?

来源:互联网 发布:手机淘宝的扫一扫在哪 编辑:程序博客网 时间:2024/06/05 15:06

1.数据库连接池实现步骤

《1》copy相对应的数据库jar到Tomcat/lib

《2》添加配置信息到conf/context.xml

<Resource name="jdbc/news" auth="Container"

type="javax.sql.DataSource"

maxActive="100"

maxIdle="30"

maxWait="10000"

username="scott"

password="tiger"

driverClassName="oracle.jdbc.driver.OracleDriver"

url="jdbc:oracle:@localhost:1521:orcl"

/>

<Resource name="jdbc/TestDB"

auth="Container"

type="root"

password="password"

diriverClassName="com.mysql.jdbc.Driver"

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

maxTotal="8"

maxIdle="4"/>

《3》配置web.xml

<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>

《4》测试

Context context=new InitialContext();

Context envContext=(Context)context.lookup("java:/comp/env");

DataSource dataSource=(DataSource)envContext.lookup("jdbc/TestDB");

Connection conn=dataSource.getConnection();

out.print(conn);

原创粉丝点击