使用Java和swing和MySQL用代码写出一个大概的宿舍管理系统
时间: 2024-03-09 17:49:20 浏览: 63
好的,下面是一个大概的宿舍管理系统的Java代码示例,使用了Java Swing和MySQL:
```
import java.sql.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class DormitoryManagementSystem extends JFrame implements ActionListener {
JPanel panel;
JLabel user_label, password_label, message;
JTextField userName_text;
JPasswordField password_text;
JButton submit, cancel;
DormitoryManagementSystem() {
user_label = new JLabel();
user_label.setText("用户名:");
userName_text = new JTextField();
password_label = new JLabel();
password_label.setText("密码:");
password_text = new JPasswordField();
submit = new JButton("登录");
cancel = new JButton("取消");
panel = new JPanel(new GridLayout(3, 1));
panel.add(user_label);
panel.add(userName_text);
panel.add(password_label);
panel.add(password_text);
message = new JLabel();
panel.add(message);
panel.add(submit);
panel.add(cancel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 设置窗体大小
setSize(500, 500);
panel.setBackground(Color.LIGHT_GRAY);
add(panel, BorderLayout.CENTER);
// 设置标题
setTitle("宿舍管理系统");
setVisible(true);
setLocationRelativeTo(null);
submit.addActionListener(this);
cancel.addActionListener(this);
}
public static void main(String[] args) {
new DormitoryManagementSystem();
}
@Override
public void actionPerformed(ActionEvent e) {
String userName = userName_text.getText();
String password = password_text.getText();
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/dormitory_management_system", "root", "123456");
PreparedStatement st = con.prepareStatement("select * from users where username=? and password=?");
st.setString(1, userName);
st.setString(2, password);
ResultSet rs = st.executeQuery();
if (rs.next()) {
message.setText("登录成功!");
this.dispose();
new Dashboard();
} else {
message.setText("用户名或密码错误!");
}
rs.close();
st.close();
con.close();
} catch (Exception ex) {
System.out.println(ex);
}
}
class Dashboard extends JFrame implements ActionListener {
JMenuBar menuBar;
JMenu menu1, menu2, menu3;
JMenuItem menuItem1, menuItem2, menuItem3, menuItem4, menuItem5, menuItem6, menuItem7, menuItem8,
menuItem9;
Dashboard() {
menuBar = new JMenuBar();
menu1 = new JMenu("系统管理");
menuItem1 = new JMenuItem("用户管理");
menuItem1.addActionListener(this);
menuItem2 = new JMenuItem("角色管理");
menuItem2.addActionListener(this);
menuItem3 = new JMenuItem("权限管理");
menuItem3.addActionListener(this);
menu1.add(menuItem1);
menu1.add(menuItem2);
menu1.add(menuItem3);
menu2 = new JMenu("学生管理");
menuItem4 = new JMenuItem("学生信息管理");
menuItem4.addActionListener(this);
menuItem5 = new JMenuItem("学生入住管理");
menuItem5.addActionListener(this);
menuItem6 = new JMenuItem("学生缺寝管理");
menuItem6.addActionListener(this);
menu2.add(menuItem4);
menu2.add(menuItem5);
menu2.add(menuItem6);
menu3 = new JMenu("宿舍管理");
menuItem7 = new JMenuItem("宿舍楼信息管理");
menuItem7.addActionListener(this);
menuItem8 = new JMenuItem("宿舍信息管理");
menuItem8.addActionListener(this);
menuItem9 = new JMenuItem("统计报表");
menuItem9.addActionListener(this);
menu3.add(menuItem7);
menu3.add(menuItem8);
menu3.add(menuItem9);
menuBar.add(menu1);
menuBar.add(menu2);
menuBar.add(menu3);
setJMenuBar(menuBar);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 500);
setTitle("宿舍管理系统");
setVisible(true);
setLocationRelativeTo(null);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == menuItem1) {
new UserManagement();
} else if (e.getSource() == menuItem2) {
new RoleManagement();
} else if (e.getSource() == menuItem3) {
new PermissionManagement();
} else if (e.getSource() == menuItem4) {
new StudentInformationManagement();
} else if (e.getSource() == menuItem5) {
new StudentAccommodationManagement();
} else if (e.getSource() == menuItem6) {
new StudentAbsenteeismManagement();
} else if (e.getSource() == menuItem7) {
new DormitoryBuildingInformationManagement();
} else if (e.getSource() == menuItem8) {
new DormitoryInformationManagement();
} else if (e.getSource() == menuItem9) {
new ReportManagement();
}
}
}
class UserManagement extends JFrame {
UserManagement() {
// 用户管理界面代码
}
}
class RoleManagement extends JFrame {
RoleManagement() {
// 角色管理界面代码
}
}
class PermissionManagement extends JFrame {
PermissionManagement() {
// 权限管理界面代码
}
}
class StudentInformationManagement extends JFrame {
StudentInformationManagement() {
// 学生信息管理界面代码
}
}
class StudentAccommodationManagement extends JFrame {
StudentAccommodationManagement() {
// 学生入住管理界面代码
}
}
class StudentAbsenteeismManagement extends JFrame {
StudentAbsenteeismManagement() {
// 学生缺寝管理界面代码
}
}
class DormitoryBuildingInformationManagement extends JFrame {
DormitoryBuildingInformationManagement() {
// 宿舍楼信息管理界面代码
}
}
class DormitoryInformationManagement extends JFrame {
DormitoryInformationManagement() {
// 宿舍信息管理界面代码
}
}
class ReportManagement extends JFrame {
ReportManagement() {
// 统计报表界面代码
}
}
}
```
上述代码仅为示例,具体实现细节和代码实现需要根据具体需求进行开发。
阅读全文