Tomcat 5.0.28 MySQL 5 串連池的建立)

來源:互聯網
上載者:User
 作者tag:tomcat mysql 

 Tomcat 5.0.28 MySQL 5 串連池的建立參考 http://tomcat.apache.org/tomcat-5.0-doc/jndi-datasource-examples-howto.html

1.下載MYSQL的JDBC驅動
    http://mysql.mirror.vmmatrix.net/Downloads/Connector-J/mysql-connector-java-3.1.13.tar.gz
    解壓後將裡面的jar檔案拷貝到tomcat的lib中
   
    cd Downloads
    tar xvzf mysql-connector-java-3.1.13
    sudo cp mysql-connector-java-3.1.13 /opt/tomcat/common/lib

2.配置server.xml

        <Host name="localhost" debug="0" appBase="webapps"
       unpackWARs="true" autoDeploy="true"
       xmlValidation="false" xmlNamespaceAware="false">

<!--配置串連池-->
<Context path="/zhaobiao" docBase="/media/workspace/j2ee/project/zhaobiao" debug="0" reloadable="true">
  <Resource name="jdbc/zhaobiao"      auth="Container"   type="javax.sql.DataSource"/>
  <ResourceParams name="jdbc/zhaobiao">
    <parameter>
      <name>factory</name>
      <value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
    </parameter>
    <parameter><name>maxActive</name> <value>10</value>   </parameter>
    <parameter><name>maxIdle</name> <value>30</value> </parameter>
    <parameter><name>maxWait</name> <value>5000</value> </parameter>
    <parameter><name>username</name>  <value>chenzhixin</value>  </parameter>
    <parameter><name>password</name> <value>19851026</value>   </parameter>
    <parameter><name>driverClassName</name> <value>com.mysql.jdbc.Driver</value></parameter>
    <parameter>
        <name>url</name>
        <value>jdbc:mysql://localhost:3306/zhaobiao?autoReconnect=true</value>
    </parameter>
  </ResourceParams>
</Context>

<Resource name="jdbc/zhaobiao"中的zhaobiao是我的資料庫名稱

3.配置你的工作目錄的web.xml

   在</web-app>前添加
    <resource-ref>
        <description>Connection pools </description>
        <res-ref-name>jdbc/zhaobiao</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
    </resource-ref>

4.測試
   
    <html>
<%@ page import="javax.sql.*,java.sql.*,javax.naming.*" %>
<body>
<%
    try{
        Context ctx=new InitialContext();
        Context envCtx=(Context)ctx.lookup("java:comp/env");
        DataSource ds=(DataSource)envCtx.lookup("jdbc/zhaobiao");
       
        Connection conn=ds.getConnection();
        Statement st=conn.createStatement();
        ResultSet rs=st.executeQuery("select * from maker");
       
        while(rs.next()){out.println(rs.getString(2));}
    }catch(Exception e){out.println(e);}
%>
</body>
</html>

5.重新啟動Tomcat,輸入 http://localhost/zhaobiao/connectionPools.jsp
   

Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=938014

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.