tomcat 4.1.10与sql server2000的数据库连接问题
server.xml内容如下:
<?xml version='1.0' encoding='utf-8'?>
<Server className="org.apache.catalina.core.StandardServer" port="8005" debug="0" shutdown="SHUTDOWN">
<Listener className="org.apache.catalina.mbeans.ServerLifecycleListener" debug="0"/>
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" debug="0"/>
<GlobalNamingResources>
<Environment name="simpleValue" override="true" type="java.lang.Integer" value="30"/>
<Resource name="jdbc/SqlDB" scope="Shareable" type="javax.sql.DataSource"/>
<Resource name="UserDatabase" scope="Shareable" type="org.apache.catalina.UserDatabase" auth="Container" description="User database that can be updated and saved"/>
<ResourceParams name="jdbc/SqlDB">
<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://bluecoffee:1433;DatabaseName=jsp</value>
</parameter>
<parameter>
<name>username</name>
<value>sa</value>
</parameter>
<parameter>
<name>password</name>
<value>xxxx</value>
</parameter>
<parameter>
<name>maxActive</name>
<value>20</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>10</value>
</parameter>
<parameter>
<name>maxWait</name>
<value>5000</value>
</parameter>
<parameter>
<name>validationQuery</name>
<value></value>
</parameter>
<parameter>
<name>removeAbandoned</name>
<value>true</value>
</parameter>
<parameter>
<name>removeAbandonedTimeout</name>
<value>60</value>
</parameter>
<parameter>
<name>logAbandoned</name>
<value>true</value>
</parameter>
</ResourceParams>
……
</GlobalNamingResources>
……
</Server>
web.xml内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC '-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN' 'http://java.sun.com/j2ee/dtds/web-app_2.2.dtd'>
<web-app>
<servlet>
<servlet-name>infovalidate</servlet-name>
<servlet-class>com.blue.beans.InfoValidate</servlet-class>
</servlet>
<servlet>
<servlet-name>test</servlet-name>
<servlet-class>Test</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>infovalidate</servlet-name>
<url-pattern>/info.validate</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>test</servlet-name>
<url-pattern>/test.test</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>30</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
Test.java内容如下:
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import javax.sql.*;
import javax.naming.*;
public class Test extends HttpServlet
{
public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException{
res.setContentType("text/plain");
PrintWriter out = res.getWriter();
try
{
Context ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/SqlDB");
Connection con = ds.getConnection();
out.println("Database connect successful!");
con.close();
}
catch (SQLException sqlex)
{
sqlex.printStackTrace();
out.println("sql server connect failed!"+sqlex.toString());
}
catch (NamingException nex)
{
nex.printStackTrace();
out.println("the database didn't find!"+nex.toString());
}
}
};
编译后执行网页上内容显示如下:
sql serv