www.pudn.com > restrant.zip > UserDao.java, change:2014-03-10,size:6561b


package com.alex.restrantol.dao; 
 
import java.sql.Connection; 
import java.sql.PreparedStatement; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
import java.text.SimpleDateFormat; 
import java.util.ArrayList; 
import java.util.Date; 
import java.util.List; 
 
import com.alex.restrantol.base.BaseDao; 
import com.alex.restrantol.entity.AdminEntity; 
import com.alex.restrantol.entity.UserEntity; 
 
public class UserDao { 
 
	Connection con; 
	PreparedStatement ps; 
	ResultSet rs; 
 
	public List<UserEntity> findUserAll() { 
		List<UserEntity> list = null; 
		UserEntity user = null; 
		try { 
			con = new BaseDao().getConn(); 
			ps = con.prepareStatement("select * from customer"); 
			rs = ps.executeQuery(); 
			while(rs.next()){ 
				list = new ArrayList<UserEntity>(); 
				user = new UserEntity(); 
				user.setId(rs.getInt("id")); 
				user.setAddress(rs.getString("address")); 
				user.setEmail(rs.getString("email")); 
				user.setName(rs.getString("name")); 
				user.setTruename(rs.getString("truename")); 
				user.setPsw(rs.getString("psw")); 
				user.setRegTime(rs.getDate("regTime")); 
				if(Integer.parseInt(rs.getString("status"))==1) 
					user.setStatus(true); 
				else 
					user.setStatus(false); 
				user.setAddress(rs.getString("address")); 
				list.add(user); 
			} 
		} catch (SQLException e) { 
			e.printStackTrace(); 
		} 
		return list; 
	} 
 
	public UserEntity findUserById(int userId) { 
		List<UserEntity> list = new ArrayList<UserEntity>(); 
		UserEntity user = null; 
		try { 
			con = new BaseDao().getConn(); 
			ps = con.prepareStatement("select * from customer where id=?"); 
			ps.setInt(1, userId); 
			rs = ps.executeQuery(); 
			while(rs.next()){ 
				user = new UserEntity(); 
				user.setId(rs.getInt("id")); 
				user.setAddress(rs.getString("address")); 
				user.setEmail(rs.getString("email")); 
				user.setName(rs.getString("name")); 
				user.setTruename(rs.getString("truename")); 
				user.setPsw(rs.getString("psw")); 
				user.setRegTime(rs.getDate("regTime")); 
				if(Integer.parseInt(rs.getString("status"))==1) 
					user.setStatus(true); 
				else 
					user.setStatus(false); 
				user.setAddress(rs.getString("address")); 
			} 
		} catch (SQLException e) { 
			e.printStackTrace(); 
		} 
		return user; 
	} 
 
	public UserEntity findUserIsExist(String uname) { 
		List<UserEntity> list = new ArrayList<UserEntity>(); 
		UserEntity user = null; 
		try { 
			con = new BaseDao().getConn(); 
			ps = con 
					.prepareStatement("select * from customer where name=?"); 
			ps.setString(1, uname); 
			rs = ps.executeQuery(); 
			while(rs.next()){ 
				user = new UserEntity(); 
				user.setId(rs.getInt("id")); 
				user.setAddress(rs.getString("address")); 
				user.setTel(rs.getString("tel")); 
				user.setEmail(rs.getString("email")); 
				user.setName(rs.getString("name")); 
				user.setTruename(rs.getString("truename")); 
				user.setPsw(rs.getString("psw")); 
				user.setRegTime(rs.getDate("regTime")); 
				if(Integer.parseInt(rs.getString("status"))==1) 
					user.setStatus(true); 
				else 
					user.setStatus(false); 
				user.setAddress(rs.getString("address")); 
			} 
		} catch (SQLException e) { 
			e.printStackTrace(); 
		} 
		return user; 
	} 
 
