WEB-INF目录下的web.xml中
<resource-ref>
<description>blog DB connect pool</description>
<res-ref-name>jdbc/blog</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
tomcat5.5.20服务器下的server.xml中
<Context path="/Blog" docBase="F:\workspace2\Blog\WebRoot" debug="5" reloadable="true" crossContext="true">
<Resource name="jdbc/blog" auth="Container"
type="javax.sql.DataSource" driverClassName="net.sourceforge.jtds.jdbc.Driver"
url="jdbc:jtds:sqlserver://localhost:1433;DatabaseName=blog"
username="sa" password="sa" maxActive="20" maxIdle="10" maxWait="-1"/>
</Context>
异常:Cannot load JDBC driver class 'net.sourceforge.jtds.jdbc.Driver'下面是数据库连接类和用于测试的jsp页面:
DB.java
package huc.blog.util;import java.sql.Connection;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import java.sql.SQLException;public class DB { private Connection conn = null;
private DataSource ds = null;
private String jdbcName = "jdbc/blog";
private Context ctx = null;
private StringBuffer jdbcStr = new StringBuffer(); /**
* 获得数据库的连接
*
* @return conn
*/
public Connection getConnection() {
this.init();
try {
conn = ds.getConnection();
} catch (SQLException e) {
System.out.println("connect db error:" + e.getMessage());
return null;
}
return conn;
} public String getJdbcName() {
return jdbcName;
} public void setJdbcName(String jdbcName) {
this.jdbcName = jdbcName;
} /**
* 初始化
*/
public void init() {
jdbcStr.append("java:comp/env/");
jdbcStr.append(this.jdbcName);
try {
ctx = new InitialContext();
if (ctx == null) {
throw new Exception("没有匹配的环境");
}
ds = (DataSource) ctx.lookup(jdbcStr.toString());
if (ds == null)
throw new Exception("没有匹配的数据库");
} catch (NamingException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("*************");
}
}dbtest.jsp<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ page import="huc.blog.util.DB" %>
<%@ page import="javax.sql.*" %>
<%@ page import="java.sql.*" %>
<%@ page import="javax.naming.*" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'dbtest.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head>
<body>
<%
DB db=new DB();
Connection conn=db.getConnection();
if(conn!=null) out.println("数据库连接成功!");
%>
</body>
</html>
还是这样的问题
common 的 lib下就行了