项目——学生信息管理系统5

目录

教师模块功能的实现

创建一个 Teacher 实体类

创建 添加老师界面 AddTeacherFrm 注意创建成 JInternalFrame 类型

页面的制作

给添加按钮绑定事件

提供一个重置表单功能

回到 MainFrm 添加教师管理的按钮

给添加教师按钮绑定事件

测试添加教师功能

创建教师信息管理页面 ManageTeacherFrm 注意创建成:JInternalFrame类型

制作 ManageTeacherFrm 页面

再制作其他组件 效果如下

注意:记得把 两个单选按钮添加到按钮组,怎么添加,参照之前的

回到MainFrm 给教师列表按钮添加事件

测试一下

回到 ManageTeacherFrm

当页面一打开,查询所有教师,给表格填充数据

首先在TeacherDao中提供查询教师的方法

在 ManageTeacherFrm 定义填充数据的方法

在 ManageTeacherFrm 构造方法中调用 setTable()方法

测试

给查询按钮绑定事件

给表格的每一行添加事件

给确认修改按钮绑定事件

首选在 TeacherDao中提供修改方法

给删除按钮添加事件

启动测试一下删除功能

回到 LoginFrm 判断如果是教师登录

首先在 TeacherDao 中提供登录的方法

在 LoginFrm 中进行判断

再回到 EditPwdFrm 页面 进行教师身份的判断

在构造方法中判断

在 TeacherDao 中提供修改密码的方法

在 EditPwdFrm 中修改submitEdit()方法

启动测试


教师模块功能的实现

创建一个 Teacher 实体类

public class Teacher {
    private int id;
    private String name;
    private String sex;
    private String title;
    private int age;
    private String password;
    //get set 方法,自己补上 
}   

创建 添加老师界面 AddTeacherFrm 注意创建成 JInternalFrame 类型

页面的制作

给组件起个名字

teacherNameTextField; teacherTitleTextField; teacherAgeTextField; teacherSexManRadioButton; teacherSexFemalRadioButton; teacherPasswordField;

给添加按钮绑定事件

//添加按钮绑定事件
        addTeacherButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                addTeacherAct(e);
            }
        });
//添加教师功能
    protected void addTeacherAct(ActionEvent ae) {
        // TODO Auto-generated method stub
        String teacherName = teacherNameTextField.getText().toString();
        String teacherSex = teacherSexManRadioButton.isSelected() ? teacherSexManRadioButton.getText().toString()
                : teacherSexFemalRadioButton.getText().toString();
        String teacherTitle = teacherTitleTextField.getText().toString();
        String teacherPassword = teacherPasswordField.getText().toString();
        int teacherAge = 0;
        try {
            teacherAge = Integer.parseInt(teacherAgeTextField.getText().toString());
        } catch (Exception e) {
            // TODO: handle exception
            JOptionPane.showMessageDialog(this, "年龄只允许输入数字!");
            return;
        }
        if (StringUtil.isEmpty(teacherName)) {
            JOptionPane.showMessageDialog(this, "教师姓名必须填写!");
            return;
        }
        if (StringUtil.isEmpty(teacherTitle)) {
            JOptionPane.showMessageDialog(this, "教师职称必须填写!");
            return;
        }
        if (teacherAge == 0 || teacherAge < 0) {
            JOptionPane.showMessageDialog(this, "教师年龄必须大于0!");
            return;
        }
        if (StringUtil.isEmpty(teacherPassword)) {
            JOptionPane.showMessageDialog(this, "教师登录密码必须填写!");
            return;
        }
        Teacher teacher = new Teacher();
        teacher.setName(teacherName);
        teacher.setSex(teacherSex);
        teacher.setTitle(teacherTitle);
        teacher.setAge(teacherAge);
        teacher.setPassword(teacherPassword);
        TeacherDao teacherDao = new TeacherDao();
        if (teacherDao.addTeacher(teacher)) {
            JOptionPane.showMessageDialog(this, "教师添加成功!");
        } else {
            JOptionPane.showMessageDialog(this, "教师添加失败!");
        }
        //添加完重一下表单
        resetValue(ae);
    }
