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 阅读(12002) 评论(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"到底是数据库的名字 还是连接池的名字  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: