登陆用户怎样获取验证码和保存用户到cookie中(一)

2014-11-24 08:20:01 · 作者: · 浏览: 6

User表:

\

User.java

package user.domain;

import java.io.Serializable;
import java.util.Date;

public class User implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
    
	private Integer id;
	private String name;
	private String pass;
	private Date birth;
	public User(){
		super();
	}
	public User(Integer id, String name, String pass, Date birth) {
		super();
		this.id = id;
		this.name = name;
		this.pass = pass;
		this.birth = birth;
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPass() {
		return pass;
	}
	public void setPass(String pass) {
		this.pass = pass;
	}
	public Date getBirth() {
		return birth;
	}
	public void setBirth(Date birth) {
		this.birth = birth;
	}
	@Override
	public String toString() {
		return "User [id=" + id + ", name=" + name + ", pass=" + pass
				+ ", birth=" + birth + "]";
	}
	
}


UserDao.java

package user.dao;

import java.util.List;

import user.domain.User;

public interface UserDao {
    /**
	 * 根据用户和密码查询用户信息
	 */
	User checkLogin(String name,String pass);
	
}

实现接口的类UserDaoImp.java

package user.imp;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import user.dao.UserDao;
import user.domain.User;
import util.DBConn;

public class UserDaoImp implements UserDao {
	private Connection conn;
	private PreparedStatement pstmt;
	private ResultSet rs;

	public User checkLogin(String name, String pass) {
		User entity=null;
		String sql="select id,name,pass from user where name=  and pass= ";
		conn=DBConn.getConn();
		try {
			pstmt=conn.prepareStatement(sql);
			int index=1;
			pstmt.setString(index++,name);
			pstmt.setString(index++,pass);
			rs=pstmt.executeQuery();
			while(rs.next()){
				entity=new User();
				entity.setId(rs.getInt("id"));
				entity.setName(rs.getString("name"));
				entity.setPass(rs.getString("pass"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			DBConn.release(rs, pstmt);
		}
		
		
		return entity;
	}

}

工具包DBConn.java

package util;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

public class DBConn {
 private static Connection conn;
 private DBConn(){
	 
 }
 public static Connection getConn(){
	 if(conn==null){
		 //创建集合对象
		 Properties properties=new Properties();
		 try {
			 //装载
			properties.load(DBConn.class.getClassLoader().getResourceAsStream("db.properties"));
		    //加载驱动程序
			Class.forName(properties.getProperty("driverClassName"));
			//获取连接对象
			conn=DriverManager.getConnection(properties.getProperty("url"),properties.getProperty("user"),properties.getProperty("pass"));
		 } catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	 }
	 return conn;
 }
 public static void update(String sql,Object params[],Prepare