基于Java+MySQL+GUI的学生信息管理系统

源码编号:F-A01  

项目类型:Java SE项目(awt+swing)

用户类型:单角色(只有管理员)

主要技术:java、awt、swing等技术

开发工具:Eclipse

运行工具:Eclipse/MyEclipse/IDEA都可以,eclipse最兼容

数  据  库:MySQL5.7以上

数据库表数量:1张表

视图数量:5个左右

项目简介:本系统主要有对学生信息的增删改查操作功能,源码开源免费

项目内容

基于Java+MySQL+GUI的学生信息管理系统_第1张图片

项目骨架

基于Java+MySQL+GUI的学生信息管理系统_第2张图片

数据库表

1.主界面

基于Java+MySQL+GUI的学生信息管理系统_第3张图片

2.添加学生信息

基于Java+MySQL+GUI的学生信息管理系统_第4张图片

3.更改学生信息

基于Java+MySQL+GUI的学生信息管理系统_第5张图片

部分核心代码

package edu.ymkz.frame;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import javax.swing.Box;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.AbstractTableModel;

/**
 * @author Administrator 学生管理系统 实现: crud所有功能
 */

public class Demo{

    public static void main(String args[]) {
        // 创建一个面板对象
        MyJFrame mf = new MyJFrame();
    }
}

// 定义一个我自己的主界面
class MyJFrame extends JFrame implements ActionListener {

    JTable jt;
    MyJTable mjt;
    // 必要地分布板块
    JPanel jpup, jpdown, jpcenter;
    // 实现数据的 增删查改即crud操作
    JButton[] button = new JButton[4];
    // 用来辅助查找模块的框框
    JTextField text;

    JScrollPane jsp = null;

    /*
     * 构造函数 功能: 实现窗口的初始化
     */
    MyJFrame() {
        mjt = new MyJTable();
        jt = new JTable(mjt);
        // jt.setModel(mjt);
        jpup = new JPanel();
        jpdown = new JPanel();
        jpcenter = new JPanel();
        text = new JTextField(14);
        button[0] = new JButton(" 查询  ");
        button[1] = new JButton(" 添加  ");
        button[2] = new JButton(" 修改  ");
        button[3] = new JButton(" 删除  ");
        for (int i = 0; i < 4; i++)
            button[i].addActionListener(this);
        jpup.add(new JLabel("用户信息"));
        jpup.add(text);
        jpup.add(button[0]);
        for (int i = 1; i < 4; i++)
            jpdown.add(button[i]);
        jt.setBackground(Color.CYAN);
        jsp = new JScrollPane(jt);
        jpcenter.add(jsp);
        // 设置一些布局 borderLaRyou
        this.add(jpup, BorderLayout.NORTH);
        this.add(jpcenter, BorderLayout.CENTER);
        this.add(jpdown, BorderLayout.SOUTH);
        // 设置窗口的标题
        this.setTitle("学生信息管理系统");
        // 设置窗口的尺寸
        this.setSize(500, 300);
        // 窗口的尺寸不可改变
        // this.setResizable(false);
        // 设置窗口是否可见
        this.setVisible(true);
        // 设置窗口的注销模式
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        int i = 0;
        for (i = 0; i < 4; i++)
            if (e.getSource() == button[i])
                break;
        String cmd = text.getText().toString().trim();
        switch (i) {
        // 查询
        case 0:
            if (cmd.isEmpty()) {
                JOptionPane.showMessageDialog(button[0], "请输入用户名");
                mjt = new MyJTable();
            } else
                mjt = new MyJTable(cmd);
            jt.setModel(mjt);
            break;
        // 添加
        case 1:
            AddJFrame myAdd = new AddJFrame(this, true);
            mjt = new MyJTable();
            jt.setModel(mjt);
            break;
        // 修改
        case 2:
            // 需要得到我们点中的行列号
            int rownum = this.jt.getSelectedRow();
            if (-1 == rownum) {
                JOptionPane.showMessageDialog(this, "请选中你要修改人");
                return;
            } else {
                String name = (String) mjt.getValueAt(rownum, 0);
                UpdataJFrame Uj = new UpdataJFrame(this, true, name);
                mjt = new MyJTable();
                jt.setModel(mjt);
            }
            break;
        // 删除
        case 3:
            int row = this.jt.getSelectedRow();
            if (-1 == row) {
                JOptionPane.showMessageDialog(this, "请选中你要修改人");
                return;
            } else {
                String name = (String) mjt.getValueAt(row, 1);
                Delete det = new Delete(this, name.trim(), true);
            }
            mjt = new MyJTable();
            jt.setModel(mjt);
            break;
        // 啥也不干
        default:

            break;
        }
    }

}

