package bdqn.newsMange.Dao.Impl; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import bdqn.newsMange.Dao.BaseDao; import bdqn.newsMange.Dao.UserDao; import bdqn.newsMange.entity.User; public class UserDaoImpl extends BaseDao implements UserDao { //查询全部 public List<User> getAllUesr() { // TODO Auto-generated method stub List<User> userlist=new ArrayList<User>(); String sql="select userId,userName,userPassword,uRole from [users]"; try { ResultSet rs=executeQurey(sql, null); while(rs.next()){ User user=new User(); user.setUserId(rs.getInt(1)); user.setUserName(rs.getString(2)); user.setUserPassword(rs.getString(3)); user.setuRole(rs.getString(4)); userlist.add(user); } } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ closeAll(); } return userlist; } //添加(注册) public int addUser(User user) { int rel=0; String sql="insert users (userName,userPassword,uRole)values(?,?,'普通用户')"; List<Object>prams=new ArrayList<Object>(); //User use=new User(); prams.add(user.getUserName()); prams.add(user.getUserPassword()); try { rel=executeUpdate(sql, prams); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ closeAll(); } return rel; } public int updateUser(User user) { int rel=0; String sql="update users set userName=?,userPassword=?,uRole=? where userID=? "; List<Object> prams=new ArrayList<Object>(); User use=new User(); use.setUserName(user.getUserName()); use.setUserPassword(user.getUserPassword()); use.setuRole(user.getuRole()); prams.add(use); try { rel=executeUpdate(sql, prams); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ closeAll(); } return rel; } public int delUser(int userId) { int rel=0; String sql="delete from users where userID=0"; List<Object> prams=new ArrayList<Object>(); User use=new User(); use.setUserId(userId); try { rel=executeUpdate(sql, prams); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ closeAll(); } return rel; } public User getLoginUser(String uName) { String sql="select userId,userName,userPassword,uRole from users where userName=?"; User user=null; List<Object> prams=new ArrayList<Object>(); prams.add(uName); try { ResultSet rs=executeQurey(sql, prams); if(rs.next()){ user=new User(); user.setUserName(rs.getString("userName")); user.setUserPassword(rs.getString("userPassword")); user.setUserId(rs.getInt("userId")); user.setuRole(rs.getString("uRole")); } } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return user; } }