数据库连接池
来源:互联网 发布:网络吃土什么意思啊 编辑:程序博客网 时间:2024/06/06 02:55
数据库连接池
1.数据库连接池
WhatWhy C3P0
2.数据库连接池实现步骤
<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);
//连接数据库的代码;
try {
//大池子
Context context=new InitialContext();
//小池子(获取java)
Context s = (Context) context.lookup("java:comp/env");
//获取值
DataSource d=(DataSource) s.lookup("jdbc/zking");
return d.getConnection();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static void setConnct(Connection conn, PreparedStatement ps,ResultSet rs){
try {
if(conn!=null&&!conn.isClosed()){
conn.close();
}
if(ps!=null){
ps.close();
}
if(rs!=null){
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
阅读全文
0 0
- 数据库连接池
- 数据库连接池
- 数据库连接池
- 数据库连接池
- 数据库连接池
- 数据库连接池
- 数据库连接池
- 数据库连接池
- 数据库连接池
- 数据库连接池
- 数据库连接池
- 数据库连接池
- 数据库连接池
- 数据库连接池
- 数据库连接池
- 数据库连接池
- 数据库连接池
- 数据库连接池
- w3svc服务启动 不了,错误 1068:依赖服务或组件无法启动
- windows下 mongo副本集配置 一主一从一仲裁
- 2738 Queue with Template(eden)
- poj 3461
- 简单记载一下,windows和linux 的清屏命令。
- 数据库连接池
- mysql之存储过程全面解析
- Linux for Ubuntu 挂载/访问Windows的共享文件夹
- 2739 Stack with Template (eden)
- Linux下实现Huffman编码压缩算法
- codeforces802 A&&B Heidi and Library (easy&&medium)
- 系统性能调优技术实战分享
- 一、关于IO
- java多线程技术