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

          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為對應的數據庫名
                        也可以換成jdbc:mysql://localhost/xscj        
                   3.JDBC Driver Class : com.mysql.jdbc.Driver   jdk中自帶
                 4.User Name:為Mysql中數據庫管理員名

          3. 下面是最關鍵的一點  

          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>

          應相應的字段真加對了就可以了。

          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");
                            //這里的數據庫前文提及的Data Source URL配置里包含的數據庫。
                     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("我的測試結束");
             }
             catch(Exception ex){
                 out.print("出現例外,信息是:”+ ex.getMessage());
              ex.printStackTrace();
             }
          %>
          </head>
          <body>
          </body>
          </html>

          總結:以上步驟均十分關鍵,如果有誤對應錯誤如下

          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

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

          FeedBack:
          # re: Tomcat5.5.12 + mysql5.0 +Eclipse連接池配置
          2006-05-17 17:35 | 賭東道達到
          怎么解決啊  回復  更多評論
            
          主站蜘蛛池模板: 赤城县| 靖州| 阜阳市| 綦江县| 普洱| 宁化县| 齐河县| 界首市| 安阳县| 吉安市| 阿拉善盟| 林甸县| 甘德县| 扶绥县| 永丰县| 甘谷县| 博爱县| 东港市| 遂宁市| 呼伦贝尔市| 黎城县| 山东省| 乌鲁木齐市| 平阳县| 大渡口区| 商洛市| 云林县| 军事| 额敏县| 德令哈市| 西青区| 麦盖提县| 囊谦县| 寻甸| 思南县| 宁陵县| 高淳县| 固阳县| 凤冈县| 马鞍山市| 开鲁县|