首页 > 代码库 > 学生选课系统----StudentDAO

学生选课系统----StudentDAO

/** * @author 李佩 *  * @version v1 *  * @time 2016/12/5 *  * @program  已选课程信息与数据库进行操作的类 * */package com.csms.dao;import java.sql.*;import java.util.ArrayList;import java.util.List;import com.csms.entity.*;import com.csms.DBLink;public class StudentDAO {	// 学生登录信息查询	public Boolean loginConfirm(String name, String password) {		// 创建学生对象		Student student = new Student();		// 1.取得数据库连接的对象conn		Connection conn = DBLink.getConn();		// 2. 查询全体学生的sql语句		String sql = "SELECT loginName,loginPsd FROM student";		Statement stm = null;		ResultSet rs = null;		try {			// 3.将查询语句发送给数据库,执行查询操作			stm = conn.createStatement();			rs = stm.executeQuery(sql);			// 逐条记录进行查询			while (rs.next()) {				student.setLoginName(rs.getString("loginName"));				student.setLoginPSD(rs.getString("loginPsd"));				if (student.getLoginName().equals(name) && student.getLoginPSD().equals(password)) {					return true;				}			}			// 关闭查询指针链接			rs.close();		} catch (SQLException e) {			e.printStackTrace();		} finally {			// 4. 操作完成,关闭连接			try {				if (stm != null)					stm.close();			} catch (SQLException e) {				// TODO Auto-generated catch block				e.printStackTrace();			}		}		return false;	}	// 查询某个学生的所有信息,用于登录标题窗口显示学生的姓名	public Student searchStudentAll(String loginName, String loginPsd) {		// 设置学生对象		Student student = null;		// 1.取得数据库连接的对象conn		Connection conn = DBLink.getConn();		// 2. 查询全体学生的sql语句		String sql = "SELECT stuName,stuID,stuAge,stuSdept,stuClass,stuGender,loginPsd FROM student WHERE loginName=? AND loginPsd=?";		PreparedStatement ptm = null;		ResultSet rs = null;		try {			// 3.将查询语句发送给数据库,执行查询操作			ptm = conn.prepareStatement(sql);			ptm.setString(1, loginName);			ptm.setString(2, loginPsd);			rs = ptm.executeQuery();			student = new Student();			// 逐条记录进行查询			while (rs.next()) {				student.setStuName(rs.getString("stuName"));				student.setStuID(rs.getString("stuID"));				student.setStuAge(rs.getInt("stuAge"));				student.setStuSdept(rs.getString("stuSdept"));				student.setStuClass(rs.getString("stuClass"));				student.setStuGender(rs.getString("stuGender"));				student.setLoginPSD(rs.getString("loginPsd"));			}			// 关闭查询指针链接			rs.close();		} catch (SQLException e) {			e.printStackTrace();		} finally {			// 4. 操作完成,关闭连接			try {				if (ptm != null)					ptm.close();			} catch (SQLException e) {				// TODO Auto-generated catch block				e.printStackTrace();			}		}		return student;	}	// 学生密码修改操作	public void updatePassword(String id, String password) {		// 1.取得数据库连接的对象conn		Connection conn = DBLink.getConn();		// 2. 查询全体学生的sql语句		String sql = "UPDATE student SET loginPsd=? WHERE stuID=?";		PreparedStatement ptm = null;		try {			// 3.将查询语句发送给数据库,执行查询操作			ptm = conn.prepareStatement(sql);			ptm.setString(1, password);			ptm.setString(2, id);			ptm.executeUpdate();		} catch (SQLException e) {			e.printStackTrace();		} finally {			// 4. 操作完成,关闭连接			try {				if (ptm != null)					ptm.close();			} catch (SQLException e) {				e.printStackTrace();			}		}	}	// 录入学生信息	public void insertStudentInformation(Student student){		Connection conn = DBLink.getConn();		String sql = "INSERT INTO student (loginName,loginPsd,stuID,stuName,stuAge,stuGender,stuSdept,stuClass) VALUES(?,?,?,?,?,?,?,?)";		PreparedStatement ptm = null;		ResultSet rs = null;		try{			ptm = conn.prepareStatement(sql);			ptm.setString(1, student.getLoginName());			ptm.setString(2, student.getLoginPSD());			ptm.setString(3, student.getStuID());			ptm.setString(4, student.getStuName());			ptm.setInt(5, student.getStuAge());			ptm.setString(6, student.getStuGender());			ptm.setString(7, student.getStuSdept());			ptm.setString(8, student.getStuClass());			ptm.execute();		}catch(SQLException e){			e.printStackTrace();		}finally{			try{				if(ptm!=null)					ptm.close();			}catch(SQLException e){				e.printStackTrace();			}		}	}		// 删除学生信息	public void deleteStudentInformation(String id){		Connection conn = DBLink.getConn();		String sql = "DELETE FROM student WHERE stuID=?";		PreparedStatement ptm = null;		try{			ptm = conn.prepareStatement(sql);			ptm.setString(1, id);			ptm.execute();		}catch(SQLException e){			e.printStackTrace();		}finally{			try{				if(ptm != null){					ptm.close();				}			}catch(SQLException e){				e.printStackTrace();			}		}	}		// 查询所有学生	public List<Student> searchAllStudent(){		List<Student> list = new ArrayList<Student>();		Student student = null;		Connection conn = DBLink.getConn();		String sql = "SELECT stuID,stuName,stuAge,stuGender,stuSdept,stuClass FROM student";		PreparedStatement ptm = null;		ResultSet rs = null;		try{			ptm = conn.prepareStatement(sql);			rs = ptm.executeQuery();			while(rs.next()){				student = new Student();				student.setStuID(rs.getString("stuID"));				student.setStuName(rs.getString("stuName"));				student.setStuAge(rs.getInt("stuAge"));				student.setStuGender(rs.getString("stuGender"));				student.setStuSdept(rs.getString("stuSdept"));				student.setStuClass(rs.getString("stuClass"));				list.add(student);			}			rs.close();		}catch(SQLException e){			e.printStackTrace();		}finally{			try{				if(ptm!=null){					ptm.close();				}			}catch(SQLException e){				e.printStackTrace();			}		}		return list;	}}

  

学生选课系统----StudentDAO