Eclipse+Java+Swing实现学生选课管理系统(上)

简介: Eclipse+Java+Swing实现学生选课管理系统

一、系统介绍


本系统实现了学生登录和管理员登录,学生实现选课,查看已选课程,修改密码,查看学生信息功能。管理员实现选课信息的增删改查,学生信息查询,学生密码修改功能。


二、系统展示


1.登录页面


20200906230720482.jpg


2.学生端-主页面


20200906230740669.jpg


3.学生端-课程选择


20200906230833229.jpg


4.学生端-查看已选课程


20200906230859836.jpg


5.学生端-修改密码


20200906230935324.jpg


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


20200906231007185.jpg


7.管理员-主页面


20200906231044301.jpg


8.管理员-查看选课情况


20200906231118220.jpg


9.管理员-添加课程


20200906231208711.jpg


10.管理员-修改课程信息


20200906231242334.jpg


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


20200906231326444.jpg


12.管理员-学生密码修改


20200906231402226.jpg


三、部分代码


Admin.java

package com.sjsq.model;
/**
 * Admin实体类
 * 
 * @author jakey
 * 
 * @author shuijianshiqing
 *
 * @date 2020-09-06 19:37
 *
 */
public class Admin {
  private int adminId;
  private String password;
  public Admin() {
    super();
  }
  public Admin(int adminId, String password) {
    super();
    this.adminId = adminId;
    this.password = password;
  }
  public int getAdminId() {
    return adminId;
  }
  public void setAdminId(int adminId) {
    this.adminId = adminId;
  }
  public String getPassword() {
    return password;
  }
  public void setPassword(String password) {
    this.password = password;
  }
}

Course.java

package com.sjsq.model;
public class Course {
  private int courseId = -1;
  private String courseName;
  private String courseTime;
  private String courseTeacher;
  private int capacity;
  private int numSelected;
  public Course() {
    super();
  }
  public Course(String courseName, String courseTime, String courseTeacher) {
    super();
    this.courseName = courseName;
    this.courseTime = courseTime;
    this.courseTeacher = courseTeacher;
  }
  public Course(int courseId, String courseName, String courseTime, String courseTeacher, int capacity) {
    super();
    this.courseId = courseId;
    this.courseName = courseName;
    this.courseTime = courseTime;
    this.courseTeacher = courseTeacher;
    this.capacity = capacity;
  }
  public Course(String courseName, String courseTime, String courseTeacher, int capacity) {
    super();
    this.courseName = courseName;
    this.courseTime = courseTime;
    this.courseTeacher = courseTeacher;
    this.capacity = capacity;
  }
  public int getCourseId() {
    return courseId;
  }
  public void setCourseId(int courseId) {
    this.courseId = courseId;
  }
  public String getCourseName() {
    return courseName;
  }
  public void setCourseName(String courseName) {
    this.courseName = courseName;
  }
  public String getCourseTime() {
    return courseTime;
  }
  public void setCourseTime(String courseTime) {
    this.courseTime = courseTime;
  }
  public String getCourseTeacher() {
    return courseTeacher;
  }
  public void setCourseTeacher(String courseTeacher) {
    this.courseTeacher = courseTeacher;
  }
  public int getCapacity() {
    return capacity;
  }
  public void setCapacity(int capacity) {
    this.capacity = capacity;
  }
  public int getNumSelected() {
    return numSelected;
  }
  public void setNumSelected(int numSelected) {
    this.numSelected = numSelected;
  }
}

Selection.java

package com.sjsq.model;
public class Selection {
  int selectId;
  int courseId = -1;
  int Sno;
  public Selection() {
    super();
  }
  public Selection(int courseId, int sno) {
    super();
    this.courseId = courseId;
    Sno = sno;
  }
  public int getSelectId() {
    return selectId;
  }
  public void setSelectId(int selectId) {
    this.selectId = selectId;
  }
  public int getCourseId() {
    return courseId;
  }
  public void setCourseId(int courseId) {
    this.courseId = courseId;
  }
  public int getSno() {
    return Sno;
  }
  public void setSno(int sno) {
    Sno = sno;
  }
}

