javaweb学生管理系统-----管理用户的实现

javaweb学生管理系统-----管理用户的实现

一、管理用户类的实现

public class StuDao {
	
	//增加用户
	public String addstudent(Student student) {
		String info = "success";
		String sql = "insert into students values(?,?,?,?,?,?,?)";	
		
		//执行更新语句
		Connection conn = null;
		PreparedStatement ps = null;
		try {
			conn = DBUtil.getConnection();
			ps = conn.prepareStatement(sql);
			
			if(!check(student)) {
				//设置参数
				ps.setString(1, student.getId());
				ps.setString(2, student.getName());
				ps.setInt(3, student.getAge());
				ps.setString(4, student.getSex());
				ps.setInt(5, student.getCore());
				ps.setString(6, student.getUserName());
				ps.setString(7, student.getPassword());
				//执行更新语句
				int r = ps.executeUpdate();
				if (r <= 0) info = "fail";
				System.out.println("addstudent()...");
			}else {
				info="fail";
			}
			
		}catch(SQLException e){
			e.printStackTrace();
		} finally{
			// 关闭数据库连接
			DBUtil.close(null,ps,conn);
		}
		return info;
	}
	//用户是否在数据库中,对用户输入的用户数据进行检查。
	public boolean check(Student student) {
		boolean ret = false;
		String sql = "select count(*) from students where id=? and username=? and password=?";
		
		//执行查询语句
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			//获得连接
			conn = DBUtil.getConnection();
			//执行查询
			ps = conn.prepareStatement(sql);
			ps.setString(1, student.getId());
			ps.setString(2, student.getUserName());
			ps.setString(3, student.getPassword());
			rs = ps.executeQuery();
		
			//处理查询结果
			if(rs.next()){
				int r = rs.getInt(1);
				if(r==1) ret = true;
					
			}
			System.out.println("check()...");
		} catch (SQLException e) {
			e.printStackTrace();
		} finally{
			//关闭连接
			DBUtil.close(rs, ps, conn);
		}
		
		return ret;
	}
	
	//从数据库中取数据
	public Student getStudent(Student student) {
		String sql = "select * from students";
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			//获得连接
			conn = DBUtil.getConnection();
			//执行查询
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
		
			//处理查询结果
			while (rs.next()) {
				String id =rs.getString("id");
				if(student.getId().equals(id)) {
					student.setName(rs.getString("name"));
					student.setAge(rs.getInt("age"));
					student.setSex(rs.getString("sex"));
					student.setCore(rs.getInt("core"));
					student.setUserName(rs.getString("username"));
					student.setPassword(rs.getString("password"));
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally{
			//关闭连接
			DBUtil.close(rs, ps, conn);
		}
		return  student;
	}
	
	// 修改学生信息
			public String updateStudent(Student student) {
				String info = "success";
				String sql = "update students set username=?,password=? where id=?";

				// 执行更新语句
				Connection conn = null;
				PreparedStatement ps = null;
				try{
					conn = DBUtil.getConnection();
					ps = conn.prepareStatement(sql);
					//设置参数(注意参数顺序!)
					ps.setString(1, student.getUserName());
					ps.setString(2, student.getPassword());
					ps.setString(3, student.getId());
					//执行更新语句
					int r = ps.executeUpdate();
					if (r <= 0) info = "fail";
				}
				catch(SQLException e){
					e.printStackTrace();
				} finally{
					// 关闭数据库连接
					DBUtil.close(null,ps,conn);
				}
						
				return info;
			}
	
	/* 测试拿取数据库数据
	public static void main(String[] args) {
		Student student=new Student();
		student.setId("100");
		StuDao stuDao=new StuDao();
		stuDao.getStudent(student);
		System.out.println(student.toString());
	}*/
	
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zzbkong

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值