• 新闻发布项目——数据实现类(UserDaoImpl)


    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;
    	}
    
    }
    

  • 相关阅读:
    【转载】USB2.0接口差分信号线设计
    2.4G高频PCB天线设计
    [转]热插拔原理和应用
    [转]or cad drc 错误
    Chrome浏览器任意修改网页内容
    Oracle笔记之约束
    Oracle笔记之表空间
    Oracle笔记之序列(Sequence)
    Oracle笔记之用户管理
    Win7下SQLPlus登录时报错"SP2-1503:无法初始化Oracle调用界面"
  • 原文地址:https://www.cnblogs.com/a1111/p/12816557.html
Copyright © 2020-2023  润新知