Sinfo.java

package com.sjsq.model;
public class Sinfo {
  private int sno = -1;
  private String sname;
  private String ssex;
  private String smajor;
  private String stele;
  public Sinfo() {
    super();
  }
  public Sinfo(int sno) {
    super();
    this.sno = sno;
  }
  public Sinfo(int sno, String sname) {
    super();
    this.sno = sno;
    this.sname = sname;
  }
  public int getSno() {
    return sno;
  }
  public void setSno(int sno) {
    this.sno = sno;
  }
  public String getSname() {
    return sname;
  }
  public void setSname(String sname) {
    this.sname = sname;
  }
  public String getSsex() {
    return ssex;
  }
  public void setSsex(String ssex) {
    this.ssex = ssex;
  }
  public String getSmajor() {
    return smajor;
  }
  public void setSmajor(String smajor) {
    this.smajor = smajor;
  }
  public String getStele() {
    return stele;
  }
  public void setStele(String stele) {
    this.stele = stele;
  }
}

Student.java

package com.sjsq.model;
public class Student {
  private int Sno = -1;
  private String Spassword;
  public Student() {
    super();
  }
  public Student(int sno) {
    super();
    Sno = sno;
  }
  public Student(int sno, String spassword) {
    super();
    Sno = sno;
    Spassword = spassword;
  }
  public int getSno() {
    return Sno;
  }
  public void setSno(int sno) {
    Sno = sno;
  }
  public String getSpassword() {
    return Spassword;
  }
  public void setSpassword(String spassword) {
    Spassword = spassword;
  }
}

DbUtil.java

