JavaLife
          天之道,利而不害;圣人之道,為而不爭;信言不美,美言不信;善者不辯,辯者不善;知者不博,博者不知!
          posts - 8,comments - 15,trackbacks - 0
          近日學(xué)習(xí)jsp時,為連接池的問題所困,經(jīng)過一番努力,終于成功了,特此為大家獻上。

          1.Tomcat5.5.12中沒有admin模塊,需要讀者自行下載

          2.Tomcat中配置如下
                  JNDI Name:    jdbc/mysql
                      Data Source URL:  jdbc:mysql://202.118.133.88/xscj  
                      JDBC Driver Class:   org.gjt.mm.mysql.Driver
                      User Name:    root
                      Password:     ********
                      Max. Active Connections:    4
                      Max. Idle Connections:    2
                      Max. Wait for Connection:    500
                      Validation Query:  
              注:
                  1.jdbc/mysql 前綱的jdbc也可以換成其它的, mysql為連池名,可任意起,在下文中注意使用
                    2.jdbc:mysql://192.168.0.16/xscj   xscj為對應(yīng)的數(shù)據(jù)庫名
                        也可以換成jdbc:mysql://localhost/xscj        
                   3.JDBC Driver Class : com.mysql.jdbc.Driver   jdk中自帶
                 4.User Name:為Mysql中數(shù)據(jù)庫管理員名

          3. 下面是最關(guān)鍵的一點  

          D:\Tomcat 5.5\conf\context.xml

          <!-- 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="" />
              -->
          <Resource
              name="mysql/xscj"
              type="javax.sql.DataSource"
              password="kingsoft88"
              driverClassName="com.mysql.jdbc.Driver"
              maxIdle="2"
              maxWait="5000"
              username="root"
              url="jdbc:mysql://202.118.133.88:3306/xscj"
              maxActive="4"/>

          </Context>

          應(yīng)相應(yīng)的字段真加對了就可以了。

          4.在Eclipse中編譯時加入Tomcat 的DBCP和Pool包就不會有問題了.
          測試程序如下:
          <html>
          <head>
          <title></title>
          <%
             out.print("開始測試:"+"<br/>");
             DataSource ds = null;
             Connection con=null;
             try{  
                     Context initCtx = new InitialContext();
                     Context ctx = (Context) initCtx.lookup("java:comp/env");
                            //這里的數(shù)據(jù)庫前文提及的Data Source URL配置里包含的數(shù)據(jù)庫。
                     ds = (DataSource)ctx.lookup("jdbc/xscj");
                     con=ds.getConnection();
             Statement stmt = con.createStatement();
             String strSql = "select * from xs";                       //表中的字段讀者自行添加
             ResultSet rs = stmt.executeQuery(strSql);
             while(rs.next()){
                out.print(rs.getString(1)+"<br/>");                
               }
                  rs.close();
                  stmt.close();
                 con.close();        
              out.print("我的測試結(jié)束");
             }
             catch(Exception ex){
                 out.print("出現(xiàn)例外,信息是:”+ ex.getMessage());
              ex.printStackTrace();
             }
          %>
          </head>
          <body>
          </body>
          </html>

          總結(jié):以上步驟均十分關(guān)鍵,如果有誤對應(yīng)錯誤如下

          1、第一步錯誤,報錯
          org.apache.commons.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null'

          2、第三步錯誤,報錯
          javax.naming.NameNotFoundException: Name jdbc is not bound in this Context

          3、第四步錯誤,報錯
          java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Error establishing socket

          如果有上述錯誤,請檢查對應(yīng)步驟是否正確實施
          posted on 2006-02-27 19:44 JavaLife 閱讀(539) 評論(1)  編輯  收藏 所屬分類: Eclipse 、Tomcat

          FeedBack:
          # re: Tomcat5.5.12 + mysql5.0 +Eclipse連接池配置
          2006-05-17 17:35 | 賭東道達到
          怎么解決啊  回復(fù)  更多評論
            
          主站蜘蛛池模板: 元阳县| 莱阳市| 博乐市| 旬阳县| 长顺县| 卢龙县| 奈曼旗| 喀喇沁旗| 津市市| 民乐县| 都兰县| 南木林县| 宁强县| 新闻| 曲靖市| 屯昌县| 余干县| 任丘市| 卓尼县| 民县| 阿克| 安义县| 二连浩特市| 安仁县| 鞍山市| 兴安县| 疏附县| 调兵山市| 凯里市| 延长县| 静安区| 天水市| 巨鹿县| 齐河县| 广州市| 四会市| 天门市| 安远县| 磴口县| 疏勒县| 五华县|