狂淘

          www.kuangtao.net

             :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            6 隨筆 :: 185 文章 :: 68 評論 :: 0 Trackbacks
          最近在網上看到的java+jsp+ tomcat6+ mysql 連接池大多數是tomcat5 的,很多都說得不詳細

          1.需要的文件:mysql-5.0.27-win32.zip(安裝文件),mysql-connector-java-5.0.4-bin.jar(連接驅動程序),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.配置你的應用下的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里面配置了,下面就可以編寫測試程序了,這個網上就很多了,主要的就上面,當然要把連接驅動程序都放到tomcat6下的lib下面.測試代碼如下:


          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("我的測試開始");   
        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必須是數據庫已有的表,   
        38.   
        39. //這里的數據庫前文提及的Data Source URL配置里包含的數據庫。   
        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("我的測試結束");   
        52.   
        53.    }   
        54.   
        55.    catch(Exception ex){   
        56.   
        57.        out.print(“出現例外,信息是:”+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 狂淘 閱讀(176) 評論(0)  編輯  收藏 所屬分類: jsp項目 傳到服務器問題
          主站蜘蛛池模板: 伊金霍洛旗| 维西| 临夏县| 藁城市| 望都县| 高碑店市| 旬阳县| 义乌市| 海晏县| 图木舒克市| 德钦县| 灵丘县| 河东区| 安泽县| 海淀区| 光泽县| 厦门市| 靖宇县| 迁安市| 安多县| 韶山市| 德州市| 唐海县| 资中县| 肇州县| 本溪| 石棉县| 登封市| 莫力| 衡东县| 灵丘县| 陆川县| 鹿泉市| 金华市| 安宁市| 陇南市| 和静县| 客服| 英超| 同心县| 乐都县|