日期:2014-05-18  浏览次数:20681 次

tomcat中MySQL连接池配置问题
配置好数据源后:
在\conf\Catalina\localhost目录下建立一个xml文件,添加内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<Context>
 <Resource
  name="jdbc/mysql"
  type="javax.sql.DataSource"
  password="123456"
  driverClassName="com.mysql.jdbc.Driver"
  maxIdle="2"
  maxWait="50"
  username="root"
  url="jdbc:mysql://localhost:3306/test"
  maxActive="4"/>
</Context>

再在conf/server.xml中进行了相同配置

在%TOMCAT_HOME%\conf\web.xml中添加以下内容:

<resource-ref>
  <description>DB Connection</description>
  <res-ref-name>jdbc/mysql</res-ref-name>
  <res-type>javax.sql.DataSource</res-type>
  <res-auth>Container</res-auth>
</resource-ref>



测试代码是:
<%@ page import="java.io.*,java.util.*,java.sql.*,javax.sql.*,javax.naming.*"%>
<%@ page contentType="text/html;charset=GB2312"%>
<html>
<head><title>DataSourse Connection Test</title></head>
<body>
<%
 try{
  Connection con;
  Statement stmt;
  ResultSet rs;
 
  Context ctx = new InitialContext();
  DataSource ds=(DataSource)ctx.lookup("java:comp/env/jdbc/mysql");
  con=ds.getConnection();
 
  stmt=con.createStatement();
  rs=stmt.executeQuery("select * from user");
while(rs.next()){
  out.println(rs.getString(1));
 %>
 <br>
 
 <%

out.print(rs.getString(2));
}
  rs.close();
  stmt.close();
  con.close();
 
 }catch(Exception e){
  out.print(e.getMessage());
 }
 
%>
 </body>
</html> 

出现错误:Cannot create JDBC driver of class '' for connect URL 'null'


我在网上找了很多种方法,都试过了还是出现这样的问题,各位帮忙看看到底是怎么回事啊?我先在这边谢过了。






------解决方案--------------------
http://wenson.javaeye.com/blog/33319
------解决方案--------------------
没用过xml文件来配置数据源,你是用数据连接池的方式吧。有没有试过用其他的连接方式,比如纯java驱动连接数据库,不过用它连接需要MySql的Jar包,把它复制到Tomcat文件夹下的叫lib的文件下,就可以了。
------解决方案--------------------
确认Resource配置正确,并且必须放在DefaultContext或者Context配置节内