tomcat5.5.9 jdbc 连接池配置时常会出现两个错误:
1.***not bound
2.Cannot create JDBC driver of class '' for connect URL 'null'
可以通过以下的方法达到正确的配置:
1.现在%tomcat home%/conf/catalina/localhost 下面添加一段独立的context xml段,命名为jasper.xml,例如
<?xml version="1.0" encoding="UTF-8"?>
<Context path="/jasper" docBase="jasper" debug="1" reloadable="true" crossContext="true">
</Context>
2.通过admin的界面(或手动在server.xml界面里进行配置)数据源,以sql2k为例,配置后的server.xml应该为
<?xml version="1.0" encoding="UTF-8"?>
<Server>
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener"/>
<Listener className="org.apache.catalina.storeconfig.StoreConfigLifecycleListener"/>
<Listener className="org.apache.catalina.mbeans.ServerLifecycleListener"/>
<GlobalNamingResources>
<Environment
name="simpleValue"
type="java.lang.Integer"
value="30"/>
<Resource
auth="Container"
description="User database that can be updated and saved"
name="UserDatabase"
type="org.apache.catalina.UserDatabase"
pathname="conf/tomcat-users.xml"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"/>
<Resource
name="jdbc/Northwind"
type="javax.sql.DataSource"
password="*****"
driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver"
maxIdle="2"
maxWait="5000"
username="sa"
url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=Northwind"
maxActive="20"/>
</GlobalNamingResources>
<Service
name="Catalina">
<Connector
port="80"
redirectPort="8443"
minSpareThreads="25"
connectionTimeout="20000"
maxSpareThreads="75"
maxThreads="150"
maxHttpHeaderSize="8192">
</Connector>
<Connector
port="8009"
redirectPort="8443"
protocol="AJP/1.3">
</Connector>
<Engine
defaultHost="localhost"
name="Catalina">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"/>
<Host
appBase="webapps"
name="localhost">
</Host>
</Engine>
</Service>
</Server>
3.在conf下面的context.xml文件中,</Context>之前加入
<ResourceLink name="jdbc/Northwind" global="jdbc/Northwind" type="javax.sql.DataSourcer"/>
4.在你的web文件夹下面的WEB-INF中找到web.xml,在最后</web-app>之前加入
<resource-ref>
<description>Northwind Connection</description>
<res-ref-name>jdbc/Northwind</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
5.保存,重启tomcat,这样,通过测试页面,就可以发现已经可以通过数据库连接池连上sql2k
test1.jsp
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<%@page import="javax.naming.*"%>
<%@page import="javax.sql.*"%>
<%
Context initContext = new InitialContext();
Context envContext = (Context)initContext.lookup("java:/comp/env");
DataSource ds = (DataSource)envContext.lookup("jdbc/Northwind");
Connection conn = ds.getConnection();
int counts=0;
String sql="select count(*) from orders";
PreparedStatement pst=conn.prepareStatement(sql);
ResultSet rst=pst.executeQuery();
while(rst.next()){
counts=rst.getInt(1);
}
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>JNDI 测试</title>
</head>
<body>
<%=sql%><br>
pnrreg talbe's count is:<%=counts%>
</body>
</html>
打完收工