Eclipse+Java+Swing实现学生宿舍管理系统(上)

简介: Eclipse+Java+Swing实现学生宿舍管理系统

一、系统介绍


本系统实现了学生登录和管理员登录,学生可以登记入住,实现学生信息查询和修改,实现宿舍信息查询。管理实现学生信息增删改查,实现宿舍信息增删改查。


二、系统展示


1.登录页面


20200902200911885.jpg


2.学生主页面


2020090220104752.jpg


3.学生端-登记页面


20200902201117139.jpg


4.学生端-学生信息修改


20200902201147902.jpg


5.学生端-寝室信息查询


20200902201231724.jpg


6.学生端-学生信息查询


20200902201337765.jpg


7.学生端-退出登录


20200902201433212.jpg


8.管理员-主页面


20200902201544630.jpg


9.管理员-宿舍信息修改


20200902201615742.jpg


10.管理员-宿舍信息删除


20200902201706910.jpg


11.管理员-学生信息修改


20200902201812606.jpg


12.管理员-学生信息删除


20200902201857421.jpg


13.管理员-宿舍信息添加


20200902201929512.jpg


14.管理员-学生信息添加


20200902202038137.jpg


15.管理员-学生信息查询


20200902202151355.jpg


16.管理员-宿舍信息查询


20200902202220634.jpg


17.管理员-退出系统


20200902202252217.jpg


三、系统实现


Room.java

package com.sjsq.model;
/**
 * 
 *
 * 
 * @author shuijianshiqing
 *
 * @date 2020-08-30 18:32
 *
 */
public class Room {
  String id;// 寝室号
  String member;// 成员
  String leader;// 寝室长
  // 构造函数
  public Room(String member) {
    super();
    this.member = member;
  }
  public Room() {
    super();
  }
  public Room(String id, String member, String leader) {
    super();
    this.id = id;
    this.member = member;
    this.leader = leader;
  }
  public Room(String member, String leader) {
    super();
    this.member = member;
    this.leader = leader;
  }
  // get,set方法
  public String getMember() {
    return member;
  }
  public void setMember(String member) {
    this.member = member;
  }
  public String getLeader() {
    return leader;
  }
  public void setLeader(String leader) {
    this.leader = leader;
  }
  public String getId() {
    return id;
  }
  public void setId(String id) {
    this.id = id;
  }
  public String toString() {
    return this.getMember();
  }
}

Student.java

package com.sjsq.model;
/**
 * 
 * 学生实体类
 * 
 * @author shuijianshiqing
 *
 * @date 2020-09-01 07:02
 *
 */
public class Student {
  int id;
  String name;
  String sex;
  String yuanxi;
  String classroom;
  int dormitory = -1;
  int bed;
  public Student(String name) {
    super();
    this.name = name;
  }
  public Student(int id, String name, String sex, String yuanxi, String classroom, int dormitory, int bed) {
    super();
    this.id = id;
    this.name = name;
    this.sex = sex;
    this.yuanxi = yuanxi;
    this.classroom = classroom;
    this.dormitory = dormitory;
    this.bed = bed;
  }
  public Student(String name, String sex, String yuanxi, String classroom, int dormitory, int bed) {
    super();
    this.name = name;
    this.sex = sex;
    this.yuanxi = yuanxi;
    this.classroom = classroom;
    this.dormitory = dormitory;
    this.bed = bed;
  }
  public Student() {
    super();
  }
  public Student(String name, String sex, String yuanxi, String classroom, int dormitory) {
    super();
    this.name = name;
    this.sex = sex;
    this.yuanxi = yuanxi;
    this.classroom = classroom;
    this.dormitory = dormitory;
  }
  public int getId() {
    return id;
  }
  public void setId(int id) {
    this.id = id;
  }
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public String getSex() {
    return sex;
  }
  public void setSex(String sex) {
    this.sex = sex;
  }
  public String getYuanxi() {
    return yuanxi;
  }
  public void setYuanxi(String yuanxi) {
    this.yuanxi = yuanxi;
  }
  public String getClassroom() {
    return classroom;
  }
  public void setClassroom(String classroom) {
    this.classroom = classroom;
  }
  public int getDormitory() {
    return dormitory;
  }
  public void setDormitory(int dormitory) {
    this.dormitory = dormitory;
  }
  public int getBed() {
    return bed;
  }
  public void setBed(int bed) {
    this.bed = bed;
  }
}

