当前位置: 技术问答>java相关
tomcat 4.1.10与sql server2000的数据库连接问题
来源: 互联网 发布时间:2015-10-16
本文导语: server.xml内容如下: factory org.apache.commons.dbcp.BasicDataSourceFactory driverClassName com.microsoft.jdbc.sqlserver.SQLServerDriv...
server.xml内容如下:
factory
org.apache.commons.dbcp.BasicDataSourceFactory
driverClassName
com.microsoft.jdbc.sqlserver.SQLServerDriver
url
jdbc:microsoft:sqlserver://bluecoffee:1433;DatabaseName=jsp
username
sa
password
xxxx
maxActive
20
maxIdle
10
maxWait
5000
validationQuery
removeAbandoned
true
removeAbandonedTimeout
60
logAbandoned
true
……
……
web.xml内容如下:
infovalidate
com.blue.beans.InfoValidate
test
Test
infovalidate
/info.validate
test
/test.test
30
index.jsp
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 server connect failed!java.sql.SQLException: Cannot load JDBC driver class 'null'
我已经安装JDBC驱动,同时三个jar文件也拷至TOMCAT的commonlib目录下,请各位兄弟帮忙解决一下,谢谢了!
factory
org.apache.commons.dbcp.BasicDataSourceFactory
driverClassName
com.microsoft.jdbc.sqlserver.SQLServerDriver
url
jdbc:microsoft:sqlserver://bluecoffee:1433;DatabaseName=jsp
username
sa
password
xxxx
maxActive
20
maxIdle
10
maxWait
5000
validationQuery
removeAbandoned
true
removeAbandonedTimeout
60
logAbandoned
true
……
……
web.xml内容如下:
infovalidate
com.blue.beans.InfoValidate
test
Test
infovalidate
/info.validate
test
/test.test
30
index.jsp
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 server connect failed!java.sql.SQLException: Cannot load JDBC driver class 'null'
我已经安装JDBC驱动,同时三个jar文件也拷至TOMCAT的commonlib目录下,请各位兄弟帮忙解决一下,谢谢了!
|
maxActive
100
maxIdle
5
maxWait
10
user
sa
username
sa
driverName
jdbc:microsoft:sqlserver://william:1433;databaseName=bluesite
url
jdbc:microsoft:sqlserver://william:1433;databaseName=bluesite
password
driverClassName
com.microsoft.jdbc.sqlserver.SQLServerDriver
ResourceParams部分如是写就可以了。Tomcat的不同版本参数好像有点不同。web.xml中需要添加:
Resource reference to a factory for java.sql.Connection instances that may be used for talking to a particular database that is configured in the server.xml file.
jdbc/SqlDB
javax.sql.DataSource
Container
|
三个jar文件的路径加入classpath