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

学生选课系统----TeacherDAO

  老师信息操作:

/** * @author 李佩 *  * @version v1 *  * @time 2016/12/2 14:54 *  * @program 老师信息与数据库之间的操作 * */package com.csms.dao;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.ArrayList;import java.util.List;import com.csms.DBLink;import com.csms.entity.Student;import com.csms.entity.Teacher;public class TeacherDAO {	// 验证老师登录学生选课系统	public Boolean loginConfirm(String name, String password) {		// 定义一个老师对象		Teacher teacher = new Teacher();		// 1.取得数据库连接的对象conn		Connection conn = DBLink.getConn();		// 2.查询全体老师的sql语句		String sql = "SELECT loginName,loginPsd FROM teacher";		Statement stm = null;		ResultSet rs = null;		try {			// 3.将查询语句发送给数据库,执行查询操作			stm = conn.createStatement();			rs = stm.executeQuery(sql);			while (rs.next()) {				teacher.setLoginName(rs.getString("loginName"));				teacher.setLoginPSD(rs.getString("loginPsd"));				if (name.equals(teacher.getLoginName()) && password.equals(teacher.getLoginPSD())) {					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 Teacher searchTeacherInformation(String loginName, String loginPassword) {		// 创建一个老师对象		Teacher teacher = null;		// 1. 取得数据库连接对象		Connection conn = DBLink.getConn();		// 2. 写出sql语句		String sql = "SELECT teacID,teacName,teacAge,teacGender,loginPsd FROM teacher WHERE loginName=? AND loginPsd=?";		// 操作数据库语句变量		PreparedStatement ptm = null;		ResultSet rs = null;		try {			// 整合数据库语句,执行查询			ptm = conn.prepareStatement(sql);			ptm.setString(1, loginName);			ptm.setString(2, loginPassword);			rs = ptm.executeQuery();			while (rs.next()) {				teacher = new Teacher();				teacher.setTeacID(rs.getString("teacID"));				teacher.setTeacName(rs.getString("teacName"));				teacher.setTeacAge(rs.getInt("teacAge"));				teacher.setTeacGender(rs.getString("teacGender"));				teacher.setLoginPSD(rs.getString("loginPsd"));			}			System.out.println(teacher.getLoginPSD());			rs.close();		} catch (SQLException e) {			e.printStackTrace();		}		return teacher;	}	// 更新老师密码	public void updatePassword(String teacherID, String password) {		Connection conn = DBLink.getConn();		String sql = "UPDATE teacher SET loginPsd=? WHERE teacID=?";		PreparedStatement ptm = null;		try {			ptm = conn.prepareStatement(sql);			ptm.setString(1, password);			ptm.setString(2, teacherID);			ptm.executeUpdate();		} catch (SQLException e) {			e.printStackTrace();		} finally {			try {				if (ptm != null)					ptm.close();			} catch (SQLException e) {				e.printStackTrace();			}		}	}	// 插入老师信息	public void insertInformation(Teacher teacher){		Connection conn = DBLink.getConn();		String sql = "INSERT INTO teacher(loginName,loginPsd,teacID,teacName,teacAge,teacGender) VALUES(?,?,?,?,?,?)";				PreparedStatement ptm = null;				try{			ptm = conn.prepareStatement(sql);			ptm.setString(1, teacher.getLoginName());			ptm.setString(2, teacher.getLoginPSD());						ptm.setString(3, teacher.getTeacID());			ptm.setString(4, teacher.getTeacName());			ptm.setInt(5, teacher.getTeacAge());			ptm.setString(6, teacher.getTeacGender());			ptm.execute();		}catch(SQLException e){			e.printStackTrace();		}finally{			try{				if(ptm!=null)					ptm.close();			}catch(SQLException e){				e.printStackTrace();			}		}	}	// 删除老师信息	public void deleteTeacherInformation(String id){		Connection conn = DBLink.getConn();		String sql = "DELETE FROM teacher WHERE teacID=?";		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<Teacher> searchAllTeacherInformation(){		List<Teacher> list = new ArrayList<Teacher>();		Teacher teacher = null;		Connection conn = DBLink.getConn();		String sql = "SELECT teacID,teacName,teacAge,teacGender FROM teacher";		PreparedStatement ptm = null;		ResultSet rs = null;		try{			ptm = conn.prepareStatement(sql);			rs = ptm.executeQuery();			while(rs.next()){				teacher = new Teacher();				teacher.setTeacID(rs.getString("teacID"));				teacher.setTeacName(rs.getString("teacName"));				teacher.setTeacAge(rs.getInt("teacAge"));				teacher.setTeacGender(rs.getString("teacGender"));				list.add(teacher);			}			rs.close();		}catch(SQLException e){			e.printStackTrace();		}finally{			try{				if(ptm!=null){					ptm.close();				}			}catch(SQLException e){				e.printStackTrace();			}		}		return list;	}}

  

学生选课系统----TeacherDAO