0


Java+Swing+MySQL实现学生选课管理系统

一、系统介绍

1.运行环境

开发工具:Eclipse/IDEA

JDK版本:jdk1.8

Mysql版本:5.7

2.技术说明

Java+Swing+Mysql

3.系统功能

主要功能包括 1.管理学生信息,其中包括添加,删除,修改等操作。2.管理课程信息,其中包括添加,删除,修改等操作。3.管理选课信息,其中包括添加,删除,修改等操作。4.查询信息,其中包括查询学生信息,查询课程信息,查询选课信息等操作

4.数据库实体E-R图设计

学生管理实体E-R图

课程管理实体E-R图

选课管理实体E-R图

5.数据库表的设计

数据库名:user

管理员表(user)

列名

数据类型

长度

是否为空

uno

int

11

name

varchar

10

password

varchar

10

数据库名:stu

学生表(stu)

列名

数据类型

长度

是否为空

sno

Int

11

name

varchar

10

sex

varchar

10

age

int

10

sdept

varchar

10

数据库名:course

课程表(course)

列名

数据类型

长度

是否为空

cno

char

4

cname

varchar

10

credit

Int

10

数据库名:sc

选课表(sc)

列名

数据类型

长度

是否为空

sno

char

11

cno

char

4

score

Int

10

二、系统实现

根据需求分析阶段得到的功能需求,可将系统功能大概分为六个模块:这几个模块用户登录、学生管理、课程管理、选课管理、查询管理、系统退出

系统数据流程图

1.用户登录

public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("登录")) {
            try {
                DBUtil.initst();
                String stuname=nameField.getText();
                String stupwd=passwordField.getText();
                //根据数据库表中的用户名查密码进行匹配
                DBUtil.rs=DBUtil.st.executeQuery("select password from user where name='"+stuname+"'");
                if (DBUtil.rs.next()) {
                    if (DBUtil.rs.getString(1).equals(stupwd)) {
                        //如果密码正确就显示主页面
                        this.hide();
                        new Main().show();
                    } else {
                        //如果密码错误弹出框
                        JOptionPane.showMessageDialog(null, "密码错误,请联系管理员");}
                }else {
                    //姓名不对弹出提示框
                    JOptionPane.showMessageDialog(null, "用户不存在");
                }
                DBUtil.closeDB();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }else {System.exit(0)} }

2.主界面

主界面主要是用于对选择相应的功能进行相应的功能,主界面主要包括学生管理、课程管理、选课管理、数据查询、系统退出五大功能。    主界面添加JMenuBar菜单条,和JMenu菜单,和JMenuItem菜单项。三个菜单包括:学生管理、课程管理、选课管理和数据查询。学生管理中包括:增加学生、修改学生、删除学生。课程管理中包括:增加课程、修改课程、删除课程。选课管理中包括:增加选课、修改选课、删除选课。数据查询主要包括学生查询、课程查询、选课查询。还有一个单独的系统退出。

重要代码:

// 学生表
        JTable jTable = new JTable();
        jTable.setModel(userModel);
        JScrollPane jScrollPane = new JScrollPane();
        jScrollPane.setViewportView(jTable);
        add(jScrollPane, BorderLayout.CENTER);
// 菜单栏
        JMenuBar menuBar = new JMenuBar();
        setJMenuBar(menuBar);
        // 学生信息菜单栏
        JMenu user = new JMenu("  学生管理  ");
        menuBar.add(user);
        JMenuItem addstu = new JMenuItem("增加学生");
        JMenuItem updatestu = new JMenuItem("修改学生");
        JMenuItem deletestu = new JMenuItem("删除学生");
        user.add(addstu);
        user.add(updatestu);
        user.add(deletestu);
        // 系统退出
        JLabel exit = new JLabel("    系统退出");
        exit.setForeground(Color.red);
        exit.setFont(new java.awt.Font("宋体", 1, 15));
        menuBar.add(exit);
        // 查询区
        JPanel jPaneln = new JPanel();
        JLabel nameJLabel = new JLabel("学号/课程号:");
        JTextField nameField = new JTextField(20);
        JButton selectbtn = new JButton("查询");
        jPaneln.add(nameJLabel);
        jPaneln.add(nameField);
        jPaneln.add(selectbtn);
        add(jPaneln, BorderLayout.NORTH);
        // 查询学号/课程号按钮
        selectbtn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                String stu = nameField.getText();
                String sql = "select * from stu where sno='" + stu + "'";
                userModel.reload(sql);
                String sql1 = "select * from course where cno='" + stu + "'";
                coursemodel.reload(sql1);
                String sql2 = "SELECT sc.sno,stu.name,sc.cno,course.cname,credit,score FROM sc,stu,course where sc.sno=stu.sno and course.cno=sc.cno and sc.sno='"+ stu + "'";
            selectsmodel.reload(sql2);}});

