1130136248wlxk

Tomcat中配置MySQL数据库连接池

Web开发中与数据库的连接是必不可少的,而数据库连接池技术很好的优化了动态页与数据库的连接,相比单个连接数据库连接池节省了很大的资源。用一个通俗的比喻:如果一个人洗澡需花一桶水,那一百个人就要花一百桶水,太浪费了.如果都在池子里洗,洗多少个人都不怕了。

1.将MySQL的JDBC驱动复制到Tomcat安装目录里的lib文件夹下。驱动可以从MySQL官网上下载,为jar包。

2.将Tomcat的配置文件Context.xml做如下修改:

<Context>

</Context>

之间添加

 

    <Resource name="jdbc/mysql"      
auth="Container"      
    type="javax.sql.DataSource"      
    driverClassName="com.mysql.jdbc.Driver"      
    url="jdbc:mysql://192.168.1.100:3306/state"      
    username="root"      
    password=""      
    maxActive="1000"      
    maxIdle="20"      
    maxWait="10000" />

注意代码中红色部分:mysql 改为自己的项目路径;state改为自己的数据源名,但是后面使用时候要与这里的配置保持一致;javauser和 javauser改为自己MySQL的用户名密码;url的格式依次为jdbc:mysql://{你的数据库服务所在的IP,如果为本机就为localhost}:{你的数据库服务端口号}/{MySQL中要使用的数据库名称}?autoReconnect=true  。

3.修改项目WEB-INF/web.xml 配置文件(若无,请新建),在“</web-app>”之上添加如下代码:

  <resource-ref>
      <description>DB Connection</description>
      <res-ref-name>jdbc/mysql</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <res-auth>Container</res-auth>
  </resource-ref>

上步中若修改了数据源名此步中红色部分请保持与上步中的一致。

4.代码示例:index.jsp测试界面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ page import="javax.sql.DataSource"%>
        <%@ page import="javax.naming.Context"%>
            <%@ page import="java.sql.Connection"%>
        <%@ page import="java.sql.ResultSet"%>
            <%@ page import="java.sql.Statement"%>
        <%@ page import="javax.naming.InitialContext"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
Context initContext = new InitialContext();
Context envContext  = (Context)initContext.lookup("java:/comp/env");
DataSource ds = (DataSource)envContext.lookup("jdbc/mysql");
Connection conn = ds.getConnection();
Statement st = null;
ResultSet rs = null;
st = conn.createStatement();
String sql="select *from recorder where descid like \'%tongcheng%\'";
System.out.println(sql);
st = conn.createStatement();
rs = st.executeQuery(sql);
while(rs.next()){
    System.out.println(rs.getString(1));
}
%>
</body>
</html>

通过1-3步就在Tomcat中配置好了MySQL的数据库连接池。

 

运行输出结果

 

