PariScamper的java天空

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            14 Posts :: 0 Stories :: 7 Comments :: 0 Trackbacks

          肯定是tomcat的數據庫連接池沒配置對,給tomcat配置mysql數據庫連接池方法如下
          1,用Tomcat的管理界面建立Data Source,此時會在conf/server.xml生成如下片段:
              <Resource
                name="jdbc/game"
                type="javax.sql.DataSource"
                maxActive="4"
                maxIdle="2"
                username="root"
                maxWait="5000"
                driverClassName="com.mysql.jdbc.Driver"
                password="3306"
                url="jdbc:mysql://localhost:3306/game"/>
          2,在server.xml <Host></Host>之間配置下面代碼:
                  <Context path="/game" debug="5" reloadable="true" crossContext="true">
                   <ResourceLink name="jdbc/game" global="jdbc/game" type="javax.sql.DataSource"/>
                  </Context>
          其中path="/game"是部署供訪問的url

          其中1,2步也可以合為一步在server.xml添加如下代碼,但我沒驗證。
            <Context path="/game" debug="5" reloadable="true" crossContext="true">
                  <Resource name="jdbc/game" auth="Container" type="javax.sql.DataSource"
                  driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/game" username="root" password="3306"
                  maxActive="20" maxIdle="10" maxWait="10000"/>
            </Context>
          3,在project的web.xml的</web-app>前加入 
           <resource-ref>
              <res-ref-name>jdbc/game</res-ref-name>
              <res-type>javax.sql.DataSource</res-type>
              <res-auth>Container</res-auth>
            </resource-ref>

          另外,如果tomcat啟動出現java.lang.NoClassDefFoundError: **********錯誤,有可能是因為不斷的用myeclipse添加library比如Hibernate 3.2 Core Librarys,Spring 2.0 AOP Librarys,Spring 2.0 Core Librarys,Spring 2.0 Persistence Core Librarys等之后,deploy時myeclipse將所有這些包都拷到WEB-INF的lib下但其中有一些jar包重復或不兼容造成的。

          也可參考這篇網文

          昨天,配置了一整天Eclipse,終于搞定。不過在配置好Tomcat的連接池之后,測試與SQL Server2000的連接的時候出現了Cannot create JDBC driver of class '' for connect URL 'null'的錯誤。下面把我的解決方案寫出:注意:我使用的是JTDS驅動!

          1.請保證你的SQL Server 2000已經打過SP4的補丁了。之前我出現Cannot create JDBC driver of class '' for connect URL 'null'的問題,原因就是沒打SP4的補丁!

          2.正確配置連接池!

          常用的配置Tomcat連接池有兩種方法:一是利用Tomcat的管理界面。二是修改Tomcat的配置文件。這里簡單介紹通過修改Tomcat的配置文件配置連接池的方法。配置Tomcat連接池需要修改兩個個地方,一是$Tomcat_HOME/conf/server.xml$Tomcat_HOME是指Tomcat的安裝目錄,在server.xml <Host></Host>之間配置下面代碼。

          例程 1-3 配置server.xml文件

          <Context path="/Blog" docBase="Blog" debug="5" reloadable="true" crossContext="true">

                     <Resource name="jdbc/blog" auth="Container"

                     type="javax.sql.DataSource" driverClassName="net.sourceforge.jtds.jdbc.Driver"

                     url="jdbc:jtds:sqlserver://localhost:1305;DatabaseName=blog"

                     username="sa" password="" maxActive="20" maxIdle="10" maxWait="-1"/>

           </Context>

          另外一個需要修改的地方是$Tomcat_HOME/webapps/bookshop/WEB-INF/web.xml,在里web.xml文件里增加下面代碼:

          例程1-4 web.xml文件里要增加的內容

          <resource-ref>

                            <description>blog DB connect pool</description>

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

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

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

                   </resource-ref>

          下面是測試代碼:
          <%@ page contentType="text/html;charset=GBK"%>
          <%@ page import="java.sql.*"%>
          <html>
          <body>
          <%
            
          Context ctx=null;
             Connection cnn=null;
             Statement stmt=null;
             ResultSet rs=null;
             try   {
              ctx=new InitialContext();
              if(ctx==null) throw new Exception("沒有匹配的環境");
              DataSource ds=(DataSource)ctx.lookup("java:comp/env/jdbc/connectDB");
              if(ds==null) throw new Exception("沒有匹配數據庫");    
             cnn=ds.getConnection();    stmt=cnn.createStatemen(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
          String sql="select * from blog_user";
          rs=stmt.executeQuery(sql);
          <%out.print("數據庫操作成功,恭喜你");%>
          <%
          }catch(Exception ee){
               System.out.println("connect db error:"+ee.getMessage());
               return false;
          }finally
             {
              if(rs!=null)rs.close();
              if(stmt!=null)stmt.close();
              if(cnn!=null)cnn.close();
              if(ctx!=null)ctx.close();
             }
          %>
          </body>
          </html>


           

          posted on 2007-10-09 23:10 PariScamper 閱讀(12070) 評論(1)  編輯  收藏 所屬分類: Struts+Spring+Hibernate

          Feedback

          # re: tomcat啟動出現Cannot create JDBC driver of class '' for connect URL 'null'的解決方法[未登錄] 2016-03-23 22:58
          2,在server.xml <Host>和</Host>之間配置下面代碼:
          <Context path="/game" debug="5" reloadable="true" crossContext="true">
          <ResourceLink name="jdbc/game" global="jdbc/game" type="javax.sql.DataSource"/>
          </Context>

          里面的"/game"到底是數據庫的名字 還是連接池的名字  回復  更多評論
            

          主站蜘蛛池模板: 江门市| 九江县| 湟中县| 徐汇区| 崇州市| 北辰区| 阜新市| 三河市| 宁海县| 常熟市| 孟村| 常州市| 玛沁县| 绿春县| 上林县| 敖汉旗| 红安县| 安图县| 桂林市| 兴国县| 仪陇县| 怀远县| 商南县| 江都市| 竹山县| 托里县| 平潭县| 宣威市| 思茅市| 武邑县| 扬州市| 樟树市| 上杭县| 西平县| 来宾市| 惠安县| 浦东新区| 松原市| 定襄县| 黄大仙区| 中阳县|