3.数据库连接

学生、课程以及成绩的增删改查都是建立在连接数据库的基础之上,在MySQL里建立用户管理员表、学生表、课程表、选课表等。建立JDBC数据源的连接。

连接数据库重要代码:

public class DBUtil {
    static Connection con;
    public static Statement st;
    static PreparedStatement ps;
    public static ResultSet rs;
    static String url="jdbc:mysql://localhost:3306/studb?useUnicode=true&characterEncoding=UTF-8";
    static String name="root";
    static String pwd="123456";
    public static void initst() {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            con=DriverManager.getConnection(url,name,pwd);
            st=con.createStatement();
        } catch (SQLException e) {
            e.printStackTrace();
        }catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    public static void initps(String sql) {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            con=DriverManager.getConnection(url,name,pwd);
            ps=con.prepareStatement(sql);
            rs= ps.executeQuery();
        } catch (SQLException e) {
            e.printStackTrace();
        }catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    public static void closeDB() {
        try {
            if (rs!=null) {
                rs.close();
            }
            if (ps!=null) {
                ps.close();
            } 
            if (st!=null) {
                st.close();
            } 
            if (con!=null) {
                con.close();
            } 
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();

4.数据查询

5.查询课程信息

**重要代码: **

/*
         * 查询管理
         */
        // 查询所有学生信息
        querystu.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                // 刷新jTable并把jTable显示出来
                userModel.reload(null);
                jTable.setModel(userModel);
            }
        });
        // 查询所有课程信息
        querycourse.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                // 刷新jTable并把jTable显示出来
                coursemodel.reload(null);
                jTable.setModel(coursemodel);
            }
        });
        // 查询所有学生选课信息
        queryselect.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                // 刷新jTable并把jTable显示出来
                selectsmodel.reload(null);
                jTable.setModel(selectsmodel);
            }
        });

6.添加学生信息

添加学生信息主要代码:

public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("确定")) {
            try {
                DBUtil.initst();
                String sno=snoField.getText();
                String stuname=nameField.getText();
                String sex=sexField.getText();
                String age=ageField.getText();
                String sdept=sdeptField.getText();
                int n=DBUtil.st.executeUpdate("insert into stu values('"+sno+"','"+stuname+"','"+sex+"','"+age+"','"+sdept+"')");
                if (n>0) {
                    JOptionPane.showMessageDialog(null, "添加成功");   
                } else {
                    JOptionPane.showMessageDialog(null, "添加失败");   
                }
                DBUtil.closeDB();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            DBUtil.closeDB();
        }else {
            this.hide();
            UserModel.reload(null);
        }
    }

7.修改学生信息

这部分的主要功能是更改数据库中学生的基本信息。查询出学生表,点击表中的某一行进行选中,再点击学生信息菜单栏的修改学生信息就可以进行修改

修改学生信息:

三、部分代码

1.ChangePwd.java

package body;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class ChangePwd extends JFrame implements ActionListener {
    JTextField nameField;
    JPasswordField passwordField;
    public ChangePwd() {
        setTitle("修改密码");
        setLayout(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(500,200,350,300);
        
        JLabel nameJLabel=new JLabel("账号");
        nameField=new JTextField();
        JLabel passJLabel=new JLabel("密码");
        passwordField=new JPasswordField();
        nameJLabel.setBounds(50,15,100,100);
        nameField.setBounds(100,50,200,30);
        passJLabel.setBounds(50,70,100,100);
        passwordField.setBounds(100,100,200,30);
        JButton loginButton=new JButton("确定");
        JButton cancelButton=new JButton("取消");
        loginButton.setBounds(100,150,80,30);
        cancelButton.setBounds(200,150,80,30);
        add(nameJLabel);
        add(nameField);
        add(passJLabel);
        add(passwordField);
        add(loginButton);
        add(cancelButton);
        
        loginButton.setActionCommand("登录");
        cancelButton.setActionCommand("取消");
        loginButton.addActionListener(this);
        cancelButton.addActionListener(this);
        
        setVisible(true);
    }
    
    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("登录")) {
            try {
                DBUtil.initst();
                String stuname=nameField.getText();
                String stupwd=passwordField.getText();
                int n=DBUtil.st.executeUpdate("update user set password='"+stupwd+"' where name='"+stuname+"'");
                if (n>0) {
                    JOptionPane.showMessageDialog(null, "修改成功");   
                } else {
                    JOptionPane.showMessageDialog(null, "修改失败");   
                }
                DBUtil.closeDB();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }else {
            this.hide();
            new Login().show();
        } 
    }
    public static void main(String[] args) {
        ChangePwd changePwd=new ChangePwd();
    }
}

2.Login.java

package body;

import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class Login extends JFrame implements ActionListener {
    JTextField nameField;
    JPasswordField passwordField;
    public Login() {
        
        JPanel panel = new ImagePanel();
        add(panel);
        panel.setLayout(null);
        
        setTitle("系统登录");
        setLayout(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(500,200,350,300);
        //添加账号密码文本及文本框
        JLabel nameJLabel=new JLabel("账号");
        nameField=new JTextField();
        JLabel passJLabel=new JLabel("密码");
        passwordField=new JPasswordField();
        nameJLabel.setBounds(50,15,100,100);
        nameField.setBounds(100,50,200,30);
        passJLabel.setBounds(50,70,100,100);
        passwordField.setBounds(100,100,200,30);
        //登录取消按钮
        JButton loginButton=new JButton("登录");
        JButton cancelButton=new JButton("取消");
        loginButton.setBounds(50,150,80,30);
        cancelButton.setBounds(220,150,80,30);
        //添加到面版
        add(nameJLabel);
        add(nameField);
        add(passJLabel);
        add(passwordField);
        add(loginButton);
        add(cancelButton);
        //给登录取消按钮添加控件
        loginButton.setActionCommand("登录");
        cancelButton.setActionCommand("取消");
        loginButton.addActionListener(this);
        cancelButton.addActionListener(this);
        
        setVisible(true);
    }
    
    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("登录")) {
            try {
                DBUtil.initst();
                String stuname=nameField.getText();
                String stupwd=passwordField.getText();
                //根据数据库表中的用户名查密码进行匹配
                DBUtil.rs=DBUtil.st.executeQuery("select password from user where name='"+stuname+"'");
                if (DBUtil.rs.next()) {
                    if (DBUtil.rs.getString(1).equals(stupwd)) {
                        //如果密码正确就显示主页面
                        this.hide();
                        new Main().show();
                    } else {
                        //如果密码错误弹出框
                        JOptionPane.showMessageDialog(null, "密码错误,请联系管理员");
                    }
                }else {
                    //姓名不对弹出提示框
                    JOptionPane.showMessageDialog(null, "用户不存在");
                }
                DBUtil.closeDB();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }else {
            System.exit(0);
        } 
    }
    class ImagePanel extends JPanel {
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            ImageIcon icon = new ImageIcon("icon/1.jpg");
            g.drawImage(icon.getImage(), 0, 0, null);
        }
    }

    public static void main(String[] args) {
        Login login=new Login();
    }
}

3.Main.java

package body;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.SQLException;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;

import course.AddCourse;
import course.CourseModel;
import course.UpdateCourse;
import selects.AddSelect;
import selects.SelectsModel;
import selects.UpdateSelect;
import stu.AddStu;
import stu.UpdateStu;
import stu.UserModel;

public class Main extends JFrame {
    public static UserModel userModel;
    public static CourseModel coursemodel;
    public static SelectsModel selectsmodel;
    JTextField exit;