User.java

package com.sjsq.model;
/**
 * 
 * User实体类
 * 
 * @author shuijianshiqing
 *
 * @date 2020-09-01 07:02
 *
 */
public class User {
  String id;
  String userName;
  String userPassword;
  public User() {
    super();
  }
  public User(String userName, String userPassword) {
    super();
    this.userName = userName;
    this.userPassword = userPassword;
  }
  public String getId() {
    return id;
  }
  public void setId(String id) {
    this.id = id;
  }
  public String getUserName() {
    return userName;
  }
  public void setUserName(String userName) {
    this.userName = userName;
  }
  public String getUserPassword() {
    return userPassword;
  }
  public void setUserPassword(String userPassword) {
    this.userPassword = userPassword;
  }
  @Override
  public String toString() {
    return this.getUserName();
  }
}

DMUtil.java

package com.sjsq.util;
import java.sql.Connection;
import java.sql.DriverManager;
/**
 * 
 * 数据库工具连接类
 * 
 * @author shuijianshiqing
 *
 * @date 2020-09-01 07:03
 *
 */
public class DMUtil {
  // URL链接
  private String url = "jdbc:mysql://localhost:3306/dormitory_management_swing?serverTimezone=UTC";
  // 账号
  private String userName = "root";
  // 密码
  private String userPassword = "admin";
  // JDBC驱动
  private String jdbcName = "com.mysql.cj.jdbc.Driver";
  // 链接
  public Connection getCon() throws Exception {
    Class.forName(jdbcName);
    Connection con = DriverManager.getConnection(url, userName, userPassword);
    return con;
  }
  // 关闭链接
  public void closeCon(Connection con) throws Exception {
    if (con != null) {
      con.close();
    }
  }
  // 测试
  public static void main(String[] args) {
    DMUtil dmutil = new DMUtil();
    Connection con = null;
    try {
      con = dmutil.getCon();
      System.out.println("数据库连接成功!");
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      try {
        dmutil.closeCon(con);
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
  }
}

StringUtil.java

package com.sjsq.util;
/**
 * 字符串工具类
 * 
 * @author shuijianshiqing
 * 
 * @date 2020-09-01 07:03
 *
 */
public class StringUtil {
  // 判断字符串是否是""
  public static boolean isEmpty(String type) {
    if (type.equals("")) {
      return true;
    } else {
      return false;
    }
  }
  // 判断字符串不为空
  public static boolean isNotEmpty(String type) {
    if (type != null && !type.equals("")) {
      return true;
    } else {
      return false;
    }
  }
}

RoomDao.java

package com.sjsq.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import com.sjsq.model.Room;
import com.sjsq.util.StringUtil;
/**
 * 
 * 操作宿舍信息
 * 
 * @author shuijianshiqing
 *
 * @date 2020-09-01 07:07
 *
 */
public class RoomDao {
  // 添加宿舍信息
  public int roomAdd(Connection con, Room room) throws Exception {
    String sql = "insert into room_message values(null,?,?)";
    PreparedStatement pstmt = con.prepareStatement(sql);
    pstmt.setString(1, room.getMember());
    pstmt.setString(2, room.getLeader());
    return pstmt.executeUpdate();
  }
  // 查询宿舍信息
  public ResultSet roomList(Connection con, Room room) throws Exception {
    StringBuffer sb = new StringBuffer("select * from room_message");
    if (StringUtil.isNotEmpty(room.getMember())) {
      String member = room.getMember();
      sb.append(" and member like '%" + room.getMember() + "%'");
    }
    PreparedStatement pstmt = con.prepareStatement(sb.toString().replaceFirst("and", "where"));
    return pstmt.executeQuery();
  }
  // 修改宿舍信息
  public int roomModify(Connection con, Room room) throws Exception {
    String sql = "update room_message set member=?,leader=? where id=?";
    PreparedStatement pstmt = con.prepareStatement(sql);
    pstmt.setString(1, room.getMember());
    pstmt.setString(2, room.getLeader());
    pstmt.setString(3, room.getId());
    return pstmt.executeUpdate();
  }
  // 删除宿舍信息
  public int roomDelete(Connection con, Room room) throws Exception {
    String sql = "delete from room_message where member=?";
    PreparedStatement pstmt = con.prepareStatement(sql);
    pstmt.setString(1, room.getMember());
    return pstmt.executeUpdate();
  }
}

UserDao.java

package com.sjsq.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import com.sjsq.model.User;
/**
 * 
 * 返回学生信息
 * 
 * @author shuijianshiqing
 *
 * @date 2020-09-01 07:07
 *
 */
public class UserDao {
  // 登录
  public User Login(Connection con, User user) throws Exception {
    User currentuser = null;
    String sql = "select * from d_user where userName=? and userPassword=?";
    PreparedStatement pstmt = con.prepareStatement(sql);
    pstmt.setString(1, user.getUserName());
    pstmt.setString(2, user.getUserPassword());
    ResultSet rs = pstmt.executeQuery();
    while (rs.next()) {
      currentuser = new User();
      currentuser.setUserName(rs.getString("userName"));
      currentuser.setUserPassword(rs.getString("userPassword"));
    }
    return currentuser;
  }
}

LoginFrm.java

package com.sjsq.view;
import java.awt.Font;
import java.sql.Connection;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import com.sjsq.dao.UserDao;
import com.sjsq.model.User;
import com.sjsq.util.DMUtil;
import com.sjsq.util.StringUtil;
/**
 * 
 * 登录界面
 * 
 * @author 刁雨健  dyj
 * @author shuijianshiqing
 *
 * @date 2020-09-01 07:41
 *
 */
public class LoginFrm extends javax.swing.JFrame {
  private JLabel jLabel1;
  private JLabel jLabel2;
  private JLabel jLabel3;
  private JLabel jLabel4;
  private JButton jb_login;
  private JButton jb_reset;
  private JComboBox jcb_choose;
  private JTextField userNameTxt;
  private JPasswordField userPasswordTxt;
  DMUtil dmutil = new DMUtil();
  UserDao userdao = new UserDao();
  /**
   * 创建窗体
   */
  public LoginFrm() {
    Font font = new Font("Dialog", Font.PLAIN, 12);
    java.util.Enumeration keys = UIManager.getDefaults().keys();
    while (keys.hasMoreElements()) {
      Object key = keys.nextElement();
      Object value = UIManager.get(key);
      if (value instanceof javax.swing.plaf.FontUIResource) {
        UIManager.put(key, font);
      }
    }
    // 初始化
    initComponents();
    // 居中
    this.setLocationRelativeTo(null);
    this.filltable();
  }
  // 填充表格
  private void filltable() {
    User user1 = new User();
    User user2 = new User();
    user1.setUserName("学生");
    user2.setUserName("管理员");
    user1.setId(1 + "");
    user2.setId(2 + "");
    jcb_choose.addItem(user1);
    jcb_choose.addItem(user2);
  }
  // 初始化
  private void initComponents() {
    jLabel1 = new JLabel("General", JLabel.CENTER);
    jLabel2 = new JLabel();
    jLabel3 = new JLabel();
    userNameTxt = new JTextField();
    userPasswordTxt = new JPasswordField();
    jb_login = new JButton();
    jb_reset = new JButton();
    jcb_choose = new JComboBox();
    jLabel4 = new JLabel();
    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setTitle("学生宿舍管理系统登录界面");
    setResizable(false);
    jLabel1.setFont(new Font("宋体", 0, 24));
    jLabel1.setText("学生宿舍管理系统");
    // 用户名
    jLabel2.setIcon(new ImageIcon(LoginFrm.class.getResource("/images/user.png")));
    jLabel2.setText("账号:");
    // 密码
    jLabel3.setIcon(new ImageIcon(LoginFrm.class.getResource("/images/password.png")));
    jLabel3.setText("密码:");
    userNameTxt.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        userNameTxtActionPerformed(evt);
      }
    });
    // 登录
    jb_login.setIcon(new ImageIcon(LoginFrm.class.getResource("/images/login.png")));
    jb_login.setText("登录");
    jb_login.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jb_loginActionPerformed(evt);
      }
    });
    // 重置
    jb_reset.setIcon(new ImageIcon(LoginFrm.class.getResource("/images/reset.png"))); // NOI18N
    jb_reset.setText("重置");
    jb_reset.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jb_resetActionPerformed(evt);
      }
    });
    jLabel4.setIcon(new ImageIcon(LoginFrm.class.getResource("/images/type.png"))); // NOI18N
    jLabel4.setText("用户类型:");
    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup().addGap(103, 103, 103).addComponent(jLabel1)
                .addContainerGap(58, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout
                .createSequentialGroup().addGroup(layout.createParallelGroup(
                    javax.swing.GroupLayout.Alignment.TRAILING).addGroup(
                        layout.createSequentialGroup().addContainerGap().addComponent(
                            jLabel4).addGap(42, 42, 42).addComponent(jcb_choose,
                                javax.swing.GroupLayout.PREFERRED_SIZE, 111,
                                javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(
                            javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup().addGap(103, 103, 103)
                                .addGroup(layout.createParallelGroup(
                                    javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel3).addComponent(jLabel2)))
                            .addGroup(layout
                                .createSequentialGroup().addGap(104, 104, 104)
                                .addComponent(jb_login)))
                        .addGroup(layout
                            .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup().addGap(42, 42, 42)
                                .addGroup(layout.createParallelGroup(
                                    javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(userNameTxt,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        111, Short.MAX_VALUE)
                                    .addComponent(userPasswordTxt,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        111, Short.MAX_VALUE)))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
                                layout.createSequentialGroup().addPreferredGap(
                                    javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(jb_reset).addGap(25, 25, 25)))))
                .addGap(100, 100, 100)));
    layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout
        .createSequentialGroup().addGap(60, 60, 60).addComponent(jLabel1)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jLabel2)
            .addComponent(userNameTxt, javax.swing.GroupLayout.PREFERRED_SIZE,
                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        .addGap(27, 27, 27)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jLabel3)
            .addComponent(userPasswordTxt, javax.swing.GroupLayout.PREFERRED_SIZE,
                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        .addGap(27, 27, 27)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jLabel4)
            .addComponent(jcb_choose, javax.swing.GroupLayout.PREFERRED_SIZE,
                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        .addGap(34, 34, 34).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
            .addComponent(jb_login).addComponent(jb_reset))
        .addContainerGap(34, Short.MAX_VALUE)));
    pack();
  }
  // 登录事件监控
  private void jb_loginActionPerformed(java.awt.event.ActionEvent evt) {
    String userName = userNameTxt.getText();
    String userPassword = new String(userPasswordTxt.getPassword());
    if (StringUtil.isEmpty(userName)) {
      JOptionPane.showMessageDialog(null, "用户名不能为空!");
      return;
    } else if (StringUtil.isEmpty(userPassword)) {
      JOptionPane.showMessageDialog(null, "密码不能为空!");
      return;
    }
    User user = new User(userName, userPassword);
    Connection con = null;
    try {
      con = dmutil.getCon();
      User currentuser = userdao.Login(con, user);
      if (currentuser != null) {
        User user1 = (User) jcb_choose.getSelectedItem();
        String userright = user1.getUserName();
        if (userright.equals("管理员")) {
          this.dispose();
          MainFrm main = new MainFrm();
          main.setVisible(true);
        } else if (userright.equals("学生")) {
          this.dispose();
          MainFrm2 main2 = new MainFrm2();
          main2.setVisible(true);
        } else {
          JOptionPane.showMessageDialog(null, "用户名密码错误!");
        }
      }
    } catch (Exception e) {
      JOptionPane.showInternalMessageDialog(null, "登陆失败");
      e.printStackTrace();
    }
  }
  // 重置事件监控
  private void jb_resetActionPerformed(java.awt.event.ActionEvent evt) {
    this.userNameTxt.setText("");
    this.userPasswordTxt.setText("");
  }
  private void userNameTxtActionPerformed(java.awt.event.ActionEvent evt) {
  }
  // 主函数
  public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
      public void run() {
        new LoginFrm().setVisible(true);
      }
    });
  }
}
相关文章
|
4月前
|
安全 Java API
|
5月前
|
JavaScript Java 测试技术
基于Java的人事管理系统设计和实现(源码+LW+部署讲解)
基于Java的人事管理系统设计和实现(源码+LW+部署讲解)
110 7
|
5月前
|
JavaScript Java 测试技术
基于Java的儿童福利院管理系统设计和实现(源码+LW+部署讲解)
基于Java的儿童福利院管理系统设计和实现(源码+LW+部署讲解)
106 7
|
4月前
|
SQL Java 数据库连接
【Java】Java Swing 图书管借阅管理系统(源码+论文)【独一无二】
【Java】Java Swing 图书管借阅管理系统(源码+论文)【独一无二】
236 0
|
4月前
|
存储 数据可视化 Java
【Java】Java swing 民宿管理系统 GUI(源码+可视化界面)【独一无二】
【Java】Java swing 民宿管理系统 GUI(源码+可视化界面)【独一无二】
|
5月前
|
Java 关系型数据库 MySQL
班级通讯录管理系统(Java+MySQL)
构建了一个Java Swing应用,搭配MySQL,实现班级通讯录管理。系统具备管理员登录、班级与学生信息的增删改查功能,每个班级窗口独立且自适应布局。利用GBK编码处理中文,JDBC连接数据库,优化窗口复用和代码结构,数据变更实时同步。示例截图展示详细界面。
班级通讯录管理系统(Java+MySQL)
|
5月前
|
JavaScript 前端开发 Java
Java数字化产科管理系统源码,多家医院应用案例,可直接上项目
Java开发的数字化产科管理系统,已在多家医院实施,支持直接部署。系统涵盖孕产全程,包括门诊、住院、统计和移动服务,整合高危管理、智能提醒、档案追踪等功能,与HIS等系统对接。采用前后端分离架构,Java语言,Vue前端,若依框架,MySQL数据库。优势在于提升就诊效率,降低漏检率,自动报表生成,减少重复工作,支持数据研究,并实现医院与卫计委平台的数据互通,打造全生育周期健康服务。
71 4
|
5月前
|
监控 Java 决策智能
使用Java实现智能物流管理系统
使用Java实现智能物流管理系统
|
5月前
|
存储 JSON Java
如何在Java中设计灵活的配置管理系统
如何在Java中设计灵活的配置管理系统
|
6天前
|
安全 Java API
java如何请求接口然后终止某个线程
通过本文的介绍,您应该能够理解如何在Java中请求接口并根据返回结果终止某个线程。合理使用标志位或 `interrupt`方法可以确保线程的安全终止,而处理好网络请求中的各种异常情况,可以提高程序的稳定性和可靠性。
36 6

推荐镜像

更多
下一篇
DataWorks