日期:2014-05-16 浏览次数:20539 次
<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>
<?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>
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();
			}
		}
	}
}
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;
	}
	
}
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;
		}
	}