    public Main() {
        setTitle("学生选课管理系统");
        setBounds(300, 100, 1000, 800);

        String sql = "select * from stu";
        userModel = new UserModel();
        userModel.init(sql);

        String sql1 = "select * from course";
        coursemodel = new CourseModel();
        coursemodel.init(sql1);

        String sql2 = "SELECT sc.sno,stu.name,sc.cno,course.cname,credit,score FROM sc,stu,course where sc.sno=stu.sno and course.cno=sc.cno";
        selectsmodel = new SelectsModel();
        selectsmodel.init(sql2);

        // 学生表
        JTable jTable = new JTable();
        
        jTable.setModel(userModel);
        JScrollPane jScrollPane = new JScrollPane();
        jScrollPane.setViewportView(jTable);
        
        add(jScrollPane, BorderLayout.CENTER);
        // 菜单栏
        JMenuBar menuBar = new JMenuBar();
        setJMenuBar(menuBar);
        // 学生信息菜单栏
        JMenu user = new JMenu("  学生管理  ");
        menuBar.add(user);
        JMenuItem addstu = new JMenuItem("增加学生");
        JMenuItem updatestu = new JMenuItem("修改学生");
        JMenuItem deletestu = new JMenuItem("删除学生");
        user.add(addstu);
        user.add(updatestu);
        user.add(deletestu);

        // 课程管理菜单栏
        JMenu course = new JMenu("  课程管理  ");
        menuBar.add(course);
        JMenuItem addcourse = new JMenuItem("增加课程");
        JMenuItem updatecourse = new JMenuItem("修改课程");
        JMenuItem deletecourse = new JMenuItem("删除课程");
        course.add(addcourse);
        course.add(updatecourse);
        course.add(deletecourse);

        // 选课管理菜单栏
        JMenu select = new JMenu("  选课管理  ");
        menuBar.add(select);
        JMenuItem addselect = new JMenuItem("添加选课");
        JMenuItem updateselect = new JMenuItem("修改选课");
        JMenuItem deleteselect = new JMenuItem("删除选课");
        select.add(addselect);
        select.add(updateselect);
        select.add(deleteselect);

        // 查询管理菜单栏
        JMenu query = new JMenu("  查询管理  ");
        menuBar.add(query);
        JMenuItem querystu = new JMenuItem("查询所有学生");
        JMenuItem querycourse = new JMenuItem("查询所有课程");
        JMenuItem queryselect = new JMenuItem("查询所有选课");
        query.add(querystu);
        query.add(querycourse);
        query.add(queryselect);

        // 系统退出
        JLabel exit = new JLabel("    系统退出");
        exit.setForeground(Color.red);
        exit.setFont(new java.awt.Font("宋体", 1, 15));
        menuBar.add(exit);

        // 查询区
        JPanel jPaneln = new JPanel();
        JLabel nameJLabel = new JLabel("学号/课程号:");
        JTextField nameField = new JTextField(20);
        JButton selectbtn = new JButton("查询");
        jPaneln.add(nameJLabel);
        jPaneln.add(nameField);
        jPaneln.add(selectbtn);
        add(jPaneln, BorderLayout.NORTH);
        // 查询学号/课程号按钮
        selectbtn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                String stu = nameField.getText();
                String sql = "select * from stu where sno='" + stu + "'";
                userModel.reload(sql);
                String sql1 = "select * from course where cno='" + stu + "'";
                coursemodel.reload(sql1);
                String sql2 = "SELECT sc.sno,stu.name,sc.cno,course.cname,credit,score FROM sc,stu,course where sc.sno=stu.sno and course.cno=sc.cno and sc.sno='"
                        + stu + "'";
                selectsmodel.reload(sql2);

            }
        });

        /*
         * 学生管理
         */
        // 增加学生信息
        addstu.addMouseListener(new MouseListener() {

            @Override
            public void mouseReleased(MouseEvent e) {
                // TODO Auto-generated method stub

            }

            @Override
            public void mousePressed(MouseEvent e) {
                // TODO Auto-generated method stub
                new AddStu().show();
            }

            @Override
            public void mouseExited(MouseEvent e) {
                // TODO Auto-generated method stub

            }

            @Override
            public void mouseEntered(MouseEvent e) {
                // TODO Auto-generated method stub

            }

            @Override
            public void mouseClicked(MouseEvent e) {
                // TODO Auto-generated method stub

            }
        });
        // 删除学生信息
        deletestu.addMouseListener(new MouseListener() {

            @Override
            public void mouseReleased(MouseEvent e) {
                // TODO Auto-generated method stub

            }

            @Override
            public void mousePressed(MouseEvent e) {
                // TODO Auto-generated method stub
                int index = jTable.getSelectedRow();
                if (index == -1) {
                    JOptionPane.showMessageDialog(null, "未选中");
                } else {
                    Object user = userModel.rowData.get(index);
                    String sno = ((Vector) user).get(0).toString();
                    try {
                        DBUtil.initst();
                        int n = DBUtil.st.executeUpdate("delete from stu where sno='" + sno + "'");
                        if (n > 0) {
                            JOptionPane.showMessageDialog(null, "删除成功");
                        } else {
                            JOptionPane.showMessageDialog(null, "删除失败");
                        }
                        UserModel.reload(null);
                        DBUtil.closeDB();
                    } catch (SQLException ex) {
                        ex.printStackTrace();
                    }
                }
            }

            @Override
            public void mouseExited(MouseEvent e) {
                // TODO Auto-generated method stub

            }

            @Override
            public void mouseEntered(MouseEvent e) {
                // TODO Auto-generated method stub

            }

            @Override
            public void mouseClicked(MouseEvent e) {
                // TODO Auto-generated method stub

            }
        });
        // 修改学生信息
        updatestu.addMouseListener(new MouseListener() {

            @Override
            public void mouseReleased(MouseEvent e) {
                // TODO Auto-generated method stub

            }

            @Override
            public void mousePressed(MouseEvent e) {
                // TODO Auto-generated method stub
                int index = jTable.getSelectedRow();
                if (index == -1) {
                    JOptionPane.showMessageDialog(null, "未选中");
                } else {
                    Object user = userModel.rowData.get(index);
                    new UpdateStu(user).show();
                }
            }

            @Override
            public void mouseExited(MouseEvent e) {
                // TODO Auto-generated method stub

            }

            @Override
            public void mouseEntered(MouseEvent e) {
                // TODO Auto-generated method stub

            }

            @Override
            public void mouseClicked(MouseEvent e) {
                // TODO Auto-generated method stub

            }
        });
        /*
         * 课程管理
         */
        // 增加课程信息
        addcourse.addMouseListener(new MouseListener() {

            @Override
            public void mouseReleased(MouseEvent e) {
                // TODO Auto-generated method stub

            }

            @Override
            public void mousePressed(MouseEvent e) {
                // TODO Auto-generated method stub
                new AddCourse().show();
            }

            @Override
            public void mouseExited(MouseEvent e) {
                // TODO Auto-generated method stub

            }

            @Override
            public void mouseEntered(MouseEvent e) {
                // TODO Auto-generated method stub

            }

            @Override
            public void mouseClicked(MouseEvent e) {
                // TODO Auto-generated method stub

            }
        });
        // 删除课程信息
        deletecourse.addMouseListener(new MouseListener() {

            @Override
            public void mouseReleased(MouseEvent e) {
                // TODO Auto-generated method stub

            }

            @Override
            public void mousePressed(MouseEvent e) {
                // TODO Auto-generated method stub
                int index = jTable.getSelectedRow();
                if (index == -1) {
                    JOptionPane.showMessageDialog(null, "未选中");
                } else {
                    Object course = coursemodel.rowData1.get(index);
                    String cno = ((Vector) course).get(0).toString();
                    try {
                        DBUtil.initst();
                        int n = DBUtil.st.executeUpdate("delete from course where cno='" + cno + "'");
                        if (n > 0) {
                            JOptionPane.showMessageDialog(null, "删除成功");
                        } else {
                            JOptionPane.showMessageDialog(null, "删除失败");
                        }
                        CourseModel.reload(null);
                        DBUtil.closeDB();
                    } catch (SQLException ex) {
                        ex.printStackTrace();
                    }
                }
            }

            @Override
            public void mouseExited(MouseEvent e) {
                // TODO Auto-generated method stub

            }

            @Override
            public void mouseEntered(MouseEvent e) {
                // TODO Auto-generated method stub

            }

            @Override
            public void mouseClicked(MouseEvent e) {
                // TODO Auto-generated method stub

            }
        });
        // 修改课程信息
        updatecourse.addMouseListener(new MouseListener() {

            @Override
            public void mouseReleased(MouseEvent e) {
                // TODO Auto-generated method stub

            }

            @Override
            public void mousePressed(MouseEvent e) {
                // TODO Auto-generated method stub
                int index = jTable.getSelectedRow();
                if (index == -1) {
                    JOptionPane.showMessageDialog(null, "未选中");
                } else {
                    Object course = coursemodel.rowData1.get(index);
                    new UpdateCourse(course).show();
                }
            }

            @Override
            public void mouseExited(MouseEvent e) {
                // TODO Auto-generated method stub

            }

            @Override
            public void mouseEntered(MouseEvent e) {
                // TODO Auto-generated method stub

            }

            @Override
            public void mouseClicked(MouseEvent e) {
                // TODO Auto-generated method stub

            }
        });
        /*
         * 学生选课管理
         */
        // 增加学生选课信息
        addselect.addMouseListener(new MouseListener() {

            @Override
            public void mouseReleased(MouseEvent e) {
                // TODO Auto-generated method stub

            }

            @Override
            public void mousePressed(MouseEvent e) {
                // TODO Auto-generated method stub
                new AddSelect().show();
            }

            @Override
            public void mouseExited(MouseEvent e) {
                // TODO Auto-generated method stub

            }

            @Override
            public void mouseEntered(MouseEvent e) {
                // TODO Auto-generated method stub

            }

            @Override
            public void mouseClicked(MouseEvent e) {
                // TODO Auto-generated method stub

            }
        });
        // 删除学生选课信息
        deleteselect.addMouseListener(new MouseListener() {

            @Override
            public void mouseReleased(MouseEvent e) {
                // TODO Auto-generated method stub

            }

            @Override
            public void mousePressed(MouseEvent e) {
                // TODO Auto-generated method stub
                int index = jTable.getSelectedRow();
                if (index == -1) {
                    JOptionPane.showMessageDialog(null, "未选中");
                } else {
                    Object select = selectsmodel.rowData2.get(index);
                    String sno = ((Vector) select).get(0).toString();
                    try {
                        DBUtil.initst();
                        int n = DBUtil.st.executeUpdate("delete from sc where sno='" + sno + "'");
                        if (n > 0) {
                            JOptionPane.showMessageDialog(null, "删除成功");
                        } else {
                            JOptionPane.showMessageDialog(null, "删除失败");
                        }
                        SelectsModel.reload(null);
                        DBUtil.closeDB();
                    } catch (SQLException ex) {
                        ex.printStackTrace();
                    }
                }
            }

            @Override
            public void mouseExited(MouseEvent e) {
                // TODO Auto-generated method stub

            }

            @Override
            public void mouseEntered(MouseEvent e) {
                // TODO Auto-generated method stub

            }

            @Override
            public void mouseClicked(MouseEvent e) {
                // TODO Auto-generated method stub

            }
        });
        // 修改学生选课信息
        updateselect.addMouseListener(new MouseListener() {

            @Override
            public void mouseReleased(MouseEvent e) {
                // TODO Auto-generated method stub

            }

            @Override
            public void mousePressed(MouseEvent e) {
                // TODO Auto-generated method stub
                int index = jTable.getSelectedRow();
                if (index == -1) {
                    JOptionPane.showMessageDialog(null, "未选中");
                } else {
                    Object select = selectsmodel.rowData2.get(index);
                    new UpdateSelect(select).show();
                }
            }

            @Override
            public void mouseExited(MouseEvent e) {
                // TODO Auto-generated method stub

            }

            @Override
            public void mouseEntered(MouseEvent e) {
                // TODO Auto-generated method stub

            }

            @Override
            public void mouseClicked(MouseEvent e) {
                // TODO Auto-generated method stub

            }
        });

        /*
         * 查询管理
         */
        // 查询所有学生信息
        querystu.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                // 刷新jTable并把jTable显示出来
                userModel.reload(null);
                jTable.setModel(userModel);
            }
        });
        // 查询所有课程信息
        querycourse.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                // 刷新jTable并把jTable显示出来
                coursemodel.reload(null);
//                jTable.show();
                jTable.setModel(coursemodel);
            }
        });
        // 查询所有学生选课信息
        queryselect.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                // 刷新jTable并把jTable显示出来
                selectsmodel.reload(null);
//                jTable.show();
                jTable.setModel(selectsmodel);
            }
        });
        /*
         * 系统退出
         */
        exit.addMouseListener(new MouseListener() {
            public void mouseClicked(MouseEvent e) {
                System.exit(0);
                // 处理鼠标点击
            }

            public void mouseEntered(MouseEvent e) {
                // 处理鼠标移入
            }

            public void mouseExited(MouseEvent e) {
                // 处理鼠标离开
            }

            public void mousePressed(MouseEvent e) {
                // 处理鼠标按下
            }

            public void mouseReleased(MouseEvent e) {
                // 处理鼠标释放
            }
        });
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        Main main = new Main();
    }
}

5.备注

如有侵权请联系我删除

标签: mysql java 数据库

本文转载自: https://blog.csdn.net/qq_61827376/article/details/125372845
版权归原作者 一点素材 所有, 如有侵权,请联系我们删除。

“Java+Swing+MySQL实现学生选课管理系统”的评论:

还没有评论