用GUI 功能开发学籍管理系统,功能包括但不限于:数据库结构设计、用户登录功能、管理员后台管理功能(至少将管理的班级数据进行展示和查询)的Java代码
时间: 2024-06-09 20:08:37 浏览: 78
以下是一个简单的学籍管理系统的Java代码,包括数据库结构设计、用户登录功能以及管理员后台管理功能。由于GUI界面代码过于冗长,这里只提供基本的逻辑代码。需要注意的是,这个代码只是一个简单的示例,还有很多需要完善的地方。
数据库结构设计:
学生表(student):
id(主键) name(姓名) gender(性别) age(年龄) class_id(班级id)
班级表(class):
id(主键) name(班级名称) grade(年级) teacher(班主任)
用户表(user):
id(主键) username(用户名) password(密码) role(角色,分为“user”和“admin”)
Java代码:
1. 用户登录功能
首先需要创建一个登录界面(LoginFrame),包括输入用户名和密码的文本框以及登录按钮。在点击登录按钮后,系统会去查询数据库中是否存在该用户,如果存在则跳转到对应的主界面(UserMainPanel或AdminMainPanel),否则提示用户名或密码错误。
```
public class LoginFrame extends JFrame {
private JTextField usernameField;
private JPasswordField passwordField;
public LoginFrame() {
// 界面布局代码省略
JButton loginButton = new JButton("登录");
loginButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String username = usernameField.getText();
String password = new String(passwordField.getPassword());
User user = UserService.login(username, password);
if (user != null) {
if (user.getRole().equals("user")) {
new UserMainPanel(user).setVisible(true);
} else if (user.getRole().equals("admin")) {
new AdminMainPanel().setVisible(true);
}
setVisible(false);
} else {
JOptionPane.showMessageDialog(LoginFrame.this, "用户名或密码错误!");
}
}
});
// 界面布局代码省略
}
}
public class UserService {
public static User login(String username, String password) {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
conn = DBUtil.getConnection();
String sql = "select * from user where username = ? and password = ?";
stmt = conn.prepareStatement(sql);
stmt.setString(1, username);
stmt.setString(2, password);
rs = stmt.executeQuery();
if (rs.next()) {
User user = new User();
user.setId(rs.getInt("id"));
user.setUsername(rs.getString("username"));
user.setPassword(rs.getString("password"));
user.setRole(rs.getString("role"));
return user;
} else {
return null;
}
} catch (SQLException e) {
e.printStackTrace();
return null;
} finally {
DBUtil.close(conn, stmt, rs);
}
}
}
```
2. 管理员后台管理功能
管理员后台管理界面(AdminMainPanel)包括班级数据的展示和查询功能。在查询功能中,管理员可以输入班级名称、年级或者班主任的姓名进行查询,系统会返回符合条件的班级列表。
```
public class AdminMainPanel extends JFrame {
private JTextField nameField;
private JTextField gradeField;
private JTextField teacherField;
private JTable table;
public AdminMainPanel() {
// 界面布局代码省略
JButton searchButton = new JButton("查询");
searchButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String name = nameField.getText();
String grade = gradeField.getText();
String teacher = teacherField.getText();
List<Class> classes = ClassService.search(name, grade, teacher);
showClasses(classes);
}
});
// 界面布局代码省略
}
private void showClasses(List<Class> classes) {
DefaultTableModel model = new DefaultTableModel();
model.addColumn("班级名称");
model.addColumn("年级");
model.addColumn("班主任");
for (Class clazz : classes) {
model.addRow(new Object[]{clazz.getName(), clazz.getGrade(), clazz.getTeacher()});
}
table.setModel(model);
}
}
public class ClassService {
public static List<Class> search(String name, String grade, String teacher) {
List<Class> classes = new ArrayList<>();
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
conn = DBUtil.getConnection();
StringBuilder sql = new StringBuilder("select * from class where 1 = 1");
if (name != null && !name.isEmpty()) {
sql.append(" and name like ?");
}
if (grade != null && !grade.isEmpty()) {
sql.append(" and grade = ?");
}
if (teacher != null && !teacher.isEmpty()) {
sql.append(" and teacher like ?");
}
stmt = conn.prepareStatement(sql.toString());
int index = 1;
if (name != null && !name.isEmpty()) {
stmt.setString(index++, "%" + name + "%");
}
if (grade != null && !grade.isEmpty()) {
stmt.setString(index++, grade);
}
if (teacher != null && !teacher.isEmpty()) {
stmt.setString(index++, "%" + teacher + "%");
}
rs = stmt.executeQuery();
while (rs.next()) {
Class clazz = new Class();
clazz.setId(rs.getInt("id"));
clazz.setName(rs.getString("name"));
clazz.setGrade(rs.getString("grade"));
clazz.setTeacher(rs.getString("teacher"));
classes.add(clazz);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(conn, stmt, rs);
}
return classes;
}
}
```
阅读全文