​
    

提供一个重置表单功能

// 重置表单
    protected void resetValue(ActionEvent ae) {
        // TODO Auto-generated method stub
        teacherNameTextField.setText("");
        teacherTitleTextField.setText("");
        teacherAgeTextField.setText("");
        teacherSexManRadioButton.setSelected(true);
        teacherPasswordField.setText("");
    }

回到 MainFrm 添加教师管理的按钮

给添加教师按钮绑定事件

//添加教师按钮事件
        mntmNewMenuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                //打开添加教师页面
                openAddTeacherFrm(e);
            }
        });
//打开添加教师页面
    protected void openAddTeacherFrm(ActionEvent e) {
        // TODO Auto-generated method stub
        AddTeacherFrm addTeacherFrm = new AddTeacherFrm();
        addTeacherFrm.setVisible(true);
        desktopPane.add(addTeacherFrm);
    }

测试添加教师功能

创建教师信息管理页面 ManageTeacherFrm 注意创建成:JInternalFrame类型

制作 ManageTeacherFrm 页面

再制作其他组件 效果如下

给组件起名字

private JTable teacherListTable; ​ private JTextField searchTeacherNameTextField; ​ private JTextField editTeacherNameTextField; ​ private JTextField editTeacherTitleTextField; ​ private JTextField editTeacherAgeTextField; ​ private JPasswordField editTeacherPasswordField; ​ private JRadioButton editTeacherSexManRadioButton; ​ private JRadioButton editTeacherSexFemalRadioButton; ​ private JButton deleteTeacherButton;

注意:记得把 两个单选按钮添加到按钮组,怎么添加,参照之前的

回到MainFrm 给教师列表按钮添加事件

    //教师列表按钮,事件绑定
        menuItem_7.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                openManageTeacherFrm(e);
            }
        });
//打开教师列表页面
    protected void openManageTeacherFrm(ActionEvent e) {
        ManageTeacherFrm manageTeacherFrm = new ManageTeacherFrm();
        manageTeacherFrm.setVisible(true);
        desktopPane.add(manageTeacherFrm);
        
    }

测试一下

回到 ManageTeacherFrm

当页面一打开,查询所有教师,给表格填充数据

首先在TeacherDao中提供查询教师的方法
//查询教师
	public List<Teacher> getTeacherList(Teacher teacher) {
		// TODO Auto-generated method stub
		List<Teacher> retList = new ArrayList<Teacher>();
		StringBuffer sqlString = new StringBuffer("select * from s_teacher");
		if(!StringUtil.isEmpty(teacher.getName())){
			sqlString.append(" where name like '%"+teacher.getName()+"%'");
		}
		try {
			Connection con = JDBCUtils.getConnection();
			PreparedStatement preparedStatement = con.prepareStatement(sqlString.toString());
			ResultSet executeQuery = preparedStatement.executeQuery();
			while(executeQuery.next()){
				Teacher t = new Teacher();
				t.setId(executeQuery.getInt("id"));
				t.setName(executeQuery.getString("name"));
				t.setSex(executeQuery.getString("sex"));
				t.setTitle(executeQuery.getString("title"));
				t.setAge(executeQuery.getInt("age"));
				t.setPassword(executeQuery.getString("password"));
				retList.add(t);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return retList;
	}

在 ManageTeacherFrm 定义填充数据的方法

// 填充表格数据
	private void setTable(Teacher teacher) {
		if ("教师".equals(MainFrm.userType.getName())) {
			Teacher tLogined = (Teacher) MainFrm.userObject;
			teacher.setName(tLogined.getName());
			searchTeacherNameTextField.setText(teacher.getName());
		}
		DefaultTableModel dft = (DefaultTableModel) teacherListTable.getModel();
		dft.setRowCount(0);
		TeacherDao teacherDao = new TeacherDao();
		List<Teacher> teacherList = teacherDao.getTeacherList(teacher);
		for (Teacher t : teacherList) {
			Vector v = new Vector();
			v.add(t.getId());
			v.add(t.getName());
			v.add(t.getSex());
			v.add(t.getTitle());
			v.add(t.getAge());
			v.add(t.getPassword());
			dft.addRow(v);
		}
	}

在 ManageTeacherFrm 构造方法中调用 setTable()方法

//调用填充表格数据的方法
		setTable(new Teacher());

测试

给查询按钮绑定事件

//查询按钮,绑定事件
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				searchTeacher(e);
			}
		});