package com.sjsq.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DbUtil {
  private String dbUrl = "jdbc:mysql://localhost:3306/courseselection_management_swing?serverTimezone=Asia/Shanghai";
  private String dbUserName = "root";
  private String dbPassword = "admin";
  private String jdbcName = "com.mysql.cj.jdbc.Driver";
  /**
   * 获取数据库连接
   * 
   * @return
   * @throws Exception
   */
  public Connection getCon() throws Exception {
    Class.forName(jdbcName);
    Connection con = DriverManager.getConnection(dbUrl, dbUserName, dbPassword);
    return con;
  }
  public void closeCon(Connection con) throws Exception {
    if (con != null) {
      con.close();
    }
  }
  public static void main(String[] args) {
    DbUtil dbUtil = new DbUtil();
    try {
      dbUtil.getCon();
      System.out.println("数据库连接成功!");
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

StringUtil.java

package com.sjsq.util;
public class StringUtil {
  public static boolean isEmpty(String str) {
    if ("".equals(str) || str == null) {
      return true;
    } else {
      return false;
    }
  }
  public static boolean isNotEmpty(String str) {
    if (!"".equals(str) && str != null) {
      return true;
    } else {
      return false;
    }
  }
}

LogOnDao.java

package com.sjsq.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import com.sjsq.model.Admin;
import com.sjsq.model.Student;
public class LogOnDao {
  /**
   * 登录验证
   * 
   * @param con
   * @param student
   * @return
   * @throws Exception
   */
  public Student login(Connection con, Student student) throws Exception {
    Student resultStu = null;
    String sql = "select * from t_slogon where Sno=? and Spassword=?";
    PreparedStatement pstmt = con.prepareStatement(sql);
    pstmt.setInt(1, student.getSno());
    pstmt.setString(2, student.getSpassword());
    ResultSet rs = pstmt.executeQuery();
    if (rs.next()) {
      resultStu = new Student();
      resultStu.setSno(rs.getInt("Sno"));
      resultStu.setSpassword(rs.getString("Spassword"));
    }
    return resultStu;
  }
  public Admin login(Connection con, Admin admin) throws Exception {
    Admin resultAdmin = null;
    String sql = "select * from t_adminlogon where adminId=? and password=?";
    PreparedStatement pstmt = con.prepareStatement(sql);
    pstmt.setInt(1, admin.getAdminId());
    pstmt.setString(2, admin.getPassword());
    ResultSet rs = pstmt.executeQuery();
    if (rs.next()) {
      resultAdmin = new Admin();
      resultAdmin.setAdminId(rs.getInt("adminId"));
      resultAdmin.setPassword(rs.getString("password"));
    }
    return resultAdmin;
  }
}

SelectionDao.java

package com.sjsq.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import com.sjsq.model.Selection;
public class SelectionDao {
  public int SelectionAdd(Connection con, Selection selection) throws Exception {
    String sql = "insert into t_selection value(null,?,?)";
    PreparedStatement pstmt = con.prepareStatement(sql);
    pstmt.setInt(1, selection.getCourseId());
    pstmt.setInt(2, selection.getSno());
    return pstmt.executeUpdate();
  }
  public int NumSelectedAdd(Connection con, int courseId) throws Exception {
    String sql = "update t_course set numSelected=numSelected+1 where courseId=?";
    PreparedStatement pstmt = con.prepareStatement(sql);
    pstmt.setInt(1, courseId);
    return pstmt.executeUpdate();
  }
  public ResultSet SelectedList(Connection con, int sno) throws Exception {
    String sql = "select  * from t_selection s,t_course c where s.Sno=? and s.courseId=c.courseId ";
    PreparedStatement pstmt = con.prepareStatement(sql);
    pstmt.setInt(1, sno);
    return pstmt.executeQuery();
  }
  public int SelectionCancel(Connection con, Selection selection) throws Exception {
    String sql = "delete from t_selection where courseId=? and Sno=?";
    PreparedStatement pstmt = con.prepareStatement(sql);
    pstmt.setInt(1, selection.getCourseId());
    pstmt.setInt(2, selection.getSno());
    return pstmt.executeUpdate();
  }
  public int NumSelectedMinus(Connection con, int courseId) throws Exception {
    String sql = "update t_course set numSelected=numSelected-1 where courseId=?";
    PreparedStatement pstmt = con.prepareStatement(sql);
    pstmt.setInt(1, courseId);
    return pstmt.executeUpdate();
  }
}

StudentDao.java

package com.sjsq.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.sjsq.model.Sinfo;
import com.sjsq.model.Student;
import com.sjsq.util.StringUtil;
public class StudentDao {
  public ResultSet StudentList(Connection con, Sinfo sinfo) throws SQLException {
    StringBuffer sb = new StringBuffer("select * from t_sinfo ");
    if (sinfo.getSno() != -1) {
      sb.append(" and Sno=" + sinfo.getSno());
    }
    if (StringUtil.isNotEmpty(sinfo.getSname())) {
      sb.append(" and Sname like '%" + sinfo.getSname() + "%'");
    }
    PreparedStatement pstmt = con.prepareStatement(sb.toString().replaceFirst("and", "where"));
    return pstmt.executeQuery();
  }
  public ResultSet PasswordList(Connection con, Student student) throws SQLException {
    StringBuffer sb = new StringBuffer("select * from t_slogon ");
    if (student.getSno() != -1) {
      sb.append("where Sno=" + student.getSno());
    }
    PreparedStatement pstmt = con.prepareStatement(sb.toString());
    return pstmt.executeQuery();
  }
  public int PasswordModify(Connection con, Student student) throws Exception {
    String sql = "update t_slogon set Spassword=? where Sno=? ";
    PreparedStatement pstmt = con.prepareStatement(sql);
    pstmt.setString(1, student.getSpassword());
    pstmt.setInt(2, student.getSno());
    return pstmt.executeUpdate();
  }
}

LogOnFrm.java

package com.sjsq.view;
import java.awt.Font;
import java.sql.Connection;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.UIManager;
import com.sjsq.dao.LogOnDao;
import com.sjsq.model.Admin;
import com.sjsq.model.Student;
import com.sjsq.util.DbUtil;
import com.sjsq.util.StringUtil;
public class LogOnFrm extends javax.swing.JFrame {
  private ButtonGroup buttonGroup1;
  private JLabel jLabel1;
  private JLabel jLabel2;
  private JLabel jLabel3;
  private JButton jb_logOn;
  private JButton jb_reset;
  private JRadioButton jrb_admin;
  private JRadioButton jrb_student;
  private JPasswordField passwordTxt;
  private JFormattedTextField userNameTxt;
  DbUtil dbUtil = new DbUtil();
  LogOnDao logOnDao = new LogOnDao();
  public static Student currentStudent;
  public LogOnFrm() {
    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.jrb_student.setSelected(true);
  }
  private void initComponents() {
    buttonGroup1 = new ButtonGroup();
    jLabel1 = new JLabel();
    jLabel2 = new JLabel();
    userNameTxt = new JFormattedTextField();
    jLabel3 = new JLabel();
    jrb_student = new JRadioButton();
    jrb_admin = new JRadioButton();
    jb_logOn = new JButton();
    jb_reset = new JButton();
    passwordTxt = new JPasswordField();
    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setTitle("用户登录页面");
    setResizable(false);
    jLabel1.setFont(new java.awt.Font("隶书", 1, 24));
    jLabel1.setIcon(new javax.swing.ImageIcon(LogOnFrm.class.getResource("/images/sys.png")));
    jLabel1.setText("学生选课系统");
    jLabel2.setIcon(new javax.swing.ImageIcon(LogOnFrm.class.getResource("/images/userName.png")));
    jLabel2.setText("账号");
    jLabel3.setIcon(new javax.swing.ImageIcon(LogOnFrm.class.getResource("/images/password.png")));
    jLabel3.setText("密码");
    buttonGroup1.add(jrb_student);
    jrb_student.setText("学生");
    jrb_student.setIcon(new javax.swing.ImageIcon(LogOnFrm.class.getResource("/images/student.png")));
    buttonGroup1.add(jrb_admin);
    jrb_admin.setText("管理员");
    jrb_admin.setIcon(new javax.swing.ImageIcon(LogOnFrm.class.getResource("/images/admin.png")));
    jb_logOn.setIcon(new javax.swing.ImageIcon(LogOnFrm.class.getResource("/images/login.png")));
    jb_logOn.setText("登录");
    jb_logOn.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jb_logOnActionPerformed(evt);
      }
    });
    jb_reset.setIcon(new javax.swing.ImageIcon(LogOnFrm.class.getResource("/images/reset.png")));
    jb_reset.setText("重置");
    jb_reset.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jb_resetActionPerformed(evt);
      }
    });
    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(106, 106, 106)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING).addComponent(jLabel1)
            .addGroup(layout.createSequentialGroup().addGroup(layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
                    layout.createSequentialGroup().addComponent(jLabel3).addGap(33, 33, 33))
                .addGroup(layout.createSequentialGroup().addComponent(jLabel2).addGap(33, 33, 33)))
                .addGap(6, 6, 6)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(passwordTxt, 0, 0, Short.MAX_VALUE)
                    .addComponent(userNameTxt, javax.swing.GroupLayout.DEFAULT_SIZE, 135,
                        Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING,
                        layout.createSequentialGroup().addGroup(layout
                            .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jb_logOn).addComponent(jrb_student))
                            .addGroup(layout
                                .createParallelGroup(
                                    javax.swing.GroupLayout.Alignment.LEADING,
                                    false)
                                .addGroup(layout.createSequentialGroup()
                                    .addGap(29, 29, 29).addComponent(jb_reset))
                                .addGroup(layout.createSequentialGroup()
                                    .addPreferredGap(
                                        javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(jrb_admin,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        Short.MAX_VALUE)))))))
        .addContainerGap(143, Short.MAX_VALUE)));
    layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addContainerGap(77, Short.MAX_VALUE).addComponent(jLabel1).addGap(39, 39, 39)
            .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))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel3).addComponent(passwordTxt, javax.swing.GroupLayout.PREFERRED_SIZE,
                    javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jrb_student).addComponent(jrb_admin))
            .addGap(32, 32, 32)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jb_reset).addComponent(jb_logOn))
            .addGap(58, 58, 58)));
    pack();
  }
  private void jb_logOnActionPerformed(java.awt.event.ActionEvent evt) {
    String userName = userNameTxt.getText();
    String password = new String(passwordTxt.getPassword());
    if (StringUtil.isEmpty(userName)) {
      JOptionPane.showMessageDialog(this, "账号不能为空!");
      return;
    }
    if (StringUtil.isEmpty(password)) {
      JOptionPane.showMessageDialog(this, "密码不能为空!");
      return;
    }
    Connection con = null;
    if (this.jrb_student.isSelected()) {
      Student student = new Student(Integer.parseInt(userName), password);
      try {
        con = dbUtil.getCon();
        currentStudent = logOnDao.login(con, student);
        if (currentStudent != null) {
          this.dispose();
          new MainFrm_student().setVisible(true);
        } else {
          JOptionPane.showMessageDialog(this, "用户名或密码错误!");
        }
      } catch (Exception e) {
        e.printStackTrace();
        JOptionPane.showMessageDialog(this, "登录失败!");
      } finally {
        try {
          dbUtil.closeCon(con);
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    } else if (this.jrb_admin.isSelected()) {
      Admin admin = new Admin(Integer.parseInt(userName), password);
      try {
        con = dbUtil.getCon();
        Admin currentAdmin = logOnDao.login(con, admin);
        if (currentAdmin != null) {
          this.dispose();
          new MainFrm_admin().setVisible(true);
        } else {
          JOptionPane.showMessageDialog(this, "用户名或密码错误!");
        }
      } catch (Exception e) {
        e.printStackTrace();
        JOptionPane.showMessageDialog(this, "登录失败!");
      } finally {
        try {
          dbUtil.closeCon(con);
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    }
  }
  private void jb_resetActionPerformed(java.awt.event.ActionEvent evt) {
    this.resetValue();
  }
  private void resetValue() {
    this.userNameTxt.setText("");
    this.passwordTxt.setText("");
    this.jrb_student.setSelected(true);
  }
  public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
      public void run() {
        new LogOnFrm().setVisible(true);
      }
    });
  }
}