select *from recorder where descid like \'%tongcheng%\'
bbs    tongcheng    http://jiaoyou.58.com/ay/mm/18-50/,1446828308831,100000000
bbs    tongcheng    http://jiaoyou.58.com/ay/mm/18-50/,1445828308831,100000000
bbs    tongcheng    http://jiaoyou.58.com/ay/mm/18-50/,1444828308831,100000000
bbs    tongcheng    http://jiaoyou.58.com/ay/mm/18-50/,1443828308831,100000000
bbs    tongcheng    http://jiaoyou.58.com/ay/mm/18-50/,1442828308831,100000000
bbs    tongcheng    http://jiaoyou.58.com/ay/mm/18-50/,1441828308831,100000000
bbs    tongcheng    http://jiaoyou.58.com/ay/mm/18-50/,1440828308831,100000000
bbs    tongcheng    http://jiaoyou.58.com/ay/mm/18-50/,1439828308831,100000000
bbs    tongcheng    http://jiaoyou.58.com/ay/mm/18-50/,1438828308831,100000000
bbs    tongcheng    http://jiaoyou.58.com/ay/gg/18-50/,1447828309279,100000000
bbs    tongcheng    http://jiaoyou.58.com/ay/gg/18-50/,1446828309279,100000000
bbs    tongcheng    http://jiaoyou.58.com/ay/gg/18-50/,1445828309279,100000000
bbs    tongcheng    http://jiaoyou.58.com/ay/gg/18-50/,1444828309279,100000000
bbs    tongcheng    http://jiaoyou.58.com/ay/gg/18-50/,1443828309279,100000000
bbs    tongcheng    http://jiaoyou.58.com/ay/gg/18-50/,1442828309279,100000000
bbs    tongcheng    http://jiaoyou.58.com/ay/gg/18-50/,1441828309279,100000000
bbs    tongcheng    http://jiaoyou.58.com/ay/gg/18-50/,1440828309279,100000000
bbs    tongcheng    http://jiaoyou.58.com/ay/gg/18-50/,1439828309279,100000000
bbs    tongcheng    http://jiaoyou.58.com/ay/gg/18-50/,1438828309279,100000000
bbs    tongcheng    http://jiaoyou.58.com/anqing/mm/18-50/,1447828309648,100000000
bbs    tongcheng    http://jiaoyou.58.com/anqing/mm/18-50/,1446828309648,100000000
bbs    tongcheng    http://jiaoyou.58.com/anqing/mm/18-50/,1445828309648,100000000
bbs    tongcheng    http://jiaoyou.58.com/anqing/mm/18-50/,1444828309648,100000000
bbs    tongcheng    http://jiaoyou.58.com/anqing/mm/18-50/,1443828309648,100000000
bbs    tongcheng    http://jiaoyou.58.com/anqing/mm/18-50/,1442828309648,100000000
bbs    tongcheng    http://jiaoyou.58.com/anqing/mm/18-50/,1441828309648,100000000
bbs    tongcheng    http://jiaoyou.58.com/anqing/mm/18-50/,1440828309648,100000000
bbs    tongcheng    http://jiaoyou.58.com/anqing/mm/18-50/,1439828309648,100000000
bbs    tongcheng    http://jiaoyou.58.com/anqing/mm/18-50/,1438828309648,100000000
bbs    tongcheng    http://jiaoyou.58.com/anqing/gg/18-50/,1447828309997,100000000
bbs    tongcheng    http://jiaoyou.58.com/anqing/gg/18-50/,1446828309997,100000000
bbs    tongcheng    http://jiaoyou.58.com/anqing/gg/18-50/,1445828309997,100000000
bbs    tongcheng    http://jiaoyou.58.com/anqing/gg/18-50/,1444828309997,100000000
bbs    tongcheng    http://jiaoyou.58.com/anqing/gg/18-50/,1443828309997,100000000
bbs    tongcheng    http://jiaoyou.58.com/anqing/gg/18-50/,1442828309997,100000000
bbs    tongcheng    http://jiaoyou.58.com/anqing/gg/18-50/,1441828309997,100000000
bbs    tongcheng    http://jiaoyou.58.com/anqing/gg/18-50/,1440828309997,100000000
bbs    tongcheng    http://jiaoyou.58.com/anqing/gg/18-50/,1439828309997,100000000
bbs    tongcheng    http://jiaoyou.58.com/anqing/gg/18-50/,1438828309997,100000000
bbs    tongcheng    http://jiaoyou.58.com/ankang/mm/18-50/,1447828310465,100000000
bbs    tongcheng    http://jiaoyou.58.com/ankang/mm/18-50/,1446828310465,100000000

分类:

技术点:

相关文章:

  • 2021-05-20
  • 2022-01-08
  • 2022-12-23
  • 2021-07-12
  • 2021-05-15
  • 2021-06-30
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-11-19
  • 2022-01-08
  • 2021-07-18
  • 2022-02-12
  • 2022-12-23
  • 2022-01-08
相关资源
相似解决方案