// 定义一个自己的table类
class MyJTable extends AbstractTableModel {

    // sql的几个变量
    Connection ct = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    Vector rowdata, colName;

    // 非带参数的构造函数,用于初始化
    MyJTable() {
        this.func("", false);
    }

    // 带参数的构造函数,用于查询
    MyJTable(String name) {
        this.func(name, true);
    };

    public void func(String name, boolean tag) {
        String sql;
        try {
            // 加载上jdbc_odbc驱动
            Class.forName("com.mysql.jdbc.Driver");
            // 连接sql
            ct = DriverManager.getConnection(
                            "jdbc:mysql://localhost:3306/db_stu?useUnicode=true&characterEncoding=utf-8&useSSL=false",
                            "root", "123456");
            if (!tag) {
                sql = new String("select * from stu");
                ps = ct.prepareStatement(sql);
            } else {
                sql = new String("Select * from stu where Stuname=?");
                ps = ct.prepareStatement(sql);
                ps.setString(1, name);
            }
            rs = ps.executeQuery();
            /*
             * 设置表单的属性
             */
            colName = new Vector();
            rowdata = new Vector();
            String[] ss = { "学号", "名字", "性别", "年龄", "家庭地址", "系别" };
            for (int i = 0; i < 6; i++)
                colName.add(ss[i]);
            while (rs.next()) {
                Vector hang = new Vector();
                for (int i = 1; i <= 6; i++) {
                    if (4 == i)
                        hang.add(rs.getInt(i));
                    else {
                        String st = new String(
                                (rs.getString(i)).getBytes("gbk"), "gb2312");
                        hang.add(st);
                    }
                }
                rowdata.add(hang);
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            // 关闭一些窗口
            try {
                if (rs != null)
                    rs.close();
                if (ps != null)
                    ps.close();
                if (ct != null)
                    ct.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    // 返回它的行数
    @Override
    public int getRowCount() {
        // TODO Auto-generated method stub
        return this.rowdata.size();
    }

    // 返回它的列数
    @Override
    public int getColumnCount() {
        // TODO Auto-generated method stub
        return this.colName.size();
    }

    // 返回该表单的内容
    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        // TODO Auto-generated method stub
        return ((Vector) this.rowdata.get(rowIndex)).get(columnIndex);
    }

    @Override
    public String getColumnName(int column) {
        // TODO Auto-generated method stub
        return (String) this.colName.get(column);
    }
}

// 定义一个添加数据的界面
class AddJFrame extends JDialog implements ActionListener {

    // 填写信息的空格
    private String sex = null;
    JTextField[] jt = new JTextField[5];
    JButton[] jb = new JButton[2];
    // 单选按钮
    JRadioButton[] jradio = new JRadioButton[2];
    // 组合单选按钮
    ButtonGroup group;
    // 用于设置 盒式布局 BoxLayout
    Box[] mybox = new Box[4];
    JPanel jp, jp1;
    // SQL几个常用的变量类型
    Connection ct = null;
    PreparedStatement ps = null;

    public AddJFrame(Frame Father, boolean Model) {
        // 采用模式对话框
        super(Father, Model);
        for (int i = 0; i < 5; i++)
            jt[i] = new JTextField(10);
        jb[0] = new JButton("确认");
        jb[0].addActionListener(this);
        jb[1] = new JButton("取消");
        jb[1].addActionListener(this);
        jradio[0] = new JRadioButton("男");
        jradio[0].addActionListener(this);
        jradio[1] = new JRadioButton("女");
        jradio[1].addActionListener(this);
        // 设置水平
        mybox[0] = Box.createVerticalBox();
        mybox[0].add(Box.createVerticalStrut(15));
        mybox[0].add(new JLabel("学号:"));
        mybox[0].add(Box.createVerticalStrut(10));
        mybox[0].add(new JLabel("名字:"));
        mybox[0].add(Box.createVerticalStrut(20));
        mybox[0].add(new JLabel("性别:"));
        mybox[0].add(Box.createVerticalStrut(25));
        mybox[0].add(new JLabel("年龄:"));
        mybox[0].add(Box.createVerticalStrut(10));
        mybox[0].add(new JLabel("家庭住址:"));
        mybox[0].add(Box.createVerticalStrut(10));
        mybox[0].add(new JLabel("系别:"));
        mybox[0].add(Box.createVerticalStrut(10));
        // 设置另一边的水平
        mybox[1] = Box.createVerticalBox();
        mybox[1].add(Box.createVerticalStrut(18));
        mybox[1].add(jt[0]);
        mybox[1].add(Box.createVerticalStrut(8));
        mybox[1].add(jt[1]);
        mybox[1].add(Box.createVerticalStrut(8));
        jp = new JPanel();
        // 单选群
        group = new ButtonGroup();
        group.add(jradio[0]);
        group.add(jradio[1]);
        jp.add(jradio[0]);
        jp.add(jradio[1]);
        mybox[1].add(jp);
       
        mybox[1].add(Box.createVerticalStrut(5));
        mybox[1].add(jt[2]);
        mybox[1].add(Box.createVerticalStrut(8));
        mybox[1].add(jt[3]);
        mybox[1].add(Box.createVerticalStrut(8));
        mybox[1].add(jt[4]);
        mybox[1].add(Box.createVerticalStrut(8));
        // 按钮部分
        jp1 = new JPanel();
        jp1.add(jb[0], BorderLayout.EAST);
        jp1.add(jb[1], BorderLayout.WEST);
        mybox[2] = Box.createHorizontalBox();
        mybox[2].add(mybox[0]);
        mybox[2].add(Box.createHorizontalStrut(10));
        mybox[2].add(mybox[1]);
        this.add(mybox[2], BorderLayout.NORTH);
        this.add(jp1, BorderLayout.SOUTH);
        init();
    }

    // 显示板块部分
    public void init() {
        this.setTitle("添加学生信息");
        this.setBackground(Color.magenta);
        this.setBounds(400, 300, 300, 300);
        this.setVisible(true);
        // this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        // 将数据写入数据库
        if (e.getSource() == jb[0]) {
            // 对写入的数据进行处理
            try {

                Class.forName("com.mysql.jdbc.Driver");
                ct = DriverManager
                        .getConnection(
                                "jdbc:mysql://localhost:3306/db_stu?useUnicode=true&characterEncoding=utf-8&useSSL=false",
                                "root", "123456");
                ps = ct.prepareStatement("insert into stu values(?,?,?,?,?,?)");
                for (int i = 1, j = 0; i <= 6; i++) {
                    if (3 == i) {
                        // 去掉多余的空格,但是这里存在一个小bug(合理的处理为正则表达式)
                        ps.setString(i, this.getSex().trim());
                        // ps.setInt(i,);
                    } else if (4 == i) {
                        String tem = jt[j].getText().toString().trim();
                        int value = Integer.valueOf(tem).intValue();
                        ps.setInt(i, value);
                        ++j;
                    } else {
                        ps.setString(i, jt[j].getText().toString().trim());
                        ++j;
                    }
                }
                int i = ps.executeUpdate();

                if (1 == i)
                    JOptionPane.showMessageDialog(this, "添加成功!");
                else
                    JOptionPane.showMessageDialog(this, "添加失败!");

            } catch (Exception e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } finally {
                // 关闭数据库的一些调用函数
                try {
                    if (ps != null)
                        ps.close();
                    if (ct != null)
                        ct.close();
                } catch (SQLException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
            this.dispose();
        } else if (e.getSource() == jb[0])
            // 释放点该窗口, 退出该版面
            this.dispose();
        else if (jradio[0].isSelected()) {
            // 将名字改为男
            this.setSex("男");
        } else if (jradio[1].isSelected()) {
            // 将名字改为女
            this.setSex("女");
        }
    }

    public String getSex() {
        return this.sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

}

// 定义一个修改数据的界面
class UpdataJFrame extends JDialog implements ActionListener {

    // 填写信息的空格
    private String sex = null;
    JTextField[] jt = new JTextField[5];
    JButton[] jb = new JButton[2];
    // 单选按钮
    JRadioButton[] jradio = new JRadioButton[2];
    // 组合单选按钮
    ButtonGroup group;
    // 用于设置 盒式布局 BoxLayout
    Box[] mybox = new Box[4];
    JPanel jp, jp1;
    // SQL几个常用的变量类型
    Connection ct = null;
    PreparedStatement ps = null;

    public UpdataJFrame(Frame Father, boolean Model, String name) {
        // 采用模式对话框
        super(Father, Model);
        for (int i = 0; i < 5; i++)
            jt[i] = new JTextField(10);
        // 设置为仅仅读取
        jt[0].setEditable(false);
        // 下方的几个按钮
        jb[0] = new JButton("确认");
        jb[0].addActionListener(this);
        jb[1] = new JButton("取消");
        jb[1].addActionListener(this);
        jradio[0] = new JRadioButton("男");
        jradio[0].addActionListener(this);
        jradio[1] = new JRadioButton("女");
        jradio[1].addActionListener(this);
        // 设置水平
        mybox[0] = Box.createVerticalBox();
        mybox[0].add(Box.createVerticalStrut(15));
        mybox[0].add(new JLabel("学号:"));
        mybox[0].add(Box.createVerticalStrut(10));
        mybox[0].add(new JLabel("名字:"));
        mybox[0].add(Box.createVerticalStrut(20));
        mybox[0].add(new JLabel("性别:"));
        mybox[0].add(Box.createVerticalStrut(25));
        mybox[0].add(new JLabel("年龄:"));
        mybox[0].add(Box.createVerticalStrut(10));
        mybox[0].add(new JLabel("家庭住址:"));
        mybox[0].add(Box.createVerticalStrut(10));
        mybox[0].add(new JLabel("系别:"));
        mybox[0].add(Box.createVerticalStrut(10));
        // 设置另一边的水平
        mybox[1] = Box.createVerticalBox();
        mybox[1].add(Box.createVerticalStrut(18));
        mybox[1].add(jt[0]);
        mybox[1].add(Box.createVerticalStrut(8));
        mybox[1].add(jt[1]);
        mybox[1].add(Box.createVerticalStrut(8));
        jp = new JPanel();
        // 单选群
        group = new ButtonGroup();
        group.add(jradio[0]);
        group.add(jradio[1]);
        jp.add(jradio[0]);
        jp.add(jradio[1]);
        mybox[1].add(jp);

        /*
         * group =new ButtonGroup(); group.add(jradio[0]); group.add(jradio[1]);
         * mybox[1].add(group); 此处需要改变的 东西,改成combox来进行,或者直接改成一样的文本框也许
         */
        mybox[1].add(Box.createVerticalStrut(5));
        mybox[1].add(jt[2]);
        mybox[1].add(Box.createVerticalStrut(8));
        mybox[1].add(jt[3]);
        mybox[1].add(Box.createVerticalStrut(8));
        mybox[1].add(jt[4]);
        mybox[1].add(Box.createVerticalStrut(8));

        // 按钮部分
        jp1 = new JPanel();
        jp1.add(jb[0], BorderLayout.EAST);
        jp1.add(jb[1], BorderLayout.WEST);
        mybox[2] = Box.createHorizontalBox();
        mybox[2].add(mybox[0]);
        mybox[2].add(Box.createHorizontalStrut(10));
        mybox[2].add(mybox[1]);
        Connect(name);
        this.add(mybox[2], BorderLayout.NORTH);
        this.add(jp1, BorderLayout.SOUTH);
        init();
    }

    // 显示板块部分
    public void init() {
        this.setTitle("修改学生信息");
        this.setBackground(Color.magenta);
        this.setBounds(400, 300, 300, 300);
        this.setVisible(true);
        // this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public void Connect(String name) {
        ResultSet rs = null;
        // 驱动加载
        try {
            Class.forName("com.mysql.jdbc.Driver");
            ct = DriverManager
                    .getConnection(
                            "jdbc:mysql://localhost:3306/db_stu?useUnicode=true&characterEncoding=utf-8&useSSL=false",
                            "root", "123456");
            String sql = new String("Select * from Stu where stuId=?");
            ps = ct.prepareStatement(sql);
            ps.setString(1, name);
            rs = ps.executeQuery();
            while (rs.next()) {
                int cnt = 1;
                for (int i = 1; i <= 6; i++) {
                    if (4 == i) {
                        // 整数转化为String
                        jt[i - cnt].setText(String.valueOf(rs.getInt(i)));
                    } else {
                        String st = null;
                        try {
                            st = new String((rs.getString(i)).getBytes("gbk"),
                                    "gb2312");
                        } catch (UnsupportedEncodingException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        if (i == 3) {
                            if (st.equals("男")) {
                                jradio[0].setSelected(true);
                                this.setSex("男");
                            } else if (st.equals("女")) {
                                jradio[1].setSelected(true);
                                this.setSex("女");
                            } else
                                JOptionPane.showMessageDialog(this, "性别处出现乱码");
                            cnt++;
                        } else
                            jt[i - cnt].setText(st);
                    }
                }

            }

        } catch (ClassNotFoundException | SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                if (rs != null)
                    rs.close();
                if (ps != null)
                    ps.close();
                if (ct != null)
                    ct.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        // 将数据写入数据库
        if (e.getSource() == jb[0]) {
            // 对写入的数据进行处理
            try {
                // 驱动加载
                Class.forName("com.mysql.jdbc.Driver");
                ct = DriverManager
                        .getConnection(
                                "jdbc:mysql://localhost:3306/db_stu?useUnicode=true&characterEncoding=utf-8&useSSL=false",
                                "root", "123456");
                ps = ct.prepareStatement("Update stu Set Stuname=?,StuSex=?,StuAge=?,StuJg=?,StuDept=? where StuId=?");
                // 回来修改........
                // 名字
                ps.setString(1, jt[1].getText().toString().trim());
                // 性别
                ps.setString(2, this.getSex().trim());
                // 年龄
                String tem = jt[2].getText().toString().trim();
                int value = Integer.valueOf(tem).intValue();
                ps.setInt(3, value);
                // 籍贯
                ps.setString(4, jt[3].getText().toString().trim());
                // 系别
                ps.setString(5, jt[4].getText().toString().trim());
                // 学号
                ps.setString(6, jt[0].getText().toString().trim());
                // 更新
                int i = ps.executeUpdate();
                if (1 == i)
                    JOptionPane.showMessageDialog(this, "添加成功!");
                else
                    JOptionPane.showMessageDialog(this, "添加失败!");

            } catch (Exception e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } finally {
                // 关闭数据库的一些调用函数
                try {
                    if (ps != null)
                        ps.close();
                    if (ct != null)
                        ct.close();
                } catch (SQLException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
            this.dispose();
        } else if (e.getSource() == jb[0])
            // 释放点该窗口, 退出该版面
            this.dispose();
        else if (jradio[0].isSelected()) {
            // 将名字改为男
            this.setSex("男");
        } else if (jradio[1].isSelected()) {
            // 将名字改为女
            this.setSex("女");
        }
    }

    public String getSex() {
        return this.sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }
}

// 删除界面类
class Delete extends JDialog implements ActionListener {

    // 设置为确认和取消两个按钮
    JButton[] button = new JButton[2];
    JPanel jp;
    JTable jt;
    JScrollPane jsp = null;
    // 设置为一个删除的表单
    MyJTable mytable;
    // SQL几个常用的变量类型

    Connection ct = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    private String name;

    public Delete(Frame ower, String name, boolean Model) {

        super(ower, Model);
        this.setName(name);
        jp = new JPanel();
        button[0] = new JButton("确定");
        button[0].addActionListener(this);
        button[1] = new JButton("取消");
        button[1].addActionListener(this);
        jp.add(button[0]);
        jp.add(button[1]);

        // 设置一个我的列表的界面
        mytable = new MyJTable(name);
        jt = new JTable(mytable);
        JScrollPane jsp = new JScrollPane(jt);
        this.add(jp, BorderLayout.SOUTH);
        this.add(jsp, BorderLayout.NORTH);
        this.setTitle("删除界面");
        this.setSize(400, 250);
        this.setResizable(false);
        this.setVisible(true);

    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        if (e.getSource() == button[0]) {

            try {
                // 加载上jdbc_odbc驱动
                Class.forName("com.mysql.jdbc.Driver");
                // 连接sql
                ct = DriverManager
                        .getConnection(
                                "jdbc:mysql://localhost:3306/db_stu?useUnicode=true&characterEncoding=utf-8&useSSL=false",
                                "root", "123456");
                String sql = new String("delete  from Stu where Stuname=?");
                ps = ct.prepareStatement(sql);
                ps.setString(1, this.getName().trim());

                int i = ps.executeUpdate();
                if (1 == i)
                    JOptionPane.showMessageDialog(this, "删除成功");
                else
                    JOptionPane.showMessageDialog(this, "删除失败了啦!");

            } catch (ClassNotFoundException | SQLException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } finally {
                try {
                    if (ps != null)
                        ps.close();
                    if (ct != null)
                        ct.close();
                } catch (SQLException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
        }

        this.dispose();
    }
}

你可能感兴趣的:(GUI+Swing,java,myeclipse,eclipse)