数据库连接池实现步骤
<1>copy相对应的数据库jar(mysql-connector-java-5.1.7-bin.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:thin:@localhost:1521:orcl"
/>
<Resource name="jdbc/TestDB"
auth="Container"
type="javax.sql.DataSource"
username="root"
password="password"
driverClassName="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);