狂淘

          www.kuangtao.net

             :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            6 隨筆 :: 185 文章 :: 68 評(píng)論 :: 0 Trackbacks
          最近在網(wǎng)上看到的java+jsp+ tomcat6+ mysql 連接池大多數(shù)是tomcat5 的,很多都說得不詳細(xì)

          1.需要的文件:mysql-5.0.27-win32.zip(安裝文件),mysql-connector-java-5.0.4-bin.jar(連接驅(qū)動(dòng)程序),apache-tomcat-6.0.10.exe(安裝文件)

          2.配置tomcat下的conf下的context.xml文件,在<context></context>之間添加連接池如下:

          <Resource name="jdbc/mysql"  

                 auth="Container"  

                type="javax.sql.DataSource"  

               driverClassName="com.mysql.jdbc.Driver"   
               url="jdbc:mysql://localhost/test"  

               username="root"  

               password="root"  

               maxActive="100"  

               maxIdle="30"  

               maxWait="10000" />  

          3.配置你的應(yīng)用下的web.xml中的<web-app></web-app>之間加入:  xml 代碼

          <resource-ref>  

              <description>DB Connection</description>  

              <res-ref-name>jdbc/mysqlx</res-ref-name>  

              <res-type>javax.sql.DataSource</res-type>  

              <res-auth>Container</res-auth>  

            </resource-ref>  

          4.大功告成,不用在原來的server.xml里面配置了,下面就可以編寫測(cè)試程序了,這個(gè)網(wǎng)上就很多了,主要的就上面,當(dāng)然要把連接驅(qū)動(dòng)程序都放到tomcat6下的lib下面.測(cè)試代碼如下:


          java 代碼


        1. <!doctype html public "-//w3c//dtd html 4.0 transitional//en"    
        2.   
        3. "http://www.w3.org/TR/REC-html40/strict.dtd">   
        4.   
        5. <%@ page import="java.sql.*"%>   
        6.   
        7. <%@ page import="javax.sql.*"%>   
        8.   
        9. <%@ page import="javax.naming.*"%>   
        10.   
        11. <%@ page session="false" %>   
        12.   
        13. <html>   
        14.   
        15. <head>   
        16.   
        17. <meta http-equiv="Content-Type" content="text/html; charset=gb2312">   
        18.   
        19. <title></title>   
        20.   
        21. <%    
        22.   
        23.    out.print("我的測(cè)試開始");   
        24.   
        25.    DataSource ds = null;   
        26.   
        27.    try{   
        28.   
        29.    InitialContext ctx=new InitialContext();   
        30.   
        31.    ds=(DataSource)ctx.lookup("java:comp/env/jdbc/mysql");   
        32.   
        33.    Connection conn = ds.getConnection();   
        34.   
        35.    Statement stmt = conn.createStatement();   
        36.   
        37.      //提示:users必須是數(shù)據(jù)庫已有的表,   
        38.   
        39. //這里的數(shù)據(jù)庫前文提及的Data Source URL配置里包含的數(shù)據(jù)庫。   
        40.   
        41.    String strSql = " select * from users";   
        42.   
        43.    ResultSet rs = stmt.executeQuery(strSql);   
        44.   
        45.    while(rs.next()){   
        46.   
        47.       out.print(rs.getString(1));                    
        48.   
        49.      }   
        50.   
        51. out.print("我的測(cè)試結(jié)束");   
        52.   
        53.    }   
        54.   
        55.    catch(Exception ex){   
        56.   
        57.        out.print(“出現(xiàn)例外,信息是:”+ex.getMessage());   
        58.   
        59.     ex.printStackTrace();   
        60.   
        61.    }   
        62.   
        63. %>   
        64.   
        65. </head>   
        66.   
        67. <body>   
        68.   
        69. </body>   
        70.   
        71. </html>   
        72. posted on 2009-10-09 10:13 狂淘 閱讀(175) 評(píng)論(0)  編輯  收藏 所屬分類: jsp項(xiàng)目 傳到服務(wù)器問題
          主站蜘蛛池模板: 鄂伦春自治旗| 新丰县| 满城县| 东安县| 武山县| 宜宾市| 滨州市| 翁源县| 新建县| 亚东县| 镇巴县| 楚雄市| 九龙县| 略阳县| 绵竹市| 吉安县| 安徽省| 武清区| 大理市| 大足县| 罗江县| 高尔夫| 三江| 武义县| 东台市| 水城县| 荥阳市| 桃园县| 清流县| 曲周县| 鄢陵县| 长顺县| 岳阳县| 开阳县| 厦门市| 夏津县| 上饶市| 宝应县| 太仆寺旗| 潜山县| 凤台县|