主题:我的tomcat数据库池错在哪儿了????
win2003+MSSQL2000;目录/root;SQL驱动已放在common\lib
目录下,tomcat安装在D盘
在d:/tomcat/onf/server.xml里添加了;
<Context path="/root" docBase="root" debug="0" reloadable="true" crossContext="true">
<Resource name="jdbc/xuxkweb" auth="Container" type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/xuxkweb">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>com.microsoft.jdbc.sqlserver.SQLServerDriver</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=xuxkweb</value>
</parameter>
<parameter>
<name>username</name>
<value>sa</value>
</parameter>
<parameter>
<name>password</name>
<value>123qwe</value>
</parameter>
<parameter>
<name>maxActive</name>
<value>20</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>10</value>
</parameter>
<parameter>
<name>maxWait</name>
<value>-1</value>
</parameter>
</ResourceParams>
</Context>
在/root/web.xml里添加
<description>MSSql server Test App</description>
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/xuxkweb</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
测试文件copool.jsp
<%@ page contentType="text/html; charset=gb2312" %>
<%@ page import="javax.naming.Context" %>
<%@ page import="javax.sql.DataSource"%>
<%@ page import="javax.naming.InitialContext"%>
<%@ page import="java.sql.*"%>
<%
DataSource ds = null;
try{
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
//从Context中lookup数据源。
ds = (DataSource)envCtx.lookup("jdbc/xuxkweb");
if(ds!=null)
{
out.println("已经获得DataSource!");
out.println("<br>");
Connection conn = ds.getConnection();
Statement stmt=conn.createStatement();
ResultSet rst=stmt.executeQuery("select * from userinfo");
out.println("以下是从数据库中读取出来的数据");
while(rst.next())
{
out.println("bookName:"+rst.getString("username"));
out.println("<br>");
}
}
else
out.println("连接失败!");
}
catch(Exception ne)
{
out.println(ne);
}
%>
运行结果:
已经获得DataSource!
org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null'
赐教!!谢谢!!
目录下,tomcat安装在D盘
在d:/tomcat/onf/server.xml里添加了;
<Context path="/root" docBase="root" debug="0" reloadable="true" crossContext="true">
<Resource name="jdbc/xuxkweb" auth="Container" type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/xuxkweb">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>com.microsoft.jdbc.sqlserver.SQLServerDriver</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=xuxkweb</value>
</parameter>
<parameter>
<name>username</name>
<value>sa</value>
</parameter>
<parameter>
<name>password</name>
<value>123qwe</value>
</parameter>
<parameter>
<name>maxActive</name>
<value>20</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>10</value>
</parameter>
<parameter>
<name>maxWait</name>
<value>-1</value>
</parameter>
</ResourceParams>
</Context>
在/root/web.xml里添加
<description>MSSql server Test App</description>
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/xuxkweb</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
测试文件copool.jsp
<%@ page contentType="text/html; charset=gb2312" %>
<%@ page import="javax.naming.Context" %>
<%@ page import="javax.sql.DataSource"%>
<%@ page import="javax.naming.InitialContext"%>
<%@ page import="java.sql.*"%>
<%
DataSource ds = null;
try{
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
//从Context中lookup数据源。
ds = (DataSource)envCtx.lookup("jdbc/xuxkweb");
if(ds!=null)
{
out.println("已经获得DataSource!");
out.println("<br>");
Connection conn = ds.getConnection();
Statement stmt=conn.createStatement();
ResultSet rst=stmt.executeQuery("select * from userinfo");
out.println("以下是从数据库中读取出来的数据");
while(rst.next())
{
out.println("bookName:"+rst.getString("username"));
out.println("<br>");
}
}
else
out.println("连接失败!");
}
catch(Exception ne)
{
out.println(ne);
}
%>
运行结果:
已经获得DataSource!
org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null'
赐教!!谢谢!!