www.pudn.com > Java_web_shipin_gongsi-.rar > UserDao.java, change:2011-05-15,size:3347b


package dao; 
 
import java.sql.Connection; 
import java.sql.PreparedStatement; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
import java.util.ArrayList; 
import bean.User; 
import common.DBConnection; 
public class UserDao { 
	public User userLogin(User u){ 
		Connection con = DBConnection.getDBConnection(); 
		try { 
			PreparedStatement ps = con 
					.prepareStatement("select id,pwd,role from user_table where id=? and pwd=?"); 
			ps.setString(1, u.getId()); 
			ps.setString(2,u.getPwd()); 
			ResultSet rs = ps.executeQuery(); 
			if (rs.next()) { 
				User t=new User(); 
				t.setId(rs.getString("id")); 
				t.setPwd(rs.getString("pwd")); 
				t.setRole(rs.getString("role")); 
				 
				return t; 
			} 
			DBConnection.close(con); 
		} catch (SQLException e) { 
			e.printStackTrace(); 
		} 
		return null; 
	} 
	public int addUser(User u){ 
		Connection con = DBConnection.getDBConnection(); 
		try { 
			PreparedStatement ps = con 
					.prepareStatement("insert into user_table(id,pwd,role) values(?,?,?)"); 
			ps.setString(1, u.getId()); 
			ps.setString(2,u.getPwd()); 
			ps.setString(3,u.getRole()); 
			int i= ps.executeUpdate(); 
			con.commit(); 
			DBConnection.close(con); 
			return i; 
		} catch (SQLException e) { 
			e.printStackTrace(); 
		}		 
		return 0; 
	} 
	public int updateUser(User u){ 
		Connection con = DBConnection.getDBConnection(); 
		try { 
			PreparedStatement ps = con 
					.prepareStatement("update user_table set pwd=?,role=? where id=?"); 
			ps.setString(1, u.getPwd()); 
			ps.setString(2,u.getRole()); 
			ps.setString(3,u.getId()); 
			int i= ps.executeUpdate(); 
			con.commit(); 
			DBConnection.close(con); 
			return i; 
		} catch (SQLException e) { 
			e.printStackTrace(); 
		}		 
		return 0;				 
	} 
	public int deleteUser(String id){ 
		Connection con = DBConnection.getDBConnection(); 
		try { 
			PreparedStatement ps = con 
					.prepareStatement("delete from  user_table where id=?"); 
			ps.setString(1, id); 
			int i= ps.executeUpdate(); 
			con.commit(); 
			DBConnection.close(con); 
			return i; 
		} catch (SQLException e) { 
			e.printStackTrace(); 
		}		 
		return 0;				 
	} 
	public ArrayList<User> getAllUser(){ 
		ArrayList<User> userList = new ArrayList<User>(); 
		Connection con = DBConnection.getDBConnection(); 
		try { 
			PreparedStatement ps = con.prepareStatement("select id,pwd,role from user_table"); 
			ResultSet rs = ps.executeQuery(); 
			while (rs.next()) { 
				User i=new User(); 
				i.setId(rs.getString("id")); 
				i.setPwd(rs.getString("pwd")); 
				i.setRole(rs.getString("role")); 
				userList.add(i); 
			} 
			DBConnection.close(con); 
		} catch (SQLException e) { 
			e.printStackTrace(); 
			return null; 
		} 
		return userList;					 
	} 
	public User getUserInfoById(String id){ 
		Connection con = DBConnection.getDBConnection(); 
		try { 
			PreparedStatement ps = con.prepareStatement("select id,pwd,role from user_table where id=?"); 
			ps.setString(1, id); 
			ResultSet rs = ps.executeQuery(); 
			 
			if(rs.next()) { 
				User i=new User(); 
				i.setId(rs.getString("id")); 
				i.setPwd(rs.getString("pwd")); 
				i.setRole(rs.getString("role")); 
				DBConnection.close(con); 
				return i; 
			} 
			return null; 
		} catch (SQLException e) { 
			e.printStackTrace(); 
			return null; 
		}				 
	}		 
}