目录
学生模块的功能实现
1. 创建Student实体类型
编写StudentDao 提供添加学生的方法
创建 学生添加页面
创建 AddStudentFrm 页面 注意创建成JInternalFrame类型
注意:把main方法注释掉
回到 MainFrm.java 页面 给添加学生按钮绑定事件
当打开 AddStudentFrm页面时,填充所属班级信息
在 AddStudentFrm 提供填充班级方法
在构造方法里面调用 setStudentClassInfo() 方法
测试
添加学生功能的实现
在提供一个重置的方法
测试一下
创建学生列表页面 ManageStudentFrm 注意创建成:JInternalFrame 类型
剩余的参照之前去做,最后如下图
在StudentDao 中,提供查询所有学生的方法
ManageStudentFrm 页面一打开,填充表格数据
ManageStudentFrm 页面一打开给所属班级下拉框填入数据
回到 MainFrm.java 页面,给 学生列表按钮绑定事件
测试
给查询按钮绑定事件
给表格每一行添加点击事件
测试
给删除按钮绑定事件
在 StudentDao中提供删除学生的方法
编写代码进行删除操作
给修改按钮绑定事件
在 StudentDao 中 提供修改的方法
回到 LoginFrm 登录页面,我们要加上 学生登录的判断
先在 StudentDao 中提供登录的方法
在 LoginFrm 中进行判断 如果是学生
回到 EditPwdFrm 页面,判断如果是学生修改密码
先在StudentDao 中提供修改密码的方法啊
在 EditPwdFrm 页面,判断如果是学生修改密码
在 EditPwdFrm 页面的构造方法里面,根据不同身份进行提示
测试:用学生登录,以及修改密码
学生模块的功能实现
1. 创建Student实体类型
public class Student {
private int id;
private String name;
private int classId;
private String password;
private String sex;
//get set 方法自己补上
}
编写StudentDao 提供添加学生的方法
public class StudentDao {
public boolean addStudent(Student student) {
try {
Connection con = JDBCUtils.getConnection();
String sql = "insert into s_student values(null,?,?,?,?)";
java.sql.PreparedStatement preparedStatement = con.prepareStatement(sql);
preparedStatement.setString(1, student.getName());
preparedStatement.setInt(2, student.getClassId());
preparedStatement.setString(3, student.getPassword());
preparedStatement.setString(4, student.getSex());
if (preparedStatement.executeUpdate() > 0)
return true;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
}
创建 学生添加页面
创建 AddStudentFrm 页面 注意创建成JInternalFrame类型
注意:把main方法注释掉






回到 MainFrm.java 页面 给添加学生按钮绑定事件

//添加学生按钮事件
menuItem_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
openAddStudentFrm(e);
}
});
//打开添加学生页面
protected void openAddStudentFrm(ActionEvent e) {
// TODO Auto-generated method stub
AddStudentFrm addStudentFrm = new AddStudentFrm();
addStudentFrm.setVisible(true);
desktopPane.add(addStudentFrm);
}
当打开 AddStudentFrm页面时,填充所属班级信息
在 AddStudentFrm 提供填充班级方法
//把班级填充到下拉框中
private void setStudentClassInfo() {
ClassDao classDao = new ClassDao();
List<StudentClass> classList = classDao.getClassList(new StudentClass());
for (StudentClass sc : classList) {
studentClassComboBox.addItem(sc);
}
}
在构造方法里面调用 setStudentClassInfo() 方法

测试

添加学生功能的实现
给 确认添加按钮,绑定一个点击事件

