• 学生信息管理系统(JAVA+MYSQL)


    基于Java swing+MySQL实现学生信息管理系统:功能:1录入学生基本信息的功能; 2查询学生基本信息的功能; 3修改学生基本信息的功能 ;4删除学生基本信息的功能 ;5显示所有学生信息的功能;应付一般课设足矣,分享给大家。

    如有需要:https://pan.baidu.com/s/1JqLFKPlmhV2INeETy9lHAQ

    提取码:nima

    里面包括了所有代码源文件+mysql8.0.25驱动jar包+登录页面时的背景图345.jpg

    1.开发环境:jdk11+win10+mysql 8+IDEA

    记得将数据库与IDEA或者eclipse连接起来,并记得添加数据库驱动jar包

    这两个分别是添加jar包和idea连接mysql大家可以做一下参考

    IDEA导入mysql数据库驱动_跟着太阳.的博客-CSDN博客[这里是图片004]https://blog.csdn.net/qq_54705917/article/details/123484397?spm=1001.2014.3001.5502IDEA连接mysql数据库_跟着太阳.的博客-CSDN博客[这里是图片005]https://blog.csdn.net/qq_54705917/article/details/123484737?spm=1001.2014.3001.5502

    2.数据库设计

    代码:

    库:create databasestudent
    表:create table stu(
    stuId varchar(20),
    stuName varchar(20),
    stuSex varchar(20),
    stuAge varchar(20),
    stuJG varchar(20),
    stuLX varchar(20),
    stuBJ varchar(20)
    );

    3. 窗口及功能设计

    (1).主函数

    main.java

    import javax.swing.*;
    
    public class main {
        public static void main(String[] args) {
            JFrame jf = new StuLogin();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    (2).登录界面(默认的账号密码都为:admin)

    StuLogin.java

    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;
    
    public class StuLogin extends JFrame {
        private StuLogin self;
        private ImageIcon imageIcon;
        private Image image;
        private String userid;// 登陆用户名和密码
        private String password;
        private JLabel unLabel = new JLabel("账号:");// 登陆面板控件
        private JTextField unField = new JTextField();
        private JLabel pwLabel = new JLabel("密码:");
        private JPasswordField pwField = new JPasswordField();
        private JButton dl = new JButton("登录");
        private JButton d2 = new JButton("重置");
        public StuLogin() {
    
            this.self = this;
            this.setSize(350, 300);// 设置登陆面板
            设置窗口背景图
            //先将contentPane设置成透明的
            ((JPanel)getContentPane()).setOpaque(false);
            //再设置图片
            imageIcon = new ImageIcon("345.jpg");//图标组件
            image = imageIcon.getImage();
            JLabel imgLabel = new JLabel(imageIcon);
            getLayeredPane().add(imgLabel, new Integer(Integer.MIN_VALUE));
            imgLabel.setBounds(0,0,400,300); //背景图片的位置
    
            this.setIconImage(image);//设置窗口图像
            this.setLocation(600,300);
            this.setVisible(true);
            this.setResizable(false);
            this.setLayout(null);
    //      this.getContentPane().setBackground(Color.BLACK);设置窗口背景色;
    
            //设置窗口名称
            this.setTitle("学生信息管理系统");
            unLabel.setSize(50, 30);
            unLabel.setLocation(60, 40);
            unLabel.setForeground(Color.red);
            unLabel.setFont(new Font("楷体",Font.BOLD,15));
            unField.setSize(150, 35);
            unField.setLocation(110, 35);
            pwLabel.setSize(50, 30);
            pwLabel.setLocation(60, 100);
            pwLabel.setForeground(Color.red);
            pwLabel.setFont(new Font("楷体",Font.BOLD,15));
            pwField.setSize(150, 35);
            pwField.setLocation(110, 100);
            dl.setSize(80, 35);
            dl.setLocation(65, 175);
            dl.setBackground(Color.red);
            d2.setSize(80, 35);
            d2.setLocation(185, 175);
            d2.setBackground(Color.red);
            dl.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    userid = unField.getText();
                    password = pwField.getText();
                    if(userid.equals("admin")&&password.equals("admin")) {
                        self.setVisible(false);
    //                    JOptionPane.showMessageDialog(null, "登录成功", "登录情况",JOptionPane.PLAIN_MESSAGE);
                        new StuManager();
                    } else {
                        JOptionPane.showMessageDialog(null, "账号或密码错误!", "登录情况",JOptionPane.PLAIN_MESSAGE);
                    }
                }
            });
            d2.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    unField.setText("");
                    pwField.setText("");
                }
            });
            this.add(unLabel);
            this.add(unField);
            this.add(pwLabel);
            this.add(pwField);
            this.add(dl);
            this.add(d2);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88

    登录页面的背景图我会放到 上面分享的链接里

    (3).管理员界面(删除功能在这里面)

    StuManager.java

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.Statement;
    
    public class StuManager extends JFrame implements ActionListener {
        //定义一些控件
        private Object[] types = {"-请选择查询方式-", "按学号号查询", "姓名查询", "性别查询","按年龄查询", "按籍贯查询","按班级查询"};
        private JComboBox searchType = new JComboBox(types); //创建一个组合框用来选取查询不同的学生信息·
        JPanel jp1,jp2;
        JLabel jl1;
        JButton jb1,jb2,jb3,jb4;
        JTable jt;
        JScrollPane jsp;
        JTextField jtf1,jtf2;
        String strRS;
        StuModel sm;
        //定义连接数据库的变量
        PreparedStatement ps;
        Connection ct = null;
        ResultSet rs = null;
        //构造函数
        public StuManager(){
            jp1 = new JPanel();
            jp1.setBackground(Color.gray);
            jtf1 = new JTextField(15);
            jtf2 = new JTextField();
            jtf2.setEditable(false);
            jb1 = new JButton("查询");
            jb1.addActionListener(this);
            jl1 = new JLabel("总人数:");
            jp1.add(searchType);
            jp1.add(jtf1);
            jp1.add(jb1);
            jp1.add(jl1);
            jp1.add(jtf2);
            jb2 = new JButton("添加");
            jb2.setSize(100,500);
            jb2.addActionListener(this);
            jb3 = new JButton("修改");
            jb3.addActionListener(this);
            jb4 = new JButton("删除");
            jb4.addActionListener(this);
    
            jp2 = new JPanel();
            jp2.add(jb2);
            jp2.add(jb3);
            jp2.add(jb4);
            jp2.setBackground(Color.gray);
            //创建模型对象
            sm = new StuModel();
            //初始化总人数
            strRS=String.valueOf(sm.getRowCount());
            jtf2.setText(strRS);
            //初始化表和滚动面板
            jt = new JTable(sm);
            jsp = new JScrollPane(jt);
    
    
            //将jsp放入到jframe中
            this.add(jsp);
            this.add(jp1,BorderLayout.PAGE_START);
            this.add(jp2,BorderLayout.PAGE_END);
            this.setTitle("学生信息管理系统");
    //        this.pack();
            this.setSize(600, 400);
            this.setLocation(500, 200);
            this.setDefaultCloseOperation(EXIT_ON_CLOSE);
            this.setVisible(true);
    
        }
        public void actionPerformed(ActionEvent arg0) {
            //判断是哪个按钮被点击
            if(arg0.getSource() == jb1){
                System.out.println("用户希望被查询...");
                int index = searchType.getSelectedIndex();
                String sql = new String();
                if(index == 0){
                    sql = "select * from stu ";
                }
                else if(index == 1){
                    //因为把对表的数据封装到StuModel中,可以比较简单的完成查询
                    String Id =this.jtf1.getText().trim();
                    //写一个sql语句
                    sql = "select * from stu where stuId = '"+Id+"' ";
    
                }
                else if(index == 2){
                    String name =this.jtf1.getText().trim();
                    sql = "select * from stu where stuName = '"+name+"' ";
    
                }
                else if(index == 3){
                    String sex =this.jtf1.getText().trim();
                    sql = "select * from stu where stuSex = '"+sex+"' ";
    
                }
                else if(index == 4){
                    String age =this.jtf1.getText().trim();
                    sql = "select * from stu where stuAge = '"+age+"' ";
                }
                else if(index ==5){
                    String jg =this.jtf1.getText().trim();
                    sql = "select * from stu where stuJG= '"+jg+"' ";
    
                }
                else if(index ==6){
                    String bj =this.jtf1.getText().trim();
                    sql = "select * from stu where stuBJ= '"+bj+"' ";
    
                }
                //构建一个数据模型类,并更新
                sm = new StuModel(sql);
    
                strRS=String.valueOf(sm.getRowCount());
                jtf2.setText(strRS);
                //更新jtable
                jt.setModel(sm);
    
            }
    
            //一、弹出添加界面
            else if(arg0.getSource() == jb2){
                System.out.println("添加...");
                StuAddDiag sa = new StuAddDiag(this,"添加学生",true);
                //重新再获得新的数据模型,
                sm =  new StuModel();
                strRS=String.valueOf(sm.getRowCount());
                jtf2.setText(strRS);
                jt.setModel(sm);
            }else if(arg0.getSource() == jb4){
                //二、删除记录
                //1.得到学生的ID
                int rowNum = this.jt.getSelectedRow();//getSelectedRow会返回给用户点中的行
                //如果该用户一行都没有选,就返回-1
                if(rowNum == -1){
                    //提示
                    JOptionPane.showMessageDialog(this, "请选中一行");
                    return ;
                }
                //得到学术ID
                String stuId = (String)sm.getValueAt(rowNum, 0);
    
    
                //连接数据库,完成删除任务
                try{
                    //1.加载驱动
                    Class.forName("com.mysql.cj.jdbc.Driver");
                    //2.连接数据库
                    String url = "jdbc:mysql://localhost:3306/student";
                    String user = "root";
                    String passwd = "020334";
    
                    ct = DriverManager.getConnection(url, user, passwd);
    //                System.out.println("连接成功");
                    ps = ct.prepareStatement("delete from stu where stuId = ?");
                    ps.setString(1,stuId);
                    ps.executeUpdate();
                    JOptionPane.showMessageDialog(null, "删除成功", "删除情况",JOptionPane.PLAIN_MESSAGE);
                }catch(Exception e){
                    e.printStackTrace();
                }finally{
                    try{
                        if(rs!= null){
                            rs.close();
                            rs = null;
    
                        }
                        if(ps!= null){
                            ps.close();
                            ps = null;
                        }
                        if(ct != null){
                            ct.close();
                            ct = null;
                        }
                    } catch(Exception e){
                        e.printStackTrace();
                    }
                }
                sm = new StuModel();
                strRS=String.valueOf(sm.getRowCount());
                jtf2.setText(strRS);
                //更新jtable
                jt.setModel(sm);
            }else if(arg0.getSource() == jb3){
    //            System.out.println("11111");
                //三、用户希望修改
                int rowNum = this.jt.getSelectedRow();
                if(rowNum == -1){
                    //提示
                    JOptionPane.showMessageDialog(this, "请选择一行");
                    return ;
                }
                //显示对话框
    //            System.out.println( "12435");
                StuUpDiag su = new StuUpDiag(this, "修改学生信息", true, sm, rowNum);
                sm = new StuModel();
                jt.setModel(sm);
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206

    (4).模型界面

    StuModel.java

    /*
      用来刷新、呈现数据库
     * 这是我的一个stu表的模型
     * 可以把对学生表的操作全都封装到这个类
     */
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.Statement;
    import java.util.Vector;
    import javax.swing.table.*;
    
    public class StuModel extends AbstractTableModel{
    
        //rowData存放行数据,columnNames存放列名
        Vector rowData,columnNames;//Vector和ArrayList一样,底层也是一个Object类型的数组Object[]。;    构造一个空向量,使其内部数据数组的大小为10,其标准容量增量为零
    
        //定义连接数据库的变量
        Statement stat = null;
        Connection ct = null;
        ResultSet rs = null;
    
        //初始化
        public void init(String sql){
            if(sql.equals("")){
                sql = "select * from stu";
            }
            //中间
            //设置列名
            columnNames = new Vector();//这里是一维向量表示列;
            columnNames.add("学号");
            columnNames.add("名字");
            columnNames.add("性别");
            columnNames.add("年龄");
            columnNames.add("籍贯");
            columnNames.add("联系方式");
            columnNames.add("班级");
    
            //rowData存放多行
            rowData = new Vector();
            try{
                //1.加载驱动
                Class.forName("com.mysql.cj.jdbc.Driver");
                System.out.println("加载成功");
                //2.连接数据库
                //定义几个常量
                String url = "jdbc:mysql://localhost:3306/student";
                String user = "root";
                String passwd = "020334";//这里你要填写你自己的数据库密码
    
                ct = DriverManager.getConnection(url,user,passwd);
                stat = ct.createStatement();//创建stat对象
                rs = stat.executeQuery(sql);//查询结果
    
                while(rs.next()){
                    Vector hang = new Vector();
                    hang.add(rs.getString(1));
                    hang.add(rs.getString(2));
                    hang.add(rs.getString(3));
                    hang.add(rs.getString(4));
                    hang.add(rs.getString(5));
                    hang.add(rs.getString(6));
                    hang.add(rs.getString(7));
                    //加入到rowData中
                    rowData.add(hang);//这里是二维向量,表示行;
                }
    
            }catch(Exception e){
                e.printStackTrace();
            }finally{
                try{
                    if(rs!=null){
                        rs.close();
                        rs = null;
                    }
                    if(stat != null){
                        stat.close();
                        stat = null;
                    }
                    if(ct != null){
                        ct.close();
                        ct = null;
                    }
                }catch(Exception e){
                    e.printStackTrace();
                }
            }
        }
    
    
        //第二个构造函数,通过传递的sql语句来获得数据模型
        public StuModel(String sql){
            this.init(sql);
        }
    
        //构造函数,用于初始化我的数据模型(表)
        public StuModel(){
            this.init("");
        }
    
        //得到共有多少行
        public int getRowCount() {
            // TODO Auto-generated method stub
            return this.rowData.size();
        }
    
        //得到共有多少列
        public  int getColumnCount() {
            // TODO Auto-generated method stub
            return this.columnNames.size();
        }
    
        //得到某行某列的数据
        public Object getValueAt(int row, int column) {
            // TODO Auto-generated method stub
            return ((Vector)(this.rowData.get(row))).get(column);
        }
    
        //得到属性名字
        public String getColumnName(int column) {
            // TODO Auto-generated method stub
            return (String)this.columnNames.get(column);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124

    (5).增加学生界面

    StuAddDiag.java

    import javax.swing.JDialog;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.sql.Statement;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.*;
    //用来实现增添读者功能
    public class StuAddDiag extends JDialog implements ActionListener {
        //定义我需要的swing组件
        JLabel jl1,jl2,jl3,jl4,jl5,jl6,jl7;
        JTextField jf1,jf2,jf3,jf4,jf5,jf6,jf7;
        JPanel jp1,jp2,jp3;
        JButton jb1,jb2;
        //owner代笔父窗口,title是窗口的名字,modal指定是模式窗口()或者非模式窗口
        public StuAddDiag(Frame owner, String title, boolean modal){
            //调用父类方法
            super(owner,title,modal);
    
            jl1 = new JLabel("学号");
            jl2 = new JLabel("名字");
            jl3 = new JLabel("性别");
            jl4 = new JLabel("年龄");
            jl5 = new JLabel("籍贯");
            jl6 = new JLabel("联系方式");
            jl7 = new JLabel("班级");
    
            jf1 = new JTextField(30);
            jf2 = new JTextField(30);
            jf3 = new JTextField(30);
            jf4 = new JTextField(30);
            jf5 = new JTextField(30);
            jf6 = new JTextField(30);
            jf7 = new JTextField(30);
    
            jb1 = new JButton("添加");
            jb1.addActionListener(this::actionPerformed);
            jb2 = new JButton("取消");
            jb2.addActionListener(this::actionPerformed);
    
            jp1 = new JPanel();
            jp2 = new JPanel();
            jp3 = new JPanel();
    
            //设置布局
            jp1.setLayout(new GridLayout(7,1));
            jp2.setLayout(new GridLayout(7,1));
    
            jp3.add(jb1);
            jp3.add(jb2);
    
            jp1.add(jl1);
            jp1.add(jl2);
            jp1.add(jl3);
            jp1.add(jl4);
            jp1.add(jl5);
            jp1.add(jl6);
            jp1.add(jl7);
    
            jp2.add(jf1);
            jp2.add(jf2);
            jp2.add(jf3);
            jp2.add(jf4);
            jp2.add(jf5);
            jp2.add(jf6);
            jp2.add(jf7);
    
            this.add(jp1, BorderLayout.WEST);
            this.add(jp2, BorderLayout.CENTER);
            this.add(jp3, BorderLayout.SOUTH);
            this.setLocation(600, 350);
            this.setSize(300,200);
            this.setVisible(true);
        }
        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            if(e.getSource() == jb1){
                Connection ct = null;
                PreparedStatement pstmt = null;
                ResultSet rs = null;
    
                try{
                    //1.加载驱动
                    Class.forName("com.mysql.cj.jdbc.Driver");
                    System.out.println("加载成功");
                    //2.连接数据库
                    //定义几个常量
                    String url = "jdbc:mysql://localhost:3306/student";
                    String user = "root";
                    String passwd = "020334";
                    ct = DriverManager.getConnection(url,user,passwd);
    
                    //与编译语句对象
    
                    String strsql = "insert into stu values(?,?,?,?,?,?,?)";
                    pstmt = ct.prepareStatement(strsql);
    
                    //给对象赋值
                    pstmt.setString(1,jf1.getText());
                    pstmt.setString(2,jf2.getText());
                    pstmt.setString(3,jf3.getText());
                    pstmt.setString(4,jf4.getText());
                    pstmt.setString(5,jf5.getText());
                    pstmt.setString(6,jf6.getText());
                    pstmt.setString(7,jf7.getText());
    
                    pstmt.executeUpdate();
                    JOptionPane.showMessageDialog(null, "添加成功", "添加情况",-1);
                    this.dispose();//关闭学生对话框
    
                }catch(Exception arg1){
                    arg1.printStackTrace();
                }finally{
                    try{
                        if(rs!=null){
                            rs.close();
                            rs = null;
                        }
                        if(pstmt != null){
                            pstmt.close();
                            pstmt = null;
                        }
                        if(ct != null){
                            ct.close();
                            ct = null;
                        }
                    }catch(Exception arg2){
                        arg2.printStackTrace();
                    }
                }
    
            }else{
                this.dispose();
            }
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141

    (6).修改学生界面

    StuUpDiag.java

    import javax.swing.JDialog;
    import javax.swing.*;
    
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.sql.Statement;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.*;
    /*
    // * 是修改学生信息
    
     */
    public class StuUpDiag extends JDialog implements ActionListener {
        //定义我需要的swing组件
        JLabel jl1,jl2,jl3,jl4,jl5,jl6,jl7;
        JTextField jf1,jf2,jf3,jf4,jf5,jf6,jf7;
        JPanel jp1,jp2,jp3;
        JButton jb1,jb2;
        //owner代笔父窗口,title是窗口的名字,modal指定是模式窗口()或者非模式窗口
        public StuUpDiag(Frame owner, String title, boolean modal, StuModel sm, int rowNum){
            //调用父类方法
            super(owner,title,modal);
    
            jl1 = new JLabel("学号");
            jl2 = new JLabel("名字");
            jl3 = new JLabel("性别");
            jl4 = new JLabel("年龄");
            jl5 = new JLabel("籍贯");
            jl6 = new JLabel("联系方式");
            jl7 = new JLabel("班级");
    
    
    
            jf1 = new JTextField(30);
            jf1.setText((sm.getValueAt(rowNum, 0)).toString());
            jf2 = new JTextField(30);
            jf2.setText((String)sm.getValueAt(rowNum, 1));
            jf3 = new JTextField(30);
            jf3.setText(sm.getValueAt(rowNum, 2).toString());
            jf4 = new JTextField(30);
            jf4.setText((sm.getValueAt(rowNum, 3)).toString());
            jf5 = new JTextField(30);
            jf5.setText((String)sm.getValueAt(rowNum, 4));
            jf6 = new JTextField(30);
            jf6.setText((String)sm.getValueAt(rowNum, 5));
            jf7 = new JTextField(30);
            jf7.setText((String)sm.getValueAt(rowNum, 6));
    
            jb1 = new JButton("修改");
            jb1.addActionListener(this::actionPerformed);
            jb2 = new JButton("取消");
            jb2.addActionListener(this::actionPerformed);
    
            jp1 = new JPanel();
            jp2 = new JPanel();
            jp3 = new JPanel();
    
            //设置布局
            jp1.setLayout(new GridLayout(7,1));
            jp2.setLayout(new GridLayout(7,1));
    
            jp3.add(jb1);
            jp3.add(jb2);
    
            jp1.add(jl1);
            jp1.add(jl2);
            jp1.add(jl3);
            jp1.add(jl4);
            jp1.add(jl5);
            jp1.add(jl6);
            jp1.add(jl7);
    
            jp2.add(jf1);
            jp2.add(jf2);
            jp2.add(jf3);
            jp2.add(jf4);
            jp2.add(jf5);
            jp2.add(jf6);
            jp2.add(jf7);
    
            this.add(jp1, BorderLayout.WEST);
            this.add(jp2, BorderLayout.CENTER);
            this.add(jp3, BorderLayout.SOUTH);
            this.setLocation(600, 350);
            this.setSize(300,200);
            this.setVisible(true);
        }
        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            if(e.getSource() == jb1){
                Connection ct = null;
                PreparedStatement pstmt = null;
                ResultSet rs = null;
    
                try{
                    //1.加载驱动
                    Class.forName("com.mysql.cj.jdbc.Driver");
                    System.out.println("加载成功");
                    //2.连接数据库
                    //定义几个常量
                    String url = "jdbc:mysql://localhost:3306/student";
                    String user = "root";
                    String passwd = "020334";
                    ct = DriverManager.getConnection(url,user,passwd);
                    //与编译语句对象
                    String strsql = "update stu set stuName = '"+jf2.getText()+"',stuSex = '"+jf3.getText()+"',stuAge = '"+jf4.getText()+"',stuJG='"+jf5.getText()+"',stuLX='"+jf6.getText()+"',stuBJ='"+jf7.getText()+"' where stuId = '"+jf1.getText()+"'";
                    pstmt = ct.prepareStatement(strsql);
    
                    pstmt.executeUpdate();
                    JOptionPane.showMessageDialog(null, "修改成功", "修改情况",JOptionPane.PLAIN_MESSAGE);
                    this.dispose();//关闭学生对话框
    
                }catch(Exception arg1){
                    arg1.printStackTrace();
                }finally{
                    try{
                        if(rs!=null){
                            rs.close();
                            rs = null;
                        }
                        if(pstmt != null){
                            pstmt.close();
                            pstmt = null;
                        }
                        if(ct != null){
                            ct.close();
                            ct = null;
                        }
                    }catch(Exception arg2){
                        arg2.printStackTrace();
                    }
                }
    
            }else{
                this.dispose();//关闭学生对话框
            }
    
        }
    
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145

    先自我介绍一下,小编13年上师交大毕业,曾经在小公司待过,去过华为OPPO等大厂,18年进入阿里,直到现在。深知大多数初中级java工程师,想要升技能,往往是需要自己摸索成长或是报班学习,但对于培训机构动则近万元的学费,着实压力不小。自己不成体系的自学效率很低又漫长,而且容易碰到天花板技术停止不前。因此我收集了一份《java开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦

  • 相关阅读:
    【数据结构】交换排序
    回忆三年浮沉
    SparkSQL【核心编程、使用IDEA开发、用户自定义函数】
    实时 Path Tracing 实现
    React+antd实现可编辑单元格,非官网写法,不使用可编辑行和form验证
    Java类和对象:类是对象的模板,对象是类的实例化
    手写vue3.0 -项目结构初始化
    mac怎么把两张图片拼在一起
    强!推荐一款开源接口自动化测试平台:AutoMeter-API !
    PHP 变量学习资料
  • 原文地址:https://blog.csdn.net/m0_67390969/article/details/126066424