www.pudn.com > bookshop.zip > UserDao.java, change:2013-07-08,size:2054b


package com.tarena.dao; 
 
import java.io.PrintWriter; 
import java.sql.Connection; 
import java.sql.PreparedStatement; 
import java.sql.ResultSet; 
 
import com.tarena.bean.User; 
import com.tarena.util.DB; 
 
public class UserDao { 
	 
	//检查 email...是否正确 
	public User checkEmailAndPwd(String email,String password){ 
		User user=null; 
		try { 
			Connection con = DB.getCon(); 
			String sql="select * from d_user where email=?and password=?"; 
			PreparedStatement prep=con.prepareStatement(sql); 
			prep.setString(1, email); 
			prep.setString(2, password); 
			ResultSet rs=prep.executeQuery(); 
			if(rs.next()){                              //账户,密码不匹配,不走if 
				user=new User(); 
				user.setId(rs.getInt("id")); 
				user.setEmail(rs.getString("email")); 
				user.setNickname(rs.getString("nickname")); 
				user.setPwd(rs.getString("password")); 
			} 
		} catch (Exception e) { 
			// TODO Auto-generated catch block 
			e.printStackTrace(); 
		}; 
		return user; 
	} 
	 
	 
 
	public void saveUser (User user){ 
		try { 
			Connection con=DB.getCon(); 
			String sql = "insert into d_user"+"(email,nickname,password) values(?,?,?)"; 
			PreparedStatement prep = con.prepareStatement(sql); 
			prep.setString(1,user.getEmail()); 
			prep.setString(2,user.getNickname()); 
			prep.setString(3,user.getPwd()); 
			prep.executeUpdate(); 
 
		} catch (Exception e) { 
			// TODO Auto-generated catch block 
			e.printStackTrace(); 
		} 
	} 
 
	public User getUserByEmail(String email){ 
		User user = null; 
		try { 
			Connection con = DB.getCon(); 
			String sql = "select * from d_user where email=?"; 
			PreparedStatement prep = con.prepareStatement(sql); 
			prep.setString(1,email); 
			ResultSet rs = prep.executeQuery(); 
			if(rs.next()){ 
				user = new User(); 
				user.setId(rs.getInt("id")); 
				user.setEmail(rs.getString("email")); 
				user.setNickname(rs.getString("nickname")); 
			} 
		} catch (Exception e) { 
			// TODO Auto-generated catch block 
			e.printStackTrace(); 
		} 
	return user;	 
	} 
 
}