//添加学生的事件绑定
addStudentButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//抽取一个方法
studentAddAct(e);
}
});
//添加学生
protected void studentAddAct(ActionEvent ae) {
// TODO Auto-generated method stub
String studentName = studentNameTextField.getText().toString();
String studentPassword = studentPasswordField.getText().toString();
if (StringUtil.isEmpty(studentName)) {
JOptionPane.showMessageDialog(this, "请填写学生姓名!");
return;
}
if (StringUtil.isEmpty(studentPassword)) {
JOptionPane.showMessageDialog(this, "请填写密码!");
return;
}
StudentClass sc = (StudentClass) studentClassComboBox.getSelectedItem();
String sex = studentSexManRadioButton.isSelected() ? studentSexManRadioButton.getText()
: (studentSexFemalRadioButton.isSelected() ? studentSexFemalRadioButton.getText()
: studentSexUnkonwRadioButton.getText());
Student student = new Student();
student.setName(studentName);
student.setClassId(sc.getId());
student.setPassword(studentPassword);
student.setSex(sex);
StudentDao studentDao = new StudentDao();
if (studentDao.addStudent(student)) {
JOptionPane.showMessageDialog(this, "添加成功!");
} else {
JOptionPane.showMessageDialog(this, "添加失败!");
}
//添加完重置
resetValue(ae);
}
在提供一个重置的方法
// 重置的方法,如果有需要可以调用下
protected void resetValue(ActionEvent ae) {
// TODO Auto-generated method stub
studentNameTextField.setText("");
studentPasswordField.setText("");
studentClassComboBox.setSelectedIndex(0);
buttonGroup.clearSelection();
// studentSexManRadioButton.setSelected(true); 把男的按钮设置为选中
}
测试一下
创建学生列表页面 ManageStudentFrm 注意创建成:JInternalFrame 类型





剩余的参照之前去做,最后如下图

给组件起的名字如下
private JTextField serachStudentNameTextField; private JTable studentListTable; private JTextField editStudentNameTextField; private JPasswordField editStudentPasswordPasswordField; private JComboBox searchStudentComboBox; private List<StudentClass> studentClassList ; private JComboBox editStudentClassComboBox; private ButtonGroup editSexButtonGroup; private JRadioButton editStudentSexManRadioButton; private JRadioButton editStudentSexFemalRadioButton; private JRadioButton editStudentSexUnkonwRadioButton; private JButton deleteStudentButton;
在StudentDao 中,提供查询所有学生的方法
public List<Student> getStudentList(Student student) {
List<Student> retList = new ArrayList<Student>();
StringBuffer sqlString = new StringBuffer("select * from s_student");
if (!StringUtil.isEmpty(student.getName())) {
sqlString.append(" and name like '%" + student.getName() + "%'");
}
if (student.getClassId() != 0) {
sqlString.append(" and classId =" + student.getClassId());
}
try {
Connection con = JDBCUtils.getConnection();
PreparedStatement preparedStatement = con
.prepareStatement(sqlString.toString().replaceFirst("and", "where"));
ResultSet executeQuery = preparedStatement.executeQuery();
while (executeQuery.next()) {
Student s = new Student();
s.setId(executeQuery.getInt("id"));
s.setName(executeQuery.getString("name"));
s.setClassId(executeQuery.getInt("classId"));
s.setSex(executeQuery.getString("sex"));
s.setPassword(executeQuery.getString("password"));
retList.add(s);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return retList;
}
ManageStudentFrm 页面一打开,填充表格数据
在 ManageStudentFrm 提供方法 setTable()方法
//填充表格数据
private void setTable(Student student){
DefaultTableModel dft = (DefaultTableModel) studentListTable.getModel();
dft.setRowCount(0);//清空表格数据
StudentDao studentDao = new StudentDao();
List<Student> studentList = studentDao.getStudentList(student);
for (Student s : studentList) {
Vector v = new Vector();
v.add(s.getId());
v.add(s.getName());
//getClassNameById()把所属班级的数字,换成班级名称
v.add(getClassNameById(s.getClassId()));
v.add(s.getSex());
v.add(s.getPassword());
dft.addRow(v);
}
}
//再提供这个方法
//getClassNameById()把所属班级的数字,换成班级名称
private String getClassNameById(int id) {
for (StudentClass sc : studentClassList) {
if (sc.getId() == id)
return sc.getName();
}
return "";
}
在构造方法中调用 setTable()方法
setTable(new Student());
ManageStudentFrm 页面一打开给所属班级下拉框填入数据
在 ManageStudentFrm 提供方法 setStudentClassInfo()方法
// 给下拉框回填数据
private void setStudentClassInfo() {
ClassDao classDao = new ClassDao();
studentClassList = classDao.getClassList(new StudentClass());
for (StudentClass sc : studentClassList) {
//给上面的下拉框回填数据
searchStudentComboBox.addItem(sc);
//给下面的下拉框回填数据
editStudentClassComboBox.addItem(sc);
}
}
在构造方法中调用 setStudentClassInfo()方法
setStudentClassInfo();
回到 MainFrm.java 页面,给 学生列表按钮绑定事件

//学生列表按钮事件绑定
menuItem_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//抽取个方法
openMagageStudentFrm(e);
}
});
//打开学生管理页面
protected void openMagageStudentFrm(ActionEvent e) {
// TODO Auto-generated method stub
ManageStudentFrm manageStudentFrm = new ManageStudentFrm();
manageStudentFrm.setVisible(true);
desktopPane.add(manageStudentFrm);
}
测试

