我在使用tomcat5.0连接池功能时,得到的conn对象总是为空,真是郁闷,请高人指点。多谢多谢了!!!!
这是我的server.xml
<?xml version='1.0' encoding='utf-8'?>
<Server>
<Listener className="org.apache.catalina.mbeans.ServerLifecycleListener"/>
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener"/>
<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"/>
<Resource name="jdbc/hello" type="javax.sql.DataSource"/>
<ResourceParams name="UserDatabase">
<parameter>
<name>factory</name>
<value>org.apache.catalina.users.MemoryUserDatabaseFactory</value>
</parameter>
<parameter>
<name>pathname</name>
<value>conf/tomcat-users.xml</value>
</parameter>
</ResourceParams>
<ResourceParams name="jdbc/hello">
<parameter>
<name>url</name>
<value>jdbc:oracle:[email protected]:1521:jingrui</value>
</parameter>
<parameter>
<name>password</name>
<value>tiger</value>
</parameter>
<parameter>
<name>maxActive</name>
<value>4</value>
</parameter>
<parameter>
<name>maxWait</name>
<value>5000</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>oracle.jdbc.driver.OracleDriver</value>
</parameter>
<parameter>
<name>username</name>
<value>scott</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>2</value>
</parameter>
</ResourceParams>
</GlobalNamingResources>
<Service name="Catalina">
<Connector acceptCount="100" connectionTimeout="20000" disableUploadTimeout="true" port="8080" redirectPort="8443" maxSpareThreads="75" maxThreads="150" minSpareThreads="25">
</Connector>
<Connector port="8009" protocol="AJP/1.3" protocolHandlerClassName="org.apache.jk.server.JkCoyoteHandler" redirectPort="8443">
</Connector>
<Engine defaultHost="localhost" name="Catalina">
<Host appBase="webapps" name="localhost">
<Logger className="org.apache.catalina.logger.FileLogger" prefix="localhost_log." suffix=".txt" timestamp="true"/>
</Host>
<Logger className="org.apache.catalina.logger.FileLogger" prefix="catalina_log." suffix=".txt" timestamp="true"/>
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"/>
</Engine>
</Service>
</Server>
这是我的web.xml加的东东
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app>
<display-name>tiannet web</display-name>
<description>connectDB test</description>
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/hello</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
都是按其文档帮助中进行的,就是不中
这是测试页面
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<%@ page import="javax.sql.*"%>
<%@ page import="javax.naming.*"%>
<html>
<body>
<%
DataSource ds = null;
ResultSet rs = null;
Statement stmt = null;
Connection conn = null;
String m_strDriver = "oracle.jdbc.driver.OracleDriver", // 驱动字符串
m_strConURL = "jdbc:oracle:thin:"; // 连接字符串
try
{
Context initContext = new InitialContext();
ds = (DataSource)initContext.lookup ("java:comp/env/jdbc/hello");
//注意connectDB的名称和上面一致
conn = ds.getConnection();
out.println("has error here");
stmt = conn.createStatement();
String strSql = " select * from emp"; //test为数据库中的一个表
rs = stmt.executeQuery(strSql);
if(rs==null){out.println("rs is null");}
else
{out.println("okokok");}
while(rs.next())
{
out.println("hello");
}
}
catch(Exception ex)
{
if(conn == null){out.println("conn is null");}
out.println(ex.getMessage());
out.println("has dkjf");
}
finally
{
}
%>
</body>
</html>
本文地址:http://com.8s8s.com/it/it13382.htm