困扰我很久tomcat6+sqlserver2000数据库连接池问题,希望大家能给个解决方法
服务器:tomcat6.0.18
数据库:MS sqlserver2000
数据库驱动:Microsoft SQL Server 2005 JDBC Driver,微软官网上写这个驱动可以连接sql server 2000,是2000和2005的通用驱动
以下是我的配置:
1。首先,我把JDBC驱动sqljdbc.jar,以及commons-pool-1.3.jar,commons-dbcp-1.2.2.jar,commons-collections-3.1.jar复制到tomcat6/lib目录下
2。在我自己web工程/web-inf 建立一个Context.xml
输入代码如下:
<Context path="/nelds" docBase="nelds" crossContext="true" reloadable="true" debug="1">
<Resource name="jdbc/nelds" auth="Container" type="javax.sql.DataSource" driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver" url="jdbc:sqlserver://localhost:1433;DatabaseName=nelds" username="sa" password="sa" maxActive="20" maxIdle="10" />
</Context>3。然后在web-inf下, 修改web.xml
<resource-ref>
<description>postgreSQL Datasource example</description>
<res-ref-name>jdbc/nelds</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>4。然后是在web工程目录下,建立test.jsp
<%@ page contentType="text/html; charset=gb2312" %>
<%@ page import="javax.naming.*" %>
<%@ page import="javax.sql.*" %>
<%@ page import="java.sql.*" %>
<html><head>
<title>JDBC Test</title>
</head>
<body>
<%
try{ Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:/comp/env");
DataSource ds = (DataSource)envCtx.lookup("jdbc/nelds");
Connection con = ds.getConnection();
Statement sql;
ResultSet rs;
sql=con.createStatement();
String squery="select * from Usermgr";
rs=sql.executeQuery(squery);
while(rs.next())
{
out.print(rs.getString("UPassword"));
}
con.close();
}
catch(Exception ex)
{
out.print(ex);
}
%>
</body>
</html>然后重启tomcat6
运行后
得到异常
org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null'
服务器:tomcat6.0.18
数据库:MS sqlserver2000
数据库驱动:Microsoft SQL Server 2005 JDBC Driver,微软官网上写这个驱动可以连接sql server 2000,是2000和2005的通用驱动
以下是我的配置:
1。首先,我把JDBC驱动sqljdbc.jar,以及commons-pool-1.3.jar,commons-dbcp-1.2.2.jar,commons-collections-3.1.jar复制到tomcat6/lib目录下
2。在我自己web工程/web-inf 建立一个Context.xml
输入代码如下:
<Context path="/nelds" docBase="nelds" crossContext="true" reloadable="true" debug="1">
<Resource name="jdbc/nelds" auth="Container" type="javax.sql.DataSource" driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver" url="jdbc:sqlserver://localhost:1433;DatabaseName=nelds" username="sa" password="sa" maxActive="20" maxIdle="10" />
</Context>3。然后在web-inf下, 修改web.xml
<resource-ref>
<description>postgreSQL Datasource example</description>
<res-ref-name>jdbc/nelds</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>4。然后是在web工程目录下,建立test.jsp
<%@ page contentType="text/html; charset=gb2312" %>
<%@ page import="javax.naming.*" %>
<%@ page import="javax.sql.*" %>
<%@ page import="java.sql.*" %>
<html><head>
<title>JDBC Test</title>
</head>
<body>
<%
try{ Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:/comp/env");
DataSource ds = (DataSource)envCtx.lookup("jdbc/nelds");
Connection con = ds.getConnection();
Statement sql;
ResultSet rs;
sql=con.createStatement();
String squery="select * from Usermgr";
rs=sql.executeQuery(squery);
while(rs.next())
{
out.print(rs.getString("UPassword"));
}
con.close();
}
catch(Exception ex)
{
out.print(ex);
}
%>
</body>
</html>然后重启tomcat6
运行后
得到异常
org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null'
解决方案 »
- 数据库乱码
- 关于图片上传无效的问题
- 怎样JAVA实现录音文件的播放???
- org.apache.jasper.JasperException: Unterminated <html:text tag
- 高分求助!JSP版fckeditor 如何在页面上去到上传图片的路径啊?
- 给网站生成静态页面的问题(没那么简单)!
- Java和Tomcat的基础问题,白拣分
- 关于jsp聊天室自动滚屏的问题!!!
- 制作gif文件的工具,推荐一些比较好的,谢谢
- 我刚下的j2sdk-1_4_0-beta3-win,但是安装后不能启动,为什么??
- 怎样在ZK中控制浏览器的刷新?
- Struts2集成Struts1的问题
1.JDBC驱动sqljdbc.jar,以及commons-pool-1.3.jar,commons-dbcp-1.2.2.jar,commons-collections-3.1.jar复制到tomcat6/lib目录下~~不能放在自己的webAPP目录下~是无效的
2.
在Tomcat 6.0\conf\catalina\localhost自己的webAPP。xml里面写入代码,最后如下:
<?xml version="1.0" encoding="UTF-8"?>
<Context path="/imo" reloadable="true" docBase="imo" workDir="imo\work"><WatchedResource>WEB-INF/web.xml</WatchedResource><Resource name="jdbc/ApplicationCon" auth="Container" type="javax.sql.DataSource"
maxActive="30" removeAbandoned="true"
removeAbandonedTimeout="10" logAbandoned="true"
username="sa" password="sasa" driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
url="jdbc:sqlserver://localhost:1152;DatabaseName=IMO" /></Context>3.
修改web。xml
写入如下代码
<resource-ref>
<description>SQL Server Datasource</description>
<res-ref-name>jdbc/ApplicationCon</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>4.写一个公共访问数据库的类,如下
package database.operation;import java.sql.*;import javax.naming.*;
import javax.sql.DataSource;public class ApplicationCon {
private Connection con;
private Statement sql;
private ResultSet rs;
public ApplicationCon()
{
con=null;
sql=null;
rs=null;
}
private Connection getConnection()
{
Connection conn=null;
try{
Context initContext=new InitialContext();
if(initContext==null)
throw new Exception("no Context");
Context envContext=(Context)initContext.lookup("java:/comp/env");
DataSource ds=(DataSource)envContext.lookup("jdbc/ApplicationCon");
if(ds!=null)
{
conn=ds.getConnection();
}
}
catch(Exception e)
{
e.printStackTrace();
}
return conn;
} public ResultSet ExecuteQuery(String s)
{ con=this.getConnection();
sql=null;
rs=null;
try
{
sql=con.createStatement();
rs=sql.executeQuery(s);
}
catch(Exception e)
{
e.printStackTrace();
}
return rs;
} public int ExecuteUpdate(String s)
{ con=this.getConnection();
sql=null;
int result=0;
try
{
sql=con.createStatement();
result=sql.executeUpdate(s);
}
catch(Exception e)
{
e.printStackTrace();
}
return result;
}
public int ExecuteInsert(String s)
{
int result=this.ExecuteUpdate(s);
return result;
} public int ExecuteDelete(String s)
{
int result=this.ExecuteUpdate(s);
return result;
}
public boolean Execute(String sqlst)
{
boolean exresult=false;
con=this.getConnection();
sql=null;
try
{
sql=con.createStatement();
exresult=sql.execute(sqlst);
}
catch(Exception e)
{
e.printStackTrace();
}
return exresult;
}
public void closeConnection()
{
if(this.con == null)return;
try
{ if(this.rs!=null)
{
//System.out.println("excute rs");
this.rs.close();
}
if(this.sql!=null)
{
//System.out.println("excute sql");
this.sql.close();
}
this.con.close();
if(con.isClosed())
{
//System.out.println("连接已经关闭");
}
this.rs = null;
this.sql = null;
this.con = null;
}
catch(Exception e)
{
System.out.print(e);
}
}
}
tomcat 的不同版本指定驱动类的名字是不同的,有的是 driverClass 你去百度一下就知道了。