Tomcat连接池配置步骤

将JDBC插件放入Tomcat的lib目录中

这里写图片描述


修改Tomcat的2个配置文件
(1)修改context.xml

这里写图片描述

在Context的结束标签之前加入:

<Resource 
       name="jdbc/t1" 
       auth="Container" 
       type="javax.sql.DataSource"
       maxActive="100" 
       maxIdle="30" 
       maxWait="10000"
       username="root" 
       password="123456" 
       driverClassName="com.mysql.jdbc.Driver"
       url="jdbc:mysql://localhost:3306/t1"
/>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

我的database名是:t1 
table名是:bnutalk 
用户名是root 
密码是123456 
你只要修改对应的地方就可以了,添加后效果如下: 
这里写图片描述

(2)修改web.xml

这里写图片描述

在webapp的结束标签之前加入:

<resource-ref>
      <description>DB Connection</description>
      <res-ref-name>jdbc/t1</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <res-auth>Container</res-auth>
  </resource-ref>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

将t1改成你的数据库名就行了。 
这里写图片描述



测试是否配置成功
(1)新建一个Serve和一个Dynamic Web Project项目

这里写图片描述

(2)new一个jsp文件,名字任意

这里写图片描述

index.jsp代码如下:

<%@ page language="java" contentType="text/html" 
pageEncoding="GBK" import="java.sql.*,javax.sql.*,javax.naming.*"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%

  Context ctx=new InitialContext();

  Connection conn=null;

  DataSource ds=(DataSource)ctx.lookup("java:comp/env/jdbc/t1");

  conn=ds.getConnection();

  Statement stmt=conn.createStatement();

  ResultSet rs=stmt.executeQuery("select id,age from bnutalk");

  while(rs.next()){

      System.out.println(rs.getInt("id")+","+rs.getInt("age")); 
  }

  rs.close();

  stmt.close();

  conn.close();   

 %>
</body>
</html>
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值