配置数据库连接池Tomcat5.5+Oracle

1。首先配置Tomcat下面Conf下面的server.xml文件,如下:

<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false">
<[color=red]Context [/color]path="/myweb" docBase="myweb" debug="5" reloadable="true" crossContext="true">
<Resource
name="jdbc/ds"
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:test" />

[color=red]</Context>[/color]
</Host>

2.web.xml文件中要配置如下信息:
[color=red]<resource-ref>[/color]
<res-ref-name>jdbc/ds</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
[color=red]</resource-ref>[/color]

3.编写一个Servlet开始测试。
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

Connection conn = null;
PrintWriter out=response.getWriter();

Statement stm=null;
ResultSet rs = null;
try {
[color=red]Context context = new InitialContext();

Context envContext=(Context)context.lookup("java:/comp/env");[/color]
DataSource dataSource = (DataSource) envContext.lookup("jdbc/ds");

//获得连接
conn = dataSource.getConnection();
stm = conn.createStatement();
rs = stm.executeQuery("select sname from stu");
while (rs.next()) {
out.println( rs.getString(1));
}
} catch (Exception e) {

e.printStackTrace();
}
out.println(conn);
}


4.总结:
我也是新手。在这个过程中,也遇到了好多其他的麻烦,以后望各位前辈多多指教。

下面是截止目前我的理解:
[color=red] 1》为什么要用连接池?[/color] 每开始一个java应用事务的时候,就要创建一个数据库连接,即Connection,那么这样频繁的创建数据库连接,会大大的削减整个应用的效率。
为了避免以上的缺点,所以采用连接池。
[color=red] 2》那么连接池到底是什么原理?能使得提高效率呢?[/color] 一般是在一个应用启动的时候,预先创建好了一定数量的数据库连接,当需要使用数据库连接的时候,在这个连接池中取出一个连接,当Java应用执行完事务,再将连接放回连接池。
这样的话,池中的那些数量的数据库连接就可以被重复使用。提高了效率。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值