日期:2014-05-16  浏览次数:20409 次

数据库缓冲池在tomcat6.0中的配置
一、创建Servlet,在web.xml文件中加入下面配置
<resource-ref>
		<description>DB Connection</description>
		<res-ref-name>jdbc/john</res-ref-name>
		<res-type>javax.sql.DataSource</res-type>
		<res-auth>Container</res-auth>
	</resource-ref>

之后,(使用的数据库是sqlserver2005)在tomcat安装目录下面的lib文件夹中加入连接sqlserver的sqljdbc.jar,网上一些例子说要加到common/lib文件夹下面,其实tomcat6.0安装目录下默认没有common文件夹,这个版本直接把驱动加入到安装目录下面的lib文件夹中就行了.)
(注:提醒一些大意的朋友,同时在项目的WebRoot/WEB-INF/lib下面也应加入了sqljdbc.jar)
----------------------------------
二、在WebRoot/META-INFO/下创建context.xml,如下
<?xml version="1.0" encoding="GBK"?>
<Context>
	<Resource 
	name="jdbc/john"
	type="javax.sql.DataSource"
	driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
	url="jdbc:sqlserver://127.0.0.1:1433;databaseName=test"
	username="john"
    password="123"
    maxIdle="2" 
    maxWait="5000"
    validationQuery="select 1"
    maxActive="4"
    />
</Context>

----------------------------
三、写Db连接数据库类
import java.sql.Connection;
import java.sql.SQLException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

public class Db {
	java.sql.Connection con = null;
	public Connection getCon(){
		
		try{
			Context ctx = new InitialContext(); 
			DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/john");
			con = ds.getConnection();
			
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return con;
	}
	public void conClose(){
		if(con != null){
			try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}

------------------
写dao:
package com.tonghu.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.tong.DBUtil.Db;
import com.tonghu.vo.DeptVO;

public class DaoTest {
	//DeptVO dept = new DeptVO();
	public boolean login(DeptVO dept){
		Db db = new Db();
		Connection conn = db.getCon();
		ResultSet rs = null;
		PreparedStatement ps = null;
		String sql = "select * from dept where dname=? and loc=?";
		try{
			ps = conn.prepareStatement(sql);
			ps.setString(1, dept.getDname());
			ps.setString(2, dept.getLoc());
			rs = ps.executeQuery();
			if(rs.next()){
				dept.setDeptno(rs.getString("deptno"));
				
				return true;
			}
		}catch(SQLException e){
			e.printStackTrace();
		}finally{
			try {
				rs.close();
				ps.close();
				conn.close();
				
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return false;
	}
	
}

--------------------------------------------
四、写servlet实现,关键代码如下:
public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//生成会话session
		HttpSession session = request.getSession();
		response.setContentType("text/html;charset=utf-8");
		ServletContext sc = getServletContext();
//		
		PrintWriter out = response.getWriter();
		DaoTest da = new DaoTest();
		String dname = request.getParameter("dname");
		String loc = request.getParameter("loc");
		DeptVO dept = new DeptVO();
		dept.setDname(dname);
		dept.setLoc(loc);
		if(da.login(dept)){
			//设置把数据保存到session中
		//	session.setAttribute("deptno", dept.getDeptno());
			request.setAttribute("deptno", dept.getDeptno());
//			System.out.println("==="+dept.getDeptno());
			//使用跳转方式
			sc.getRequestDispatcher("/loginsuccess.jsp").forward(request, response);
			//重定向
//			response.sendRedirect("../loginsuccess.jsp");
//			return;
		
		}else{
			//使用重定向则应该加上一个return,因为后面的内容不需要再加载了
			response.sendRedirect("../error.jsp");
			return;
		}
	}

----------------------------