学生选课系统----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;
	}
}

  

时间: 2024-11-03 20:58:39

学生选课系统----TeacherDAO的相关文章

学生选课系统----LoginWindow

/** * @author 李佩 * * @version v1 * * @time 2016/12/1 15:37 * * @program 创建对应数据表实体的老师类,方便dao与数据库的连接 * */ package com.csms.windows; // 导入要用到的包 import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import com.csm

学生选课系统----TeacherWindow

/** * @author 李佩 * * @version v1 * * @time 2016/12/6 9:29 * * @program 画出学生选课系统的学生界面 * */ package com.csms.windows; import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.even

3.python小项目:学生选课系统

学生选课系统 编程核心:在对象中封装对象 目录结构: 1.administrator.py import random import os import sys sys.path.append(os.path.dirname(os.path.dirname(__file__))) import time import pickle from lib import models from config import settings from lib.models import * # 全部导入

学生选课系统----StudentWindow

/** * @author 李佩 * * @version v1 * * @time 2016/12/2 22:21 * * @program 画出学生选课系统的学生界面 * */ package com.csms.windows; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.table.*; import java.util.List; import com.csms.

JAVA | 学生选课系统

这里使用JAVA语言编写的简易的学生选课系统,展现的都是这个系统核心代码. 其中有不足欢迎批评和指正! 链接数据库的代码 package connection;//连接数据库student import java.sql.Connection;import java.sql.DriverManager;//import java.sql.Statement;import java.sql.SQLException; public class xu{ private static Connecti

学生选课系统数据存储用shelve

工程目录: #bin/run.py from core.main import Run Run.login() #core/ADM.py #-*-coding:utf-8 -*- __author__ = 'MrChu' import hashlib import shelve #shelve={'Student':[{'name':'xxx','passWord':'xxx','course':[]},{...},{...}], # 'Administrators':[{'name':'xxx

面向对象的应用:学生选课系统

一.要求: 选课系统 管理员: 创建老师:姓名.性别.年龄.资产 创建课程:课程名称.上课时间.课时费.关联老师 使用pickle保存在文件 学生: 学生:用户名.密码.性别.年龄.选课列表[].上课记录{课程1:[di,a,]} 1.列举所有课程 2.选择课程 3.学生上课, 4.ret = 课程.work() 获取课程的返回; 资产+=课时费 二.代码思路 2.1 管理员系统 a.定义了Teacher类,name,age,gender,property,其中property为私有成员,用对象

学生选课系统----SelectCourseDAO

/** * @author 李佩 * * @version v1 * * @time 2016/12/5 22:03 * * @program 已选课程信息与数据库进行操作的类 * */ package com.csms.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.ut

学生选课系统----实体类

学生: package com.csms.entity; /** * @author 李佩 * * @version v1 * * @time 2016/12/1 10:10 * * @program 创建对应数据表实体的学生类,方便dao与数据库的连接 * */ public class Student { // 学生登录系统的登录名 private String loginName; // 登录密码 private String loginPSD; // 学生学号 private Strin