//条件查询
	protected void searchTeacher(ActionEvent e) {
		// TODO Auto-generated method stub
		String teacherNameString = searchTeacherNameTextField.getText().toString();
		Teacher teacher = new Teacher();
		teacher.setName(teacherNameString);
		setTable(teacher);
	}

自己启动测试一下

给表格的每一行添加事件

//给表格的每一行添加事件
		teacherListTable.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				//点击表格没一行时,把这行的旧数据展示在编辑框里面
				selectedTableRow(e);
			}
		});

	// 点击表格每一行,展示旧数据
	protected void selectedTableRow(MouseEvent e) {
		DefaultTableModel dft = (DefaultTableModel) teacherListTable.getModel();
		editTeacherNameTextField.setText(dft.getValueAt(teacherListTable.getSelectedRow(), 1).toString());
		editTeacherTitleTextField.setText(dft.getValueAt(teacherListTable.getSelectedRow(), 3).toString());
		editTeacherAgeTextField.setText(dft.getValueAt(teacherListTable.getSelectedRow(), 4).toString());
		editTeacherPasswordField.setText(dft.getValueAt(teacherListTable.getSelectedRow(), 5).toString());
		String sex = dft.getValueAt(teacherListTable.getSelectedRow(), 2).toString();
		if (sex.equals(editTeacherSexManRadioButton.getText())) {
			editTeacherSexManRadioButton.setSelected(true);
		}
		if (sex.equals(editTeacherSexFemalRadioButton.getText())) {
			editTeacherSexFemalRadioButton.setSelected(true);
		}
	}

测试

给确认修改按钮绑定事件

首选在 TeacherDao中提供修改方法

public boolean update(Teacher teacher){
		String sql = "update s_teacher set name=?, sex=?,title=?,age=?,password=? where id=?";
		try {
			Connection con = JDBCUtils.getConnection();
			PreparedStatement preparedStatement = con.prepareStatement(sql);
			preparedStatement.setString(1, teacher.getName());
			preparedStatement.setString(2, teacher.getSex());
			preparedStatement.setString(3, teacher.getTitle());
			preparedStatement.setInt(4, teacher.getAge());
			preparedStatement.setString(5, teacher.getPassword());
			preparedStatement.setInt(6, teacher.getId());
			if(preparedStatement.executeUpdate() > 0){
				return true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

//确认修改按钮,事件绑定
		updateTeacherButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				editTeacherAct(e);
			}
		});

//修改教师
	protected void editTeacherAct(ActionEvent e) {
		int row = teacherListTable.getSelectedRow();
		if(row == -1){
			JOptionPane.showMessageDialog(this, "请选择要修改的数据!");
			return;
		}
		String teacherName = editTeacherNameTextField.getText().toString();
		String teacherSex = editTeacherSexManRadioButton.isSelected() ? editTeacherSexManRadioButton.getText().toString() : editTeacherSexFemalRadioButton.getText().toString();
		String teacherTitle = editTeacherTitleTextField.getText().toString();
		int teacherAge = 0;
		try {
			teacherAge = Integer.parseInt(editTeacherAgeTextField.getText().toString());
		} catch (Exception ex) {
			// TODO: handle exception
			JOptionPane.showMessageDialog(this, "年龄只允许输入数字!");
			return;
		}
		String teacherPassword = editTeacherPasswordField.getText().toString();
		if(StringUtil.isEmpty(teacherName)){
			JOptionPane.showMessageDialog(this, "教师姓名必须填写!");
			return;
		}
		if(StringUtil.isEmpty(teacherTitle)){
			JOptionPane.showMessageDialog(this, "教师职称必须填写!");
			return;
		}
		if(teacherAge == 0 || teacherAge < 0){
			JOptionPane.showMessageDialog(this, "教师年龄必须大于0!");
			return;
		}
		if(StringUtil.isEmpty(teacherPassword)){
			JOptionPane.showMessageDialog(this, "教师登录密码必须填写!");
			return;
		}
		Teacher teacher = new Teacher();
		teacher.setId(Integer.parseInt(teacherListTable.getValueAt(row, 0).toString()));
		teacher.setName(teacherName);
		teacher.setSex(teacherSex);
		teacher.setTitle(teacherTitle);
		teacher.setAge(teacherAge);
		teacher.setPassword(teacherPassword);
		TeacherDao teacherDao = new TeacherDao();
		if(teacherDao.update(teacher)){
			JOptionPane.showMessageDialog(this, "修改成功!");
		}else{
			JOptionPane.showMessageDialog(this, "修改失败!");
		}
		
		//修改完成填充表格数据
		setTable(new Teacher());
		
	}

启动测试一下

给删除按钮添加事件

//删除按钮绑定事件
		deleteTeacherButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				deleteTeacher(e);
			}
		});

// 删除教师
	protected void deleteTeacher(ActionEvent e) {
		int row = teacherListTable.getSelectedRow();
		if (row == -1) {
			JOptionPane.showMessageDialog(this, "请选择要删除的数据!");
			return;
		}
		if (JOptionPane.showConfirmDialog(this, "确定要删除么?") != JOptionPane.OK_OPTION)
			return;
		int id = Integer.parseInt(teacherListTable.getValueAt(row, 0).toString());
		TeacherDao teacherDao = new TeacherDao();
		if (teacherDao.delete(id)) {
			JOptionPane.showMessageDialog(this, "删除成功!");
		} else {
			JOptionPane.showMessageDialog(this, "删除失败!");
		}
		// 删除完填充数据
		setTable(new Teacher());

	}

启动测试一下删除功能

回到 LoginFrm 判断如果是教师登录

首先在 TeacherDao 中提供登录的方法

public Teacher login(Teacher teacher){
		String sql = "select * from s_teacher where name=? and password=?";
		Teacher teacherRst = null;
		try {
			Connection con = JDBCUtils.getConnection();
			PreparedStatement prst = con.prepareStatement(sql);//
			prst.setString(1, teacher.getName());
			prst.setString(2, teacher.getPassword());
			ResultSet executeQuery = prst.executeQuery();
			if(executeQuery.next()){
				teacherRst = new Teacher();
				teacherRst.setId(executeQuery.getInt("id"));
				teacherRst.setName(executeQuery.getString("name"));
				teacherRst.setPassword(executeQuery.getString("password"));
				teacherRst.setSex(executeQuery.getString("sex"));
				teacherRst.setAge(executeQuery.getInt("Age"));
				teacherRst.setTitle(executeQuery.getString("title"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return teacherRst;
	}

在 LoginFrm 中进行判断

// 点击登录按钮进行处理
	protected void userLogin(ActionEvent e) throws SQLException {
		// 获取用户输入的用户名和密码,以及登录类型 按快捷键 ctrl+1 会补全返回值
		String username = usernameTextField.getText().toString().trim();
		String password = passwordTextField.getText().toString().trim();
		// 获取用户的下拉选择
		UserType selectedItem = (UserType) userTypeComboBox.getSelectedItem();
		// 判断用户名和密码是否为空
		if (StringUtil.isEmpty(username)) {
			// 弹出一个提示框进行提示
			JOptionPane.showMessageDialog(this, "用户名不能为空!");
			return; // 注意return掉
		}
		if (StringUtil.isEmpty(password)) {
			JOptionPane.showMessageDialog(this, "密码不能为空!");
			return; // 注意return掉
		}
		// 根据不同的身份进行不同的登录操作
		Admin admin = null;
		if ("管理员".equals(selectedItem.getName())) {
			//代码略...

		} else if ("教师".equals(selectedItem.getName())) {
			//教师登录
			Teacher teacher = null;
			TeacherDao teacherDao = new TeacherDao();
			Teacher teacherTmp = new Teacher();
			teacherTmp.setName(username);
			teacherTmp.setPassword(password);
			teacher = teacherDao.login(teacherTmp);
			if(teacher == null){
				JOptionPane.showMessageDialog(this, "用户名或密码错误!");
				return;
			}
			JOptionPane.showMessageDialog(this, "欢迎【"+selectedItem.getName()+"】:"+teacher.getName()+"登录本系统!");
			this.dispose();
			new MainFrm(selectedItem, teacher).setVisible(true);

		} else {
			//学生登录..代码略
			
		}
	}

再回到 EditPwdFrm 页面 进行教师身份的判断

在构造方法中判断

// 根据不同的身份在,修改密码页面展示用户名
		if ("管理员".equals(MainFrm.userType.getName())) {
			Admin admin = (Admin) MainFrm.userObject;
			cureentUser.setText("【系统管理员】" + admin.getName());
		}else if("学生".equals(MainFrm.userType.getName())){
			Student student = (Student)MainFrm.userObject;
			cureentUser.setText("【学生】" + student.getName());
		}else{
			//教师
			Teacher teacher = (Teacher)MainFrm.userObject;
			cureentUser.setText("【教师】" + teacher.getName());
		}

在 TeacherDao 中提供修改密码的方法

public String editPassword(Teacher teacher,String newPassword){
		String sql = "select * from s_teacher where id=? and password=?";
		PreparedStatement prst = null;
		int id = 0;
		try {
			Connection con = JDBCUtils.getConnection();
			prst = con.prepareStatement(sql);
			prst.setInt(1, teacher.getId());
			prst.setString(2, teacher.getPassword());
			ResultSet executeQuery = prst.executeQuery();
			if(!executeQuery.next()){
				String retString = "旧密码错误";
				return retString;
			}
			id = executeQuery.getInt("id");
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		String retString = "修改失败";
		String sqlString = "update s_teacher set password = ? where id = ?";
		try {
			Connection con = JDBCUtils.getConnection();
			prst = con.prepareStatement(sqlString);
			prst.setString(1, newPassword);
			prst.setInt(2, id);
			int rst = prst.executeUpdate();
			if(rst > 0){
				retString = "密码修改成功";
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return retString;
	}

在 EditPwdFrm 中修改submitEdit()方法

// 确认修改
	protected void submitEdit(ActionEvent e) {
		// 获取输入框的输入内容
		String oldPassword = oldPwdTextField.getText().toString().trim();
		String newPassword = newPwdTextField.getText().toString().trim();
		String conformPassword = okNewPwdTextField.getText().toString().trim();
		if (StringUtil.isEmpty(oldPassword)) {
			JOptionPane.showMessageDialog(this, "请填写旧密码!");
			return;
		}
		if (StringUtil.isEmpty(newPassword)) {
			JOptionPane.showMessageDialog(this, "请填写新密码!");
			return;
		}
		if (StringUtil.isEmpty(conformPassword)) {
			JOptionPane.showMessageDialog(this, "请确认新密码!");
			return;
		}
		if (!newPassword.equals(conformPassword)) {
			JOptionPane.showMessageDialog(this, "两次密码输入不一致!");
			return;
		}
		if ("管理员".equals(MainFrm.userType.getName())) {
			//代码略...
		}
		if ("学生".equals(MainFrm.userType.getName())) {
			//代码略...
		}
		if("教师".equals(MainFrm.userType.getName())){
			TeacherDao teacherDao = new TeacherDao();
			Teacher teacherTmp = new Teacher();
			Teacher teacher = (Teacher)MainFrm.userObject;
			teacherTmp.setName(teacher.getName());
			teacherTmp.setPassword(oldPassword);
			teacherTmp.setId(teacher.getId());
			JOptionPane.showMessageDialog(this, teacherDao.editPassword(teacherTmp, newPassword));
			
			return;
		}

	}
启动测试

文章来源地址https://uudwc.com/A/NbAp5

阅读剩余 92%

原文地址:https://blog.csdn.net/m0_63138660/article/details/131466553

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请联系站长进行投诉反馈,一经查实,立即删除!

h
上一篇 2023年07月04日 17:00
VM虚拟机端口映射接收CS弹反木马
下一篇 2023年07月04日 17:03