CourseAddInterFrm.java

package com.sjsq.view;
import java.sql.Connection;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;
import com.sjsq.dao.CourseDao;
import com.sjsq.model.Course;
import com.sjsq.util.DbUtil;
import com.sjsq.util.StringUtil;
public class CourseAddInterFrm extends javax.swing.JInternalFrame {
  private JTextField capacityTxt;
  private JTextField courseNameTxt;
  private JTextField courseTeacherTxt;
  private JTextField courseTimeTxt;
  private JLabel jLabel1;
  private JLabel jLabel2;
  private JLabel jLabel3;
  private JLabel jLabel4;
  private JButton jb_add;
  private JButton jb_reset;
  DbUtil dbUtil = new DbUtil();
  CourseDao coursedao = new CourseDao();
  public CourseAddInterFrm() {
    initComponents();
    this.setLocation(200, 50);
  }
  private void initComponents() {
    jLabel1 = new JLabel();
    jLabel2 = new JLabel();
    courseTimeTxt = new JTextField();
    jLabel3 = new JLabel();
    jLabel4 = new JLabel();
    courseNameTxt = new JTextField();
    courseTeacherTxt = new JTextField();
    capacityTxt = new JTextField();
    jb_add = new JButton();
    jb_reset = new JButton();
    setClosable(true);
    setIconifiable(true);
    setTitle("添加课程");
    jLabel1.setText("课程名称:");
    jLabel2.setText("上课时间:");
    jLabel3.setText("任课老师:");
    jLabel4.setText("课程容量:");
    jb_add.setIcon(new javax.swing.ImageIcon(LogOnFrm.class.getResource("/images/add.png")));
    jb_add.setText("添加");
    jb_add.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jb_addActionPerformed(evt);
      }
    });
    jb_reset.setIcon(new javax.swing.ImageIcon(LogOnFrm.class.getResource("/images/reset.png")));
    jb_reset.setText("重置");
    jb_reset.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jb_resetActionPerformed(evt);
      }
    });
    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(41, 41, 41)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup().addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(courseNameTxt, javax.swing.GroupLayout.PREFERRED_SIZE, 144,
                    javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(60, 60, 60).addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(
                    courseTimeTxt, javax.swing.GroupLayout.PREFERRED_SIZE, 144,
                    javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup().addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(courseTeacherTxt, javax.swing.GroupLayout.PREFERRED_SIZE,
                            144, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jb_add))
                .addGap(60, 60, 60)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup().addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(capacityTxt, javax.swing.GroupLayout.PREFERRED_SIZE, 144,
                            javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jb_reset))))
        .addContainerGap(44, Short.MAX_VALUE)));
    layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup().addGap(46, 46, 46)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel1).addComponent(jLabel2)
                .addComponent(courseTimeTxt, javax.swing.GroupLayout.PREFERRED_SIZE,
                    javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(courseNameTxt, javax.swing.GroupLayout.PREFERRED_SIZE,
                    javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGap(18, 18, 18)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel3).addComponent(jLabel4)
                .addComponent(courseTeacherTxt, javax.swing.GroupLayout.PREFERRED_SIZE,
                    javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(capacityTxt, javax.swing.GroupLayout.PREFERRED_SIZE,
                    javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 55, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jb_reset).addComponent(jb_add))
            .addGap(20, 20, 20)));
    pack();
  }
  private void jb_addActionPerformed(java.awt.event.ActionEvent evt) {
    String courseName = this.courseNameTxt.getText();
    String courseTime = this.courseTimeTxt.getText();
    String courseTeacher = this.courseTeacherTxt.getText();
    String capacity = this.capacityTxt.getText();
    if (StringUtil.isEmpty(courseName)) {
      JOptionPane.showMessageDialog(this, "课程名称不能为空!");
      return;
    }
    if (StringUtil.isEmpty(courseTime)) {
      JOptionPane.showMessageDialog(this, "上课时间不能为空!");
      return;
    }
    if (StringUtil.isEmpty(courseTeacher)) {
      JOptionPane.showMessageDialog(this, "任课老师不能为空!");
      return;
    }
    if (StringUtil.isEmpty(capacity)) {
      JOptionPane.showMessageDialog(this, "课程容量不能为空!");
      return;
    }
    Course course = new Course(courseName, courseTime, courseTeacher, Integer.parseInt(capacity));
    Connection con = null;
    try {
      con = dbUtil.getCon();
      int n = coursedao.courseAdd(con, course);
      if (n == 1) {
        JOptionPane.showMessageDialog(this, "课程添加成功!");
        this.resetValue();
      } else {
        JOptionPane.showMessageDialog(this, "课程添加失败!");
      }
    } catch (Exception e) {
      e.printStackTrace();
      JOptionPane.showMessageDialog(this, "课程添加失败!");
    }
  }
  private void jb_resetActionPerformed(java.awt.event.ActionEvent evt) {
    this.resetValue();
  }
  private void resetValue() {
    this.courseNameTxt.setText("");
    this.courseTeacherTxt.setText("");
    this.courseTimeTxt.setText("");
    this.capacityTxt.setText("");
  }
}
相关文章
|
10天前
|
运维 自然语言处理 供应链
Java云HIS医院管理系统源码 病案管理、医保业务、门诊、住院、电子病历编辑器
通过门诊的申请,或者直接住院登记,通过”护士工作站“分配患者,完成后,进入医生患者列表,医生对应开具”长期医嘱“和”临时医嘱“,并在电子病历中,记录病情。病人出院时,停止长期医嘱,开具出院医嘱。进入出院审核,审核医嘱与住院通过后,病人结清缴费,完成出院。
35 3
|
14天前
|
Java 数据库连接 数据库
深入探讨Java连接池技术如何通过复用数据库连接、减少连接建立和断开的开销,从而显著提升系统性能
在Java应用开发中,数据库操作常成为性能瓶颈。本文通过问题解答形式,深入探讨Java连接池技术如何通过复用数据库连接、减少连接建立和断开的开销,从而显著提升系统性能。文章介绍了连接池的优势、选择和使用方法,以及优化配置的技巧。
16 1
|
15天前
|
JavaScript Java 项目管理
Java毕设学习 基于SpringBoot + Vue 的医院管理系统 持续给大家寻找Java毕设学习项目(附源码)
基于SpringBoot + Vue的医院管理系统,涵盖医院、患者、挂号、药物、检查、病床、排班管理和数据分析等功能。开发工具为IDEA和HBuilder X,环境需配置jdk8、Node.js14、MySQL8。文末提供源码下载链接。
|
18天前
|
移动开发 前端开发 JavaScript
java家政系统成品源码的关键特点和技术应用
家政系统成品源码是已开发完成的家政服务管理软件,支持用户注册、登录、管理个人资料,家政人员信息管理,服务项目分类,订单与预约管理,支付集成,评价与反馈,地图定位等功能。适用于各种规模的家政服务公司,采用uniapp、SpringBoot、MySQL等技术栈,确保高效管理和优质用户体验。
|
20天前
|
XML JSON 监控
告别简陋:Java日志系统的最佳实践
【10月更文挑战第19天】 在Java开发中,`System.out.println()` 是最基本的输出方法,但它在实际项目中往往被认为是不专业和不足够的。本文将探讨为什么在现代Java应用中应该避免使用 `System.out.println()`,并介绍几种更先进的日志解决方案。
44 1
|
5天前
|
安全 Java 测试技术
Java并行流陷阱:为什么指定线程池可能是个坏主意
本文探讨了Java并行流的使用陷阱,尤其是指定线程池的问题。文章分析了并行流的设计思想,指出了指定线程池的弊端,并提供了使用CompletableFuture等替代方案。同时,介绍了Parallel Collector库在处理阻塞任务时的优势和特点。
|
14天前
|
安全 Java
java 中 i++ 到底是否线程安全?
本文通过实例探讨了 `i++` 在多线程环境下的线程安全性问题。首先,使用 100 个线程分别执行 10000 次 `i++` 操作,发现最终结果小于预期的 1000000,证明 `i++` 是线程不安全的。接着,介绍了两种解决方法:使用 `synchronized` 关键字加锁和使用 `AtomicInteger` 类。其中,`AtomicInteger` 通过 `CAS` 操作实现了高效的线程安全。最后,通过分析字节码和源码,解释了 `i++` 为何线程不安全以及 `AtomicInteger` 如何保证线程安全。
java 中 i++ 到底是否线程安全?
|
2天前
|
安全 Java 开发者
深入解读JAVA多线程:wait()、notify()、notifyAll()的奥秘
在Java多线程编程中,`wait()`、`notify()`和`notifyAll()`方法是实现线程间通信和同步的关键机制。这些方法定义在`java.lang.Object`类中,每个Java对象都可以作为线程间通信的媒介。本文将详细解析这三个方法的使用方法和最佳实践,帮助开发者更高效地进行多线程编程。 示例代码展示了如何在同步方法中使用这些方法,确保线程安全和高效的通信。
15 9
|
5天前
|
存储 安全 Java
Java多线程编程的艺术:从基础到实践####
本文深入探讨了Java多线程编程的核心概念、应用场景及其实现方式,旨在帮助开发者理解并掌握多线程编程的基本技能。文章首先概述了多线程的重要性和常见挑战,随后详细介绍了Java中创建和管理线程的两种主要方式:继承Thread类与实现Runnable接口。通过实例代码,本文展示了如何正确启动、运行及同步线程,以及如何处理线程间的通信与协作问题。最后,文章总结了多线程编程的最佳实践,为读者在实际项目中应用多线程技术提供了宝贵的参考。 ####
|
2天前
|
监控 安全 Java
Java中的多线程编程:从入门到实践####
本文将深入浅出地探讨Java多线程编程的核心概念、应用场景及实践技巧。不同于传统的摘要形式,本文将以一个简短的代码示例作为开篇,直接展示多线程的魅力,随后再详细解析其背后的原理与实现方式,旨在帮助读者快速理解并掌握Java多线程编程的基本技能。 ```java // 简单的多线程示例:创建两个线程,分别打印不同的消息 public class SimpleMultithreading { public static void main(String[] args) { Thread thread1 = new Thread(() -> System.out.prin