肯定是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"/>
另外一個需要修改的地方是$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>