在server.xml中</host>上面加入:
<Context
docBase="D:\eclipse3.2\workspace\test\WebRoot"
path="/test"
reloadable="true" >
<Resource name="jdbc/rat" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver"
username="sa" password=""
url="jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=test"
defaultAutoCommit="true" removeAbondoned="true"
removeAbondonedTimeout="60" logAbondoned="true"/>
</Context>
在web.xml文件中加入:
<resource-ref>
<description>Sql Server Datasource</description>
<res-ref-name>jdbc/rat</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
项目文件夹是test
数据库:test
测试页:test.jsp
<%@ page contentType="text/html;charset=gb2312" %>
<%@ page import="javax.naming.*" %>
<%@ 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");
file://从Context中lookup数据源
ds=(DataSource)envCtx.lookup("jdbc/rat");
if(ds!=null)
{
out.println("DataSource!OK!");
out.println("<br>");
Connection conn=ds.getConnection();
Statement stmt=conn.createStatement();
ResultSet rst=stmt.executeQuery("select * from test");
out.println("DATA out!");
while(rst.next())
{
out.println("bookName:"+rst.getString("test"));
out.println("<br>");
}
}
else
out.println("失败!");
}
catch(Exception ne)
{
out.println(ne);
}
%>
tomcat5.5.20解压版
JDK5
请问问题出在哪?
<Context
docBase="D:\eclipse3.2\workspace\test\WebRoot"
path="/test"
reloadable="true" >
<Resource name="jdbc/rat" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver"
username="sa" password=""
url="jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=test"
defaultAutoCommit="true" removeAbondoned="true"
removeAbondonedTimeout="60" logAbondoned="true"/>
</Context>
在web.xml文件中加入:
<resource-ref>
<description>Sql Server Datasource</description>
<res-ref-name>jdbc/rat</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
项目文件夹是test
数据库:test
测试页:test.jsp
<%@ page contentType="text/html;charset=gb2312" %>
<%@ page import="javax.naming.*" %>
<%@ 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");
file://从Context中lookup数据源
ds=(DataSource)envCtx.lookup("jdbc/rat");
if(ds!=null)
{
out.println("DataSource!OK!");
out.println("<br>");
Connection conn=ds.getConnection();
Statement stmt=conn.createStatement();
ResultSet rst=stmt.executeQuery("select * from test");
out.println("DATA out!");
while(rst.next())
{
out.println("bookName:"+rst.getString("test"));
out.println("<br>");
}
}
else
out.println("失败!");
}
catch(Exception ne)
{
out.println(ne);
}
%>
tomcat5.5.20解压版
JDK5
请问问题出在哪?
org.apache.commons.dbcp.SQLNestedException: Cannot load JDBC driver class 'com.microsoft.jdbc.sqlserver.SQLServerDriver'
你要把mssql server的jdbc驱动放到tomcat的common\lib目录下
WEB-INF\lib\
把驱动放在应用项目WEB-INF\lib\下时,需要改web.xml
<resource-ref>
<description>mysql1 Connection</description>
<res-ref-name>jdbc/mysql1</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource
name="jdbc/quickstart"
type="javax.sql.DataSource"
password="123456"
driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver"
maxIdle="2"
maxWait="5000"
username="sa"
url="jdbc:microsoft:sqlserver://localhost;DatabaseName=quickstart"
maxActive="4"/>
</Context>
还有可能是JDBC驱动版本问题,希望对LZ有帮助
MSSQL2005:com.microsoft.sqlserver.jdbc.SQLServerDriver
不知道你是用2000还是用2005了。这个问题我以前遇到过希望帮得到你吧。