高分;关于数据库连接池的问题
环境:Tomcat5.0.25 + jdk1.4+ mysql+Winxp
首先在http://localhost/admin 创建数据源
写了个jsp测试
<!doctype html public "-//w3c//dtd html 4.0 transitional//en "
"http://www.w3.org/TR/REC-html40/strict.dtd ">
<%@ page import= "java.sql.* "%>
<%@ page import= "javax.sql.* "%>
<%@ page import= "javax.naming.* "%>
<%@ page session= "false " %>
<%@ page contentType= "text/html; charset=gb2312 " %>
<html>
<body>
<meta http-equiv= "Content-Type " content= "text/html; charset=gb2312 ">
<title> </title>
<%
DataSource ds = null;
try{
InitialContext ctx=new InitialContext();
ds=(DataSource)ctx.lookup( "java:comp/env/jdbc/mysql ");
Connection conn = ds.getConnection();
Statement stmt = conn.createStatement();
String strSql = " select * from student ";
ResultSet rs = stmt.executeQuery(strSql);
while(rs.next()){
out.print(rs.getString(1));
}
out.print( "over ");
}
catch(Exception ex){
out.print( "Error occur: "+ex.getMessage());
ex.printStackTrace();
}
%>
</body>
</html>
但是报错如下;Error occur:Cannot create JDBC driver of class ' ' for connect URL 'null '
很郁闷。高手看看是怎么回事。谢谢 !
[解决办法]
这是我用的,没问题,你试一下
首先:在tomcat目录下的conf目录中建立路径Catalina\localhost,在新建的目录下建立xml文件,注意:名称最好和工程名相同。在里面加入如下内容
<?xml version= "1.0 " encoding= "UTF-8 "?>
<Context displayName= "MessageBoard " docBase= "MessageBoard " path= "/MessageBoard " workDir= "work\Catalina\localhost\Messageboard ">
<Resource auth= "Container " name= "MessageBoard/jdbc/Data " type= "javax.sql.DataSource "> </Resource>
<ResourceParams name= "MessageBoard/jdbc/Data ">
<parameter>
<name> factory </name>
<value> org.apache.commons.dbcp.BasicDataSourceFactory </value>
</parameter>
<parameter>
<name> maxActive </name>
<value> 100 </value>
</parameter>
<parameter>
<name> maxIdle </name>
<value> 30 </value>
</parameter>
<parameter>
<name> maxWait </name>
<value> 10000 </value>
</parameter>
<parameter>
<name> username </name>
<value> root </value>
</parameter>
<parameter>
<name> password </name>
<value> 851120 </value>
</parameter>
<parameter>
<name> driverClassName </name>
<value> com.mysql.jdbc.Driver </value>
</parameter>
<parameter>
<name> url </name>
<value> jdbc:mysql://localhost:3306/msgboard?autoReconnect=true </value>
</parameter>
</ResourceParams>
</Context>
下一步时配置web.xml文件:在web-app中加入resource-ref文件
<web-app>
<resource-ref>
<description> DB Connection </description>
<res-ref-name> jdbc/msgBoard </res-ref-name>
<res-type> javax.sql.DataSource </res-type>
<res-auth> Container </res-auth>
</resource-ref>
</web-app>
测试数据源是否成功
新建立一个jsp文件,加入如下内容:
<%@ page language= "java " contentType= "text/html; charset=gbk "%>
<%@ page import= "java.sql.* " %>
<%@ page import= "javax.sql.* " %>
<%@ page import= "javax.naming.* " %>
<%@ page pageEncoding= "gbk " %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN ">
<html>
<head>
<%!
//这个函数用来获取一个和数据库库的连连接
Connection getConnection() throws SQLException{
String dbURL= "jdbc:mysql://localhost/msgboard ";
String dbUser= "root ";
String dbPassword= "851120 ";
try{
Class.forName( "com.mysql.jdbc.Driver ");
}catch(ClassNotFoundException e){
e.printStackTrace();
}
return DriverManager.getConnection(dbURL,dbUser,dbPassword);
}
%>
<meta http-equiv= "Content-Type " content= "text/html; charset=gbk ">
<title> 数据库库的连连接 </title>
</head>
<body>
<%
//Connection conn=null;
//try{
//conn=getConnection();
//}catch(SQLException e){
//e.printStackTrace();
//}
Context ctxt=new InitialContext();
Context envContext = (Context)ctxt.lookup( "java:/comp/env ");
DataSource ds = (DataSource)envContext.lookup( "MessageBoard/jdbc/Data ");
//DataSource ds=(DataSource)ctxt.lookup( "java:comp/env/MessageBoard/jdbc/Data ");
try{
Connection conn=ds.getConnection();
}catch(SQLException e){
e.printStackTrace();
}
%>
</body>
</html>
[解决办法]
可能是mysql-connection_java版本太底.用5.0的看看, 在xml中要设置全局context