	public void addUser(UserEntity user) { 
		try { 
			con = new BaseDao().getConn(); 
			String sql = "insert into customer values(?,?,?,?,?,?,?,?,?)"; 
			ps = con.prepareStatement(sql); 
			ps.setInt(1, user.getId()); 
			ps.setString(2, user.getName()); 
			ps.setString(3, ""); 
			ps.setString(4, user.getPsw()); 
			ps.setString(5, ""); 
			ps.setString(6, ""); 
			ps.setString(7, ""); 
			ps.setDate(8, new java.sql.Date(user.getRegTime().getTime())); 
			if(user.isStatus()) 
				ps.setInt(9, 1); 
			else 
				ps.setInt(9, 1); 
			ps.executeUpdate(); 
		} catch (SQLException e) { 
			e.printStackTrace(); 
		} 
	} 
 
	// public void addAddress(int uid, String concreteAddress) { 
	// try { 
	// con = new BaseDao().getConn(); 
	// ps = con 
	// .prepareStatement("insert into SendMealAddress values(?,null,?,'��'"); 
	// ps.setInt(1, uid); 
	// ps.setString(2, concreteAddress); 
	// ps.executeUpdate(); 
	// } catch (SQLException e) { 
	// e.printStackTrace(); 
	// } 
	// } 
 
	public void modifyUserState(int uid, String state) { 
		try { 
			con = new BaseDao().getConn(); 
			ps = con 
					.prepareStatement("update Users set StartUpState=? where UserId=?"); 
 
			ps.setString(1, state); 
			ps.setInt(2, uid); 
 
			ps.executeUpdate(); 
		} catch (SQLException e) { 
			e.printStackTrace(); 
		} 
	} 
 
 
	public void modifyUserLastLoginTime(int uid) { 
		try { 
			con = new BaseDao().getConn(); 
			ps = con 
					.prepareStatement("update Users set LastLoginTime=? where UserId=?"); 
 
			ps.setString(1, new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date())); 
			ps.setInt(2, uid); 
 
			ps.executeUpdate(); 
		} catch (SQLException e) { 
			e.printStackTrace(); 
		} 
	} 
 
	public void modifyUserById(UserEntity user) { 
		// try { 
		// con = new BaseDao().getConn(); 
		// String sqlString = "update 
		// StuNum=?,LoginName=?,LoginPwd=?,TrueName=?," 
		// + 
		// "IdCard=?,StartUpState=?,CreateTime=?,LastLoginTime=?,AllOrderCount=? 
		// " 
		// + "where UserId=?"; 
		// ps = con.prepareStatement(sqlString); 
		// 
		// ps.setString(1, user.getStuNum()); 
		// ps.setString(2, user.getLoginName()); 
		// ps.setString(3, user.getLoginPwd()); 
		// ps.setString(4, user.getTrueName()); 
		// ps.setString(5, user.getIdCard()); 
		// ............... 
		// 
		// ps.executeUpdate(); 
		// } catch (SQLException e) { 
		// e.printStackTrace(); 
		// } 
	} 
 
	public void delUserById(int userId) { 
		try { 
			con = new BaseDao().getConn(); 
			ps = con.prepareStatement("delete from Users where UserId=?"); 
			ps.setInt(1, userId); 
 
			ps.executeUpdate(); 
		} catch (SQLException e) { 
			e.printStackTrace(); 
		} 
	} 
 
	public AdminEntity findAdminIsExist(String uname) { 
		AdminEntity admin = null; 
		try { 
			con = new BaseDao().getConn(); 
			ps = con 
					.prepareStatement("select * from admin where username=?"); 
			ps.setString(1, uname); 
			rs = ps.executeQuery(); 
			while(rs.next()){ 
				admin = new AdminEntity(); 
				admin.setAdminId(rs.getInt("id")); 
				admin.setAdminName(uname); 
				admin.setAdminPwd(rs.getString("password")); 
				if(rs.getInt("isSuper")==1){ 
					admin.setSuper(true); 
				}else{ 
					admin.setSuper(false); 
				} 
			} 
		} catch (SQLException e) { 
			e.printStackTrace(); 
		} 
		return admin; 
	} 
}