javaweb中增、删、改、查方法

1.添加

public int addept(department dept) {
		conn=dao.getConnection();
		int count=0;
		String sql="insert into t_dept(p_name) values(?)";
		try {
			pstmt=conn.prepareStatement(sql);
			pstmt.setString(1,dept.getPname());
			count=pstmt.executeUpdate();
			if(count!=0){
				System.out.println("** 添加成功 **");
			}
			else{
				System.out.println("** 添加失败 **");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return count;
	}
2.删除

public int deldept(department dept) {
		conn=dao.getConnection();
		int count=0;
		String sql="delete from t_dept where p_id=?";
		try {
			pstmt=conn.prepareStatement(sql);
			pstmt.setInt(1,dept.getPid());
			count=pstmt.executeUpdate();
			if(count!=0){
				System.out.println("** 删除成功 **");
			}
			else{
				System.out.println("** 删除失败 **");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return count;
	}

3.根据ID查询信息

public Map alldeptById(department dept) {
		conn=dao.getConnection();
		Map map=new HashMap();
		String sql="select * from t_dept where p_id=?";
		try {
			pstmt=conn.prepareStatement(sql);
			pstmt.setInt(1,dept.getPid());
			rs=pstmt.executeQuery();
			while(rs.next()){
				map.put("pid",rs.getInt("p_id"));
				map.put("pname",rs.getString("p_name"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return map;
	}
4.修改

public int uppdept(department dept) {
		conn=dao.getConnection();
		int count=0;
		String sql="update t_dept set p_name=? where p_id=?";
		try {
			pstmt=conn.prepareStatement(sql);
			pstmt.setString(1,dept.getPname());
			pstmt.setInt(2,dept.getPid());
			count=pstmt.executeUpdate();
			if(count!=0){
				System.out.println("** 修改成功 **");
			}
			else{
				System.out.println("** 修改失败 **");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return count;
	}
5.全查询

public List<department> alldept() {
		conn=dao.getConnection();
		String sql="select * from t_dept";
		List<department> list=new ArrayList<department>();
		try {
			pstmt=conn.prepareStatement(sql);
			rs=pstmt.executeQuery();
			while(rs.next()){
				department dept=new department();
				dept.setPid(rs.getInt("p_id"));
				dept.setPname(rs.getString("p_name"));
				list.add(dept);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}
6.统计总记录

public int getTotalPage() {
		int count=0;
		conn=dao.getConnection();
		String sql="select count(*) from t_dept";
		try {
			pstmt=conn.prepareStatement(sql);
			rs=pstmt.executeQuery();
			while(rs.next()){
				count=rs.getInt(1);
			}
			count=(int)Math.ceil((count+1.0-1.0)/DATA_PER_PAGE);//4
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return count;
	}
7.分页查询

public List<department> getpadingByPage(int cur){
		conn=dao.getConnection();
		List<department> list=new ArrayList<department>();
		String sql="select * from t_dept limit ?,?";
		try {
			pstmt=conn.prepareStatement(sql);
			pstmt.setInt(1, (cur - 1) * DATA_PER_PAGE);
			pstmt.setInt(2, DATA_PER_PAGE);
			rs=pstmt.executeQuery();
			while(rs.next()){
				department dept=new department();
				dept.setPid(rs.getInt("p_id"));
				dept.setPname(rs.getString("p_name"));
				list.add(dept);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}
8.用户登录

public int logemp(String uname, String pwd) {
		int count=0;
		conn=dao.getConnection();
		String sql="select count(*) from t_user where t_username='"+uname+"' and t_password='"+pwd+"'";
		try {
			pstmt=conn.prepareStatement(sql);
			rs=pstmt.executeQuery();
			while(rs.next()){
				count=rs.getInt(1);
				if(count!=0){
					System.out.println("** 登录成功 **");
				}
				else{
					System.out.println("** 登录失败 **");
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return count;
	}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值