数据库连接池

来源:互联网 发布:php优势和劣势 知乎 编辑:程序博客网 时间:2024/05/29 00:33

数据库连接池实现步骤

<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: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);

测试的话,输出得到的connection,如果不为null的话,就表示连接成功了,如果不成功,那么你就好好的检查配置和java代码,这个主要是细心,加油!


原创粉丝点击