给查询按钮绑定事件

//条件查询按钮,绑定事件
searchButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
searchStudent(ae);
}
});
//条件查询
protected void searchStudent(ActionEvent ae) {
// TODO Auto-generated method stub
Student student = new Student();
student.setName(serachStudentNameTextField.getText().toString());
StudentClass sc = (StudentClass) searchStudentComboBox.getSelectedItem();
student.setClassId(sc.getId());
//再次填充数据
setTable(student);
}
给表格每一行添加点击事件

//给表格的每一行添加点击事件
studentListTable.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
//抽取个方法,把点击的那一行的数据,展示到编辑框
selectedTableRow(arg0);
}
});
展示你点击的那一行数据
// 把表格点击的那一行数据,展示出来
protected void selectedTableRow(MouseEvent me) {
// 获取表格模型
DefaultTableModel dft = (DefaultTableModel) studentListTable.getModel();
// 获取第二列的数据进行填充
editStudentNameTextField.setText(dft.getValueAt(studentListTable.getSelectedRow(), 1).toString());
// 填充密码
editStudentPasswordPasswordField.setText(dft.getValueAt(studentListTable.getSelectedRow(), 4).toString());
// 获取班级名称
String className = dft.getValueAt(studentListTable.getSelectedRow(), 2).toString();
//循环下拉框选项,进行判断,填充
for (int i = 0; i < editStudentClassComboBox.getItemCount(); i++) {
StudentClass sc = (StudentClass) editStudentClassComboBox.getItemAt(i);
if (className.equals(sc.getName())) {
editStudentClassComboBox.setSelectedIndex(i);
}
}
// 获取性别
String sex = dft.getValueAt(studentListTable.getSelectedRow(), 3).toString();
//清空按钮组所选
editSexButtonGroup.clearSelection();
//判断勾选哪个性别
if (sex.equals(editStudentSexManRadioButton.getText())) {
editStudentSexManRadioButton.setSelected(true);
}
if (sex.equals(editStudentSexFemalRadioButton.getText())) {
editStudentSexFemalRadioButton.setSelected(true);
}
if (sex.equals(editStudentSexUnkonwRadioButton.getText())) {
editStudentSexUnkonwRadioButton.setSelected(true);
}
}
测试

给删除按钮绑定事件

在 StudentDao中提供删除学生的方法
//删除学生
public boolean delete(int id) {
String sql = "delete from s_student where id=?";
try {
Connection con = JDBCUtils.getConnection();
PreparedStatement preparedStatement = con.prepareStatement(sql);
preparedStatement.setInt(1, id);
if (preparedStatement.executeUpdate() > 0) {
return true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
编写代码进行删除操作
//删除按钮绑定事件
deleteStudentButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
//抽取方法进行删除
deleteStudent(ae);
}
});
//删除学生操作
protected void deleteStudent(ActionEvent ae) {
//获取选中行的索引
int row = studentListTable.getSelectedRow();
if (row == -1) {
JOptionPane.showMessageDialog(this, "请选中要删除的数据!");
return;
}
if (JOptionPane.showConfirmDialog(this, "您确定删除么?") != JOptionPane.OK_OPTION) {
return;
}
StudentDao studentDao = new StudentDao();
//获取第一列的id
int id = Integer.parseInt(studentListTable.getValueAt(row, 0).toString());
if (studentDao.delete(id)) {
JOptionPane.showMessageDialog(this, "删除成功!");
} else {
JOptionPane.showMessageDialog(this, "删除失败!");
}
//重新填充表格数据
setTable(new Student());
}
给修改按钮绑定事件

在 StudentDao 中 提供修改的方法
//修改学生
public boolean update(Student student) {
String sql = "update s_student set name=?, classId=?,sex=?,password=? where id=?";
try {
Connection con = JDBCUtils.getConnection();
PreparedStatement preparedStatement = con.prepareStatement(sql);
preparedStatement.setString(1, student.getName());
preparedStatement.setInt(2, student.getClassId());
preparedStatement.setString(3, student.getSex());
preparedStatement.setString(4, student.getPassword());
preparedStatement.setInt(5, student.getId());
if (preparedStatement.executeUpdate() > 0) {
return true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
//修改按钮绑定事件
submitEditButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
submitEditAct(ae);
}
});
//修改操作
protected void submitEditAct(ActionEvent ae) {
// TODO Auto-generated method stub
int row = studentListTable.getSelectedRow();
if (row == -1) {
JOptionPane.showMessageDialog(this, "请选中要修改的数据!");
return;
}
String studentName = editStudentNameTextField.getText().toString();
String studentPassword = editStudentPasswordPasswordField.getText().toString();
if (StringUtil.isEmpty(studentName)) {
JOptionPane.showMessageDialog(this, "请填写学生姓名!");
return;
}
if (StringUtil.isEmpty(studentPassword)) {
JOptionPane.showMessageDialog(this, "请填写密码!");
return;
}
Student student = new Student();
student.setName(studentName);
student.setPassword(studentPassword);
StudentClass sc = (StudentClass) editStudentClassComboBox.getSelectedItem();
student.setClassId(sc.getId());
student.setId(Integer.parseInt(studentListTable.getValueAt(row, 0).toString()));
if (editStudentSexManRadioButton.isSelected())
student.setSex(editStudentSexManRadioButton.getText().toString());
if (editStudentSexFemalRadioButton.isSelected())
student.setSex(editStudentSexFemalRadioButton.getText().toString());
if (editStudentSexUnkonwRadioButton.isSelected())
student.setSex(editStudentSexUnkonwRadioButton.getText().toString());
StudentDao studentDao = new StudentDao();
if (studentDao.update(student)) {
JOptionPane.showMessageDialog(this, "更新成功!");
} else {
JOptionPane.showMessageDialog(this, "更新失败!");
}
//修改完成重新填充表格数据
setTable(new Student());
}
回到 LoginFrm 登录页面,我们要加上 学生登录的判断
先在 StudentDao 中提供登录的方法
//学生登录
public Student login(Student student) {
String sql = "select * from s_student where name=? and password=?";
Student studentRst = null;
try {
Connection con = JDBCUtils.getConnection();
PreparedStatement prst = con.prepareStatement(sql);//
prst.setString(1, student.getName());
prst.setString(2, student.getPassword());
ResultSet executeQuery = prst.executeQuery();
if (executeQuery.next()) {
studentRst = new Student();
studentRst.setId(executeQuery.getInt("id"));
studentRst.setClassId(executeQuery.getInt("classId"));
studentRst.setName(executeQuery.getString("name"));
studentRst.setPassword(executeQuery.getString("password"));
studentRst.setSex(executeQuery.getString("sex"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return studentRst;
}
在 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())) {
} else {
//学生登录
Student student = null;
StudentDao studentDao = new StudentDao();
Student studentTmp = new Student();
studentTmp.setName(username);
studentTmp.setPassword(password);
student = studentDao.login(studentTmp);
if(student == null){
JOptionPane.showMessageDialog(this, "用户名或密码错误!");
return;
}
JOptionPane.showMessageDialog(this, "欢迎【"+selectedItem.getName()+"】:"+student.getName()+"登录本系统!");
this.dispose();
//打开主页面
new MainFrm(selectedItem, student).setVisible(true);
}
}
回到 EditPwdFrm 页面,判断如果是学生修改密码
先在StudentDao 中提供修改密码的方法啊
//修改密码
public String editPassword(Student student, String newPassword) throws SQLException {
Connection con = JDBCUtils.getConnection();
String sql = "select * from s_student where id=? and password=?";
PreparedStatement prst = null;
int id = 0;
try {
prst = con.prepareStatement(sql);
prst.setInt(1, student.getId());
prst.setString(2, student.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_student set password = ? where id = ?";
try {
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 页面,判断如果是学生修改密码
// 确认修改
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())) {
StudentDao studentDao = new StudentDao();
Student studentTmp = new Student();
Student student = (Student) MainFrm.userObject;
studentTmp.setName(student.getName());
studentTmp.setPassword(oldPassword);
studentTmp.setId(student.getId());
try {
JOptionPane.showMessageDialog(this, studentDao.editPassword(studentTmp, newPassword));
} catch (HeadlessException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return;
}
}
在 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{
//教师
}




















