tomcat连接池配置

经过几次修改Tomcat+mysql数据库的连接池的配置终于成功啦!!
各位看官看我将我的配置步骤细细说来,依葫芦画瓢,相信聪明的看官一看就明白:
表及数据的创建就不说了,下面是我的建表和简单的测试数据:
# Host: localhost    Database: mytest
# ------------------------------------------------------
# Server version 5.0.27-community-nt
#
# Table structure for table t1
#
DROP TABLE IF EXISTS `t1`;
CREATE TABLE `t1` (
  `pid` int(11) default NULL,
  `id` int(11) default NULL,
  `gongxu` varchar(10) default NULL
) ENGINE=InnoDB DEFAULT CHARSET=gbk;

INSERT INTO `t1` VALUES (1,1,'印花');
INSERT INTO `t1` VALUES (1,2,'车缝');
INSERT INTO `t1` VALUES (1,3,'绣花');
INSERT INTO `t1` VALUES (1,4,'包装');

首先:
我们来配置Tomcat$/conf下的server.xml文件(所有更改的配置文件先做个备份,原因就不必说啦^_^):
将下面这段话放到<host></host>标签中:
<Host>
<Context path="/Test" doBase="Test" debug="10" reloadable="true">      
<Resource  name="jndi/ds/mysql" type="javax.sql.DataSource" auth="Container" maxVactive="10" maxIdle="2" maxWait="5000" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/mytest?characterEncoding=gbk"
username="root" password="sa"/>
</Context>
......
</Host>
相信这段话的意思,聪明的看官比我明白^_^,各个属性的意思我就不废话啦!
然后:
来配置Tomcat$/conf下的context.xml文件,在<Context></Context>标签中加上一句话就欧拉:
<ResourceLink  name="jndi/ds/mysql" global="jndi/ds/mysql" type="javax.sql.DataSourcer"/>
新安装的Tomcat,这个文件中总共也就这么几句话,索性都copy上:
<!-- The contents of this file will be loaded for each web application -->
<Context>
    <!-- Default set of monitored resources -->
    <WatchedResource>WEB-INF/web.xml</WatchedResource>
    <!-- Uncomment this to disable session persistence across Tomcat restarts -->
    <!--
    <Manager pathname="" />
    -->
<ResourceLink  name="jndi/ds/mysql" global="jndi/ds/mysql" type="javax.sql.DataSourcer"/>
</Context>
到此Tomcat配置文件的配置就完成啦。


下面就是我们手动在Tomcat-5$/webapps下面创建工程了(这个就简单啦):
先建一个Test文件夹
再在其下建上WEB-INF文件夹和index.jsp文件
最后在WEB-INF文件夹下建好lib、classes文件夹和web.xml文件,及将数据库的驱动copy到lib目录下。
好拉建完啦,但是还得动手写点东西才行
web.xml文件中写上这么一段:
<?xml version="1.0" encoding="ISO-8859-1"?>

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-

app_2_4.xsd"
    version="2.4">
    <resource-ref>
      <res-ref-name>jndi/ds/mysql</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <res-auth>Container</res-auth>
    </resource-ref>
</web-app>

index.jsp文件中:
<%@ page contentType="text/html;charset=gbk"%>
<%@ page import="javax.sql.DataSource,java.sql.*,javax.naming.*;"%>

<html>
   
    <head><title>test.jsp</title><head>
       
        <body>
           
            <p>Tomcat Connection Pool Test </p>
           
            <%
              Context initCtx = new InitialContext();
              DataSource ds = (DataSource)initCtx.lookup("java:comp/env/jndi/ds/mysql");
              Connection conn = ds.getConnection();
              out.print("connection successful ! "+conn.getClass().getName()+"<br>");

            Statement stmt=conn.createStatement();
            ResultSet rs=stmt.executeQuery("select * from t1");
            try{
        while(rs.next()){
            out.println(rs.getInt("pid"));
            out.println(rs.getInt("id"));
            out.println(rs.getString("gongxu"));
        }
        }catch(Exception e){e.printStackTrace();}
        finally{
        rs.close();
        stmt.close();
        conn.close();
        }   
            %>
            </body>
    <html>
到此mysql+tomcat的数据源配置就结束啦,相信聪明的看官一定明白了^_^.
启动tomcat测试一下吧,我的测试结果:

test.jsp

Tomcat Connection Pool Test

connection successful !
org.apache.tomcat.dbcp.dbcp.PoolingDataSource$PoolGuardConnectionWrapper
1 1 印花 1 2 车缝 1 3 绣花 1 4 包装


如果出现下面的错误性息:可能是你的数据库驱动包没有加到Tomcat-5$/common/lib目录下。或者其他的错误,比如解释xml文件错误,可能是server.xml、context.xml等配置文件中设置了中文,将中文去掉就ok啦。
org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null'
    at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:780)
    at org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:540)
    at org.apache.jsp.test_jsp._jspService(org.apache.jsp.test_jsp:59)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:322)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:868)
    at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:663)
    at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
    at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
    at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
    at java.lang.Thread.run(Thread.java:595)
Caused by: java.sql.SQLException: No suitable driver
    at java.sql.DriverManager.getDriver(DriverManager.java:243)
    at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:773)
    ... 22 more







  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值