Java校园宿舍管理系统(GUI)
需求
校园宿舍管理系统主要分为三类用户,系统管理员、宿舍管理员和学生。主要功能需求如下。
系统管理员功能需求:
(1)宿舍管理员管理:能够添加管理员用户、删除管理员用户信息、查找管理员信息、修改管理员信息。
(2)学生信息管理:能够添加学生信息、删除学生信息、查找学生信息、修改学生信息。
(3)宿舍楼信息管理:能够添加宿舍楼信息、删除宿舍楼信息、查找宿舍楼信息、修改宿舍楼信息。
(4)宿舍信息管理:能够添加宿舍信息、删除宿舍信息、查找宿舍信息、修改宿舍信息。
(5)学生住宿管理:能够添给学生分配宿舍、能够给学生调换宿舍、能够删除学生住宿信息。
(6)学生缺寝管理:学生缺寝时,能够记录学生缺寝记录,如果缺寝信息有误,学生申诉后能够修改缺寝信息、删除缺寝信息、查询学生缺寝信息。
(8)系统登录退出:能够正常登陆、退出系统。
宿舍管理员功能需求:
(1)学生缺寝管理:学生缺寝时,能够记录学生缺寝记录,如果缺寝信息有误,学生申诉后能够修改缺寝信息、删除缺寝信息、查询学生缺寝信息。
(2)系统登录退出:能够正常登陆、退出系统。
学生功能需求:
(1)能查看个人缺寝信息。
(2)能进行系统登录、退出。
图片
代码比较多,如需要全部代码和数据库内容,请+vx:keep_going_67
package DMS;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableRowSorter;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import java.util.ArrayList;
/**登录界面
* @author LQ
* @create 2020-6-9 15:58
*/
public class login extends JFrame implements ActionListener {
JLabel welcome=new JLabel("欢迎使用校园宿舍管理系统");
JLabel user, password;
JTextField username;
JPasswordField passwordField;
JButton loginButton;
JButton button;
CardLayout cardLayout = new CardLayout();
JPanel card;
JPanel cardPanel,cardPanel2,cardPanel3,cardPanel4;
JTabbedPane jTabbedPane,jTabbedPane2;
int type=1;
Users users;
Font font = new Font("宋体", Font.BOLD|Font.ITALIC, 20);
public login() {
init();
}
private void init() {
welcome.setFont(font);
setTitle("宿舍管理系统");
setLayout(new BorderLayout());
user = new JLabel("用户名");
password = new JLabel("密码");
button = new JButton("重置");
card = new JPanel(cardLayout);
JPanel panel1 = new JPanel(new BorderLayout());
username = new JTextField();
passwordField = new JPasswordField();
loginButton = new JButton("Login!");
loginButton.addActionListener(this);
JPanel titlepanel = new JPanel(new FlowLayout());//标题面板
JPanel loginpanel = new JPanel();//登录面板
loginpanel.setLayout(null);
welcome.setBounds(300,100,400,25);
user.setBounds(340, 170, 50, 20);
password.setBounds(340, 210, 50, 20);
username.setBounds(390, 170, 120, 20);
passwordField.setBounds(390, 210, 120, 20);
loginButton.setBounds(340, 250, 80, 25);
button.setBounds(430,250,80,25);
loginpanel.add(welcome);
loginpanel.add(user);
loginpanel.add(password);
loginpanel.add(username);
loginpanel.add(passwordField);
loginpanel.add(loginButton);
loginpanel.add(button);
panel1.add(titlepanel, BorderLayout.NORTH);
panel1.add(loginpanel, BorderLayout.CENTER);
//panel1.add(loginButton, BorderLayout.SOUTH);
card.add(panel1, "login");
//card.add(cardPanel, "info");
add(card);
setBounds(300, 100, 900, 600);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
button.addActionListener(new ActionListener(){ //为重置按钮添加监听事件
//同时清空name、password的数据
public void actionPerformed(ActionEvent arg0) {
// TODO 自动生成方法存根
username.setText("");
passwordField.setText("");
}
});
}
public static void main(String[] args) {
new login();
}
@Override
public void actionPerformed(ActionEvent e) {
boolean flag=false;//用来标志用户是否正确
if (e.getSource() == loginButton) {
ArrayList<Users> list = new CheckUsers().getUsers();//获得所有用户信息
for (int i = 0; i < list.size(); i++) {//遍历所有用户信息,以此来判断输入的信息是否正确
users = list.get(i);
String passwordStr = new String(passwordField.getPassword());
if (username.getText().equals(users.getName()) && passwordStr.equals(users.getPassword())) {
if(users.getType()==1){//如果时学生
type=users.getType();
System.out.println("登录人员类别"+type);
JOptionPane.showMessageDialog(null, "欢迎(学生)"+username.getText()+"登录", "学生宿舍管理系统", JOptionPane.PLAIN_MESSAGE);
//当输入的信息正确时,就开始加载选项卡界面,并把选项卡界面加入到卡片布局器中
StudentInfo studentInfo=new StudentInfo(users,type);//学生信息
//DormitoryInfo dormitoryInfo = new DormitoryInfo(users,type);//宿舍信息
AddLater later = new AddLater(type,users);//晚归信息
guanyu guanyu=new guanyu();
login.guanyu.help help=new guanyu.help();
Exit exit=new Exit();
cardPanel = new JPanel();
cardPanel2 = new JPanel();
cardPanel3 = new JPanel();
cardPanel4 = new JPanel();
jTabbedPane = new JTabbedPane(JTabbedPane.LEFT);
//jTabbedPane.add("宿舍信息", dormitoryInfo);
jTabbedPane.add("学生信息",studentInfo);
//jTabbedPane.add("学生离校与返校", outAndIn);
jTabbedPane.add("缺寝记录", later);
//jTabbedPane.add("宿舍物品", things);
jTabbedPane.add("退出登录", exit);
cardPanel.add(jTabbedPane);
jTabbedPane2 = new JTabbedPane(JTabbedPane.TOP);
jTabbedPane2.add("系统操作页面",cardPanel);
jTabbedPane2.add("关于系统",guanyu);
jTabbedPane2.add("帮助",help);
cardPanel2.add(jTabbedPane2);
card.add(cardPanel2, "info");
cardLayout.show(card, "info");//输入信息正确就显示操作界面,否则重新输入正确信息
}else if (users.getType()==2){//如果时宿管
type=users.getType();
System.out.println("登录人员类别"+type);
JOptionPane.showMessageDialog(null, "欢迎(宿管)"+username.getText()+"登录", "学生宿舍管理系统", JOptionPane.PLAIN_MESSAGE);
//当输入的信息正确时,就开始加载选项卡界面,并把选项卡界面加入到卡片布局器中
StudentInfo studentInfo=new StudentInfo(users,type);//学生信息
CooDormitory dormitoryInfo = new CooDormitory(type,users);//宿舍信息
AddLater later = new AddLater(type,users);//晚归信息
editLater editLater=new editLater(type,users);//修改缺寝信息
DeleteLater deleteLater=new DeleteLater(type,users);//删除
SeekLater seekLater=new SeekLater(type,users);
guanyu guanyu=new guanyu();
login.guanyu.help help=new guanyu.help();
Exit exit=new Exit();//退出系统
cardPanel = new JPanel();
cardPanel2 = new JPanel();
cardPanel3 = new JPanel();
cardPanel4 = new JPanel();
cardPanel = new JPanel();
jTabbedPane = new JTabbedPane(JTabbedPane.LEFT);
jTabbedPane.add("学生信息",studentInfo);
jTabbedPane.add("宿舍信息", dormitoryInfo);
//jTabbedPane.add("学生离校与返校", outAndIn);
jTabbedPane.add("缺寝记录", later);
jTabbedPane.add("查询缺寝记录",seekLater);
jTabbedPane.add("修改缺寝记录", editLater);
jTabbedPane.add("删除缺寝记录",deleteLater);
//jTabbedPane.add("宿舍物品", things);
jTabbedPane.add("退出登录", exit);
cardPanel.add(jTabbedPane);
jTabbedPane2 = new JTabbedPane(JTabbedPane.TOP);
jTabbedPane2.add("系统操作页面",cardPanel);
jTabbedPane2.add("关于系统",guanyu);
jTabbedPane2.add("帮助",help);
cardPanel2.add(jTabbedPane2);
card.add(cardPanel2, "info");
cardLayout.show(card, "info");//输入信息正确就显示操作界面,否则重新输入正确信息
}else if (users.getType()==3){
type=users.getType();
System.out.println("登录人员类别"+type);
JOptionPane.showMessageDialog(null, "欢迎(超管)"+username.getText()+"登录", "学生宿舍管理系统", JOptionPane.PLAIN_MESSAGE);
//当输入的信息正确时,就开始加载选项卡界面,并把选项卡界面加入到卡片布局器中
//StudentInfo studentInfo=new StudentInfo(users,type);//学生信息
AddLater later = new AddLater(type,users);//晚归信息
editLater editLater=new editLater(type,users);//修改缺寝信息
DeleteLater deleteLater=new DeleteLater(type,users);//删除
SeekLater seekLater=new SeekLater(type,users);
CooUsers cooUsers=new CooUsers(type,users);
CooStu cooStu=new CooStu(type,users);
CooDormitory dormitoryInfo = new CooDormitory(type,users);//宿舍信息
CooBuilding cooBuilding=new CooBuilding(type,users);
guanyu guanyu=new guanyu();
login.guanyu.help help=new guanyu.help();
Exit exit=new Exit();
cardPanel = new JPanel();
cardPanel2 = new JPanel();
cardPanel3 = new JPanel();
cardPanel4 = new JPanel();
jTabbedPane = new JTabbedPane(JTabbedPane.LEFT);
jTabbedPane.add("缺寝记录", later);
//jTabbedPane.add("学生信息",studentInfo);
jTabbedPane.add("查询缺寝记录",seekLater);
jTabbedPane.add("修改缺寝记录", editLater);
jTabbedPane.add("删除缺寝记录",deleteLater);
jTabbedPane.add("宿管人员管理",cooUsers);
jTabbedPane.add("学生人员管理",cooStu);
jTabbedPane.add("宿舍信息管理", dormitoryInfo);
jTabbedPane.add("寝楼信息管理",cooBuilding);
jTabbedPane.add("退出登录", exit);
cardPanel.add(jTabbedPane);
jTabbedPane2 = new JTabbedPane(JTabbedPane.TOP);
jTabbedPane2.add("系统操作页面",cardPanel);
jTabbedPane2.add("关于系统",guanyu);
jTabbedPane2.add("帮助",help);
cardPanel2.add(jTabbedPane2);
card.add(cardPanel2, "info");
cardLayout.show(card, "info");//输入信息正确就显示操作界面,否则重新输入正确信息
}
flag = true;
break;//如果信息正确就退出遍历,提高效率
}
}
if(!flag){//信息不正确,重新输入
JOptionPane.showMessageDialog(null, "请输入正确的用户名或密码", "警告",JOptionPane.WARNING_MESSAGE);
username.setText("");
passwordField.setText("");
}
}
}
/**连接到SQL server
* @author LQ
* @create 2020-06-10 22:03
*/
public static class GetConnection {
private Connection con=null;
public Connection GetConnection(){
String URL="jdbc:mysql://localhost:3306/day10?&useSSL=false&serverTimezone=UTC";//数据库位置
String USER="root";
String KEY="root";
try {
Class.forName("com.mysql.cj.jdbc.Driver");//加载驱动,连接数据库,
con= DriverManager.getConnection(URL, USER, KEY);
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
return con;
}
}
/**用户信息
* @author LQ
* @create 2020-06-19 15:38
*/
public static class Users {
private String name;
private String password;
private int type;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
}
/**获得用户列表
* @author LQ
* @create 2020-06-05 15:40
*/
public static class CheckUsers {
GetConnection getConnection=new GetConnection();
Connection connection=getConnection.GetConnection();
public ArrayList<Users> getUsers(){
ArrayList<Users>list = new ArrayList<>();
try {
PreparedStatement state=connection.prepareStatement("select *from Users");
ResultSet res=state.executeQuery();
while(res.next()){
Users user = new Users();
user.setName(res.getString(1));
user.setPassword(res.getString(2));
user.setType(res.getInt(3));///类型
list.add(user);
}
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}
return list;
}
}
/**学生晚归
* @author LQ
* @create 2020-6-11 16:44
*/
public static class AddLater extends JPanel implements ActionListener {
Connection connection = new GetConnection().GetConnection();
int type;
Users user;
JTable table = new JTable();
//JButton button = new JButton("");
String[] col = {"学号","姓名", "宿舍号", "缺寝时间", "缺寝原因"};
DefaultTableModel mm = new DefaultTableModel(col, 0); // 定义一个表的模板
JLabel Atime,Areason,Dno,Sno,Sname,tip;
JTextField AtimeText,AreasonText,DnoText,SnoText,SnameText;
JButton submit;
JPanel student;
public AddLater(int type, Users user){
this.user=user;
this.type=type;
setLayout(new FlowLayout());//整个采用流动式布局 很好的适应了表格带来的影响
table.setModel(mm);
table.setRowSorter(new TableRowSorter<>(mm));
JScrollPane js=new JScrollPane(table);
add(js);
search();
}
private void search(){
PreparedStatement state;
ResultSet resultSet;
if(type==1){
try {
/*String select="select Dno from student where Sname"+"="+"'"+user.getName()+"'";
PreparedStatement state=connection.prepareStatement(select);
ResultSet resultSet=state.executeQuery();
while (resultSet.next()) {
DDno = resultSet.getString("Dno");
}*/
state=connection.prepareStatement("select * from absent where Sname"+"="+"'"+user.getName()+"'"); //这里不好 因为名字可以一样 以后要改
resultSet = state.executeQuery();
while (resultSet.next()){
String Sno=resultSet.getString(1);
String Sname=resultSet.getString(2);
String Dno=resultSet.getString(3);
String Atime=resultSet.getString(4);
String Areason=resultSet.getString(5);
String[] data={Sno,Sname,Dno,Atime,Areason};
mm.addRow(data);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
if(type==2||type==3){
try {
record();
state=connection.prepareStatement("select*from absent");
resultSet = state.executeQuery();
while (resultSet.next()){
String Sno=resultSet.getString(1);
String Sname=resultSet.getString(2);
String Dno=resultSet.getString(3);
String Atime=resultSet.getString(4);
String Areason=resultSet.getString(5);
String[] data={Sno,Sname,Dno,Atime,Areason};
mm.addRow(data);
}
}catch (Exception e){
e.printStackTrace();
}
}
}
private void record(){ //这就是宿管添加晚归记录的功能
Sno=new JLabel("要添加学生的学号:");
SnoText=new JTextField(10);
Sname=new JLabel("姓名:");
SnameText=new JTextField(10);
Dno=new JLabel("宿舍号:");
DnoText=new JTextField(10);
Atime=new JLabel("缺寝时间:");
AtimeText=new JTextField(10);
Areason=new JLabel("缺寝原因:");
AreasonText=new JTextField(10);
submit=new JButton("添加");
submit.addActionListener(this);
student=new JPanel(new GridLayout(8, 1));
//student.add(tip);
student.add(Sno);student.add(SnoText);
student.add(Sname);student.add(SnameText);
student.add(Dno);student.add(DnoText);
student.add(Atime);student.add(AtimeText);
student.add(Areason);student.add(AreasonText);
student.add(submit);
add(student);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==submit){ //这就是宿管添加晚归记录的功能
try {
PreparedStatement statement = connection.prepareStatement("insert into absent values(?,?,?,?,?)");
statement.setString(1, SnoText.getText());
statement.setString(2, SnameText.getText());
statement.setString(3, DnoText.getText());
statement.setString(4, AtimeText.getText());
statement.setString(5, AreasonText.getText());
statement.executeUpdate();
PreparedStatement state=connection.prepareStatement("select*from absent");
ResultSet resultSet = state.executeQuery();
while(mm.getRowCount()>0){//把表格进行刷新,下次显示的时候重头开始显示
//System.out.println(model.getRowCount());
mm.removeRow(mm.getRowCount()-1);
}
while (resultSet.next()){
String Sno=resultSet.getString(1);
String Sname=resultSet.getString(2);
String Dno=resultSet.getString(3);
String Atime=resultSet.getString(4);
String Areason=resultSet.getString(5);
String[] data={Sno,Sname,Dno,Atime,Areason};
mm.addRow(data);
}
} catch (SQLException e1) {
e1.printStackTrace();
}
}
JOptionPane.showMessageDialog(this,"添加成功");
}
}
数据库表的格式
表名在左上角