基于Java+MySQL+swing的一个学生信息管理系统,登录分为教师端登录和学生端登录

教师端:可以查看所以学生的信息,并能对学生信息进行增删改查。

学生端:只能查看自己的信息

开发环境:IDEA+jdk11+mysql

项目代码文件:链接:https://pan.baidu.com/s/1ItGqKIr0AdEa8NKY1JDbzA 
                     提取码:5uhu  (之前不小心将百度网盘的文件清理了,已经重新上传了)

创建项目的时候选择创建Maven项目,如图,直接点击下一步,

 这里注意pom.xml文件里一定要去导MySQL的依赖,不然代码运只要需要对数据库进行操作会显示线程异常。需要的话自行去Maven仓库里去找,添加到pom.xml后记得还要去点一下右上角的那个圈圈,加载Maven变更

dao包对数据库的具体操作,完成对数据库信息的增删改查

db包链接数据库

view视图包

(mysql里放的是xsgl数据库的sql语句)

1.登录

登录的话,会判断账号密码及验证码是否为空和是否正确,判断顺序依次为先判断验证码是否正确,然后在判断账号和密码是否正确,如果不正确的话会弹框提示,并刷新界面。

登录界面代码(LoginView.java)

package com.zj.java.view;
import com.zj.java.dao.UsersDAO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;

public class LoginView extends JFrame {
    private JLabel systemLab, nameLab, passLab, privilegesLab, checkLab, imgLab, messageLab;
    private JTextField nameVal, checkVal;
    private JPasswordField passVal;
    private JButton button1, button2;
    private JRadioButton jrb1, jrb2;
    private ButtonGroup bg;
    private String yzm = "";
    private UsersDAO udao = new UsersDAO();

    public LoginView() throws HeadlessException {
        setTitle("登录页面");
        // 设置自定义布局方式
        setLayout(null);
        // 产生相应的控件
        systemLab = new JLabel("学 生 管 理 系 统");
        nameLab = new JLabel("账号:");
        passLab = new JLabel("密码:");
        privilegesLab = new JLabel("权限:");
        checkLab = new JLabel("验证码:");
        nameVal = new JTextField();
        passVal = new JPasswordField();
        jrb1 = new JRadioButton("教师");
        jrb2 = new JRadioButton("学生");
        bg = new ButtonGroup();
        bg.add(jrb1);
        bg.add(jrb2);
        jrb2.setSelected(true);

        checkVal = new JTextField();
        imgLab = new JLabel();
        button1 = new JButton("登  录");
        button2 = new JButton("重  置");
        messageLab = new JLabel("");
        // 设置验证码的图片
        Icon icon = new ImageIcon(getImage(60, 20));
        imgLab.setIcon(icon);
        // 设置控件在窗体中的位置
        systemLab.setBounds(150, 10, 100, 20);
        nameLab.setBounds(100, 60, 50, 20);
        passLab.setBounds(100, 100, 50, 20);
        privilegesLab.setBounds(100, 140, 50, 20);
        jrb1.setBounds(150, 140, 60, 20);
        jrb2.setBounds(210, 140, 60, 20);
        checkLab.setBounds(100, 180, 50, 20);
        nameVal.setBounds(150, 60, 120, 20);
        passVal.setBounds(150, 100, 120, 20);
        checkVal.setBounds(150, 180, 60, 20);
        imgLab.setBounds(210, 180, 60, 20);
        button1.setBounds(100, 250, 80, 20);
        button2.setBounds(200, 250, 80, 20);
        // 给控件添加事件
        imgLab.addMouseListener(new FlushCheckImg());
        // button1.addActionListener(new Login());
        // 加入控件
        add(systemLab);
        add(nameLab);
        add(passLab);
        add(privilegesLab);
        add(jrb1);
        add(jrb2);
        add(checkLab);
        add(nameVal);
        add(passVal);
        add(checkVal);
        add(imgLab);
        add(button1);
        add(button2);
        add(messageLab);

        setBounds(0, 3, 400, 400);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setVisible(true);
        setBackground(Color.pink);

        //为按钮添加监听事件
        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String yzm1 = checkVal.getText();
                //如果验证码正确
                if (yzm1.equalsIgnoreCase(yzm)) {

                    //如果选中教师登录
                    if (jrb1.isSelected()) {
                        tealogin();
                    } else if (jrb2.isSelected()) //学生在登录系统
                    {
                        stulogin();
                    }
                }else{
                    clear();
                    JOptionPane.showMessageDialog(null,"验证码错误!\n请重新登录","提示消息",JOptionPane.ERROR_MESSAGE);
                }
            }
        });
        button2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //弹出对话
                clear();
            }
        });
    }
//学生登录
    public void stulogin () {
        String name = nameVal.getText();
        String pass = passVal.getText();
        boolean b = udao.checkStuLogin(name, pass);
        if (b) {
            // 成功
            new StudentMainView();
            dispose();
        } else {
            JOptionPane.showMessageDialog(null, "账号或者密码错误!\n请重新输入", "提示消息", JOptionPane.ERROR_MESSAGE);
            clear();
        }
    }
      //老师登录
        public void tealogin () {
            String name = nameVal.getText();
            String pass = passVal.getText();
            boolean b = udao.checkTeaLogin(name, pass);
            if (b) {
                // 成功
               new TeacherMainView();
               dispose();
            } else {
                JOptionPane.showMessageDialog(null, "账号或者密码错误!\n请重新输入", "提示消息", JOptionPane.ERROR_MESSAGE);
                clear();
            }
        }
        // 书写一个类实现点击验证码图片刷新操作
        private class FlushCheckImg implements MouseListener {
            @Override
            public void mouseClicked(MouseEvent e) {
                Icon icon = new ImageIcon(getImage(60, 20));
                imgLab.setIcon(icon);
            }

            @Override
            public void mousePressed(MouseEvent e) {
            }
            @Override
            public void mouseReleased(MouseEvent e) {
            }
            @Override
            public void mouseEntered(MouseEvent e) {
            }
            @Override
            public void mouseExited(MouseEvent e) {

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


        private Image getImage ( int width, int height){
            yzm = "";
            BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            // 创建一支笔
            Graphics g = image.getGraphics();
            // 填充背景
            g.setColor(Color.gray);
            g.fillRect(0, 0, width, height);
            // 书写文字
            for (int i = 0; i < 4; i++) {
                char c = (char) (Math.random() * 26 + 65);
                yzm += c;
            }
            int red = (int) (Math.random() * 256);
            int green = (int) (Math.random() * 256);
            int blue = (int) (Math.random() * 256);
            Color strColor = new Color(red, green, blue);
            g.setColor(strColor); //蘸水
            g.drawString(yzm, 12, 13);
            // 干扰点
       for (int i = 0; i < 4; i++) {
            int red1 = (int) (Math.random() * 256);
            int green1 = (int) (Math.random() * 256);
            int blue1 = (int) (Math.random() * 256);
            Color ganrao = new Color(red1, green1, blue1);
            int x1 = (int)(Math.random()*60);
            int x2 = (int)(Math.random()*60);
            int y1 = (int)(Math.random()*20);
            int y2 = (int)(Math.random()*20);
            g.setColor(ganrao);
            g.drawLine(x1,y1,x2,y2);
        }
            return image;
        }
        public void clear ()
        {
            Icon icon = new ImageIcon(getImage(60, 20));
            imgLab.setIcon(icon);
            nameVal.setText("");
            checkVal.setText("");
            passVal.setText("");

        }


    }

学生端登录:账号:123456,密码:a123456

教师端登录:账号:654321,密码:a654321

账号和密码是放在数据库里的,可以通过对xsgl这个数据库里的tuser(教师端)和tuser(学生端)这两个表的数据修改实现修改登录账号和密码。

登录验证代码(UsersDAO.java)

package com.zj.java.dao;
import com.zj.java.db.DBManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class UsersDAO {

    //学生登录
    public boolean checkStuLogin(String name, String pass) {
        // 获取到了链接
        Connection conn = DBManager.getConnection();
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            String sql = "select * from Suser where username=? and userpass=?";
            // 获取到传令官
            ps = conn.prepareStatement(sql);
            // 给?号赋值
            ps.setString(1, name);
            ps.setString(2, pass);
            // 执行查询操作
            rs = ps.executeQuery();
            if (rs.next()) {

                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBManager.close(conn, ps, rs);
        }
        return false;

    }


    //老师登录
    public boolean checkTeaLogin(String name, String pass) {
        // 获取到了链接
        Connection conn = DBManager.getConnection();
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            String sql = "select * from Tuser where username=? and userpass=?";
            // 获取到传令官
            ps = conn.prepareStatement(sql);
            // 给?号赋值
          ps.setString(1, name);
            ps.setString(2, pass);
            // 执行查询操作
            rs = ps.executeQuery();
            if (rs.next()) {


                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBManager.close(conn, ps, rs);
        }
        return false;
    }
}

教师端登录

 可以根据数据库学生数据的数量进行分页,我这里设置的是每页显示25条信息。

教师端界面(TeacherMainView.java)

package com.zj.java.view;

import com.zj.java.dao.StudentDAO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TeacherMainView extends JFrame {
    private final JPanel top, bottom;
    private final JScrollPane jsp;
    private final JButton addButton, updateButton, removeButton;
    private JTable table;
    private MenuBar bar;
    private Menu m1;
    private MenuItem mi1, mi2;
    private final StudentDAO sdao = new StudentDAO();
    private String[][] datas;
    // 分页的按钮们
    private final JButton[] pageButtons;
    public TeacherMainView() {
        setTitle("教师端页面");
        top = new JPanel();
        top.setLayout(new FlowLayout(FlowLayout.RIGHT));
        addButton = new JButton("学生注册");
        updateButton = new JButton("信息修改");
        removeButton = new JButton("删除学生");
        bar = new MenuBar();
        m1 = new Menu("查询");
        mi1 = new MenuItem("学号查询");
        mi2 = new MenuItem("姓名查询");
        datas = sdao.page(1);
        table = new JTable(datas, getTitles());
        jsp = new JScrollPane(table);// 存放到了面板中
        bottom = new JPanel();
        // 添加到窗体
        bar.add(m1);
        m1.add(mi1);
        m1.add(mi2);
        setMenuBar(bar);
        top.add(addButton);
        top.add(updateButton);
        top.add(removeButton);
        add(top, BorderLayout.NORTH);
        add(jsp);
        add(bottom, BorderLayout.SOUTH);
        // 负责页码显式的区域
        int allCount = sdao.findAllCount();
        int size = 25;
        int allPageNum ;
        if(allCount%size==0){
            allPageNum = allCount / size;
        }else{
            allPageNum = allCount / size + 1; // 8/3=2
        }
        // 确定了分页按钮个数
        pageButtons = new JButton[allPageNum];
        // 产生这些按钮
        for (int i = 0; i < allPageNum; i++) {
            JButton p = new JButton(i+1+"");
            // 放入到bottom的面板
            bottom.add(p);
            // 放入到数组中
            pageButtons[i] = p;
            // 给每个按钮添加了事件
            p.addActionListener(new PageNumber(i+1));
        }
        // 绑定事件
        addButton.addActionListener(new Save());
        removeButton.addActionListener(new Remove());
        updateButton.addActionListener(new Update());

        mi1.addActionListener(new IdSearch());
        mi2.addActionListener(new NameSearch());
          
        setBounds(0, 3, 800, 600);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setVisible(true);
    }
    private String[] getTitles() {
        String[] titles = {"学号","姓名","性别","年龄","专业","入学时间","JAVA成绩","数学成绩","英语成绩","总成绩","平均成绩"};
        return titles;
    }

    //查询
    private class IdSearch implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {

            new com.zj.java.view. IdSearch();

        }
    }
    private class  NameSearch implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {

            new com.zj.java.view. NameSearch();
        }
    }
    //新增
    private class Save implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            // 弹出一个注册窗体
            new SaveView(TeacherMainView.this, table, jsp);
            // 刷新table
            Refresh();
        }
    }
  //分页
    private class PageNumber implements ActionListener{
        private final int page;
        public PageNumber(int page){

            this.page = page;
        }
        @Override
        public void actionPerformed(ActionEvent e) {
            //System.out.println("显式的页数:" + page);
            // 刷新table
            datas = sdao.page(page); // 重新查询
            table = new JTable(datas, getTitles());
            jsp.setViewportView(table); // 刷新面板
        }
    }

    private class Update implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            int rowNum = table.getSelectedRow();
            //修改
            // 获取到用户选择的数据
            if (rowNum > -1) {
                String id = datas[rowNum][0];
                String name = datas[rowNum][1];
                String sex = datas[rowNum][2];
                String age = datas[rowNum][3];
                String subject = datas[rowNum][4];
                String grade1 = datas[rowNum][6];
                String grade2 = datas[rowNum][7];
                String grade3 = datas[rowNum][8];
                new UpdateView(TeacherMainView.this, table, jsp,id, name, sex ,age,subject, grade1, grade2,grade3);
                // 刷新table
                Refresh();
            }
        }
    }
   //删除
    private class Remove implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            // 获取到用户选择的数据
            int rowNum = table.getSelectedRow();
            if (rowNum > -1) {
                // 询问框
                int result = JOptionPane.showConfirmDialog(TeacherMainView.this, "你确定要删除" + datas[rowNum][1] + "么?");
                if (result == 0) {
                    // 获取到学号 因为学号在第一列
                    String id = datas[rowNum][0];
                    // 调用删除方法执行删除操作
                    sdao.delete(id);
                    Refresh();
                }
            }
        }
    }
    public  void Refresh(){
        // 刷新table
        datas = sdao.page(1); // 重新查询
        table = new JTable(datas, getTitles());
        jsp.setViewportView(table); // 刷新面板
    }
    public static void main(String[] args) {
        new TeacherMainView();
    }
}

查看:查看分为学号查询和姓名查询

  • 学号查询结果唯一,要么这个学生存在,则显示该学生的信息,如果不存在则弹窗提示。
  • 姓名查询结果不唯一,如果有多个学生重名则会显示多个学生信息,如果没有该学生,则弹窗提示

查询不到:

 学号查询唯一(为了方便看出效果,我没有关闭上一个界面界面,弹出的新界面也是可以移动)

学号查询(IdSearch.java)

package com.zj.java.view;

import com.zj.java.dao.StudentDAO;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class IdSearch extends JFrame {
    private JLabel jl1;
    private JTextField jt1;
    private JButton button;
    private JScrollPane jsp;
    private JTable table;
    private StudentDAO sdao;
    public IdSearch() {
        jl1 = new JLabel("请输入想查询的学生学号");
        jt1 = new JTextField();
        button = new JButton("查询");

        jl1.setBounds(150, 50, 150, 20);
        jt1.setBounds(310, 50, 150, 20);
        button.setBounds(460, 50, 70, 20);
        add(jl1);
        add(jt1);
        add(button);
        //查询按钮添加监听事件
        button.addActionListener(new Search());

        setLayout(null);
        setSize( 800, 300);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setVisible(true);
    }
    public class Search implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            sdao = new StudentDAO();
            String id=jt1.getText();
            int num=0;
            if(id.equals("")){
                JOptionPane.showMessageDialog(null,"学号不能为空!\n请重新输入","提示消息",JOptionPane.ERROR_MESSAGE);
                return;
            }else{
                try {
                    num = Integer.parseInt(jt1.getText());
                } catch (NumberFormatException ex) {
                    JOptionPane.showMessageDialog(null,"学号错误!\n(六位整数)请重新输入","提示消息",JOptionPane.ERROR_MESSAGE);
                    jt1.setText("");
                    return;
                }
            }
            if(num<100000||num>=1000000){
                JOptionPane.showMessageDialog(null,"学号错误!\n(学号六位)请重新输入","提示消息",JOptionPane.ERROR_MESSAGE);
                jt1.setText("");
                return;
            }
            //判断数据库里能否查到此学号
            boolean flag=sdao.findID(id);
            if(flag){
                JFrame jf1 = new JFrame();
                String[][] dates = sdao.findstudent( id);
                JScrollPane jsp = new JScrollPane();
                JTable table = new JTable(dates, getTitles1());
                jsp = new JScrollPane(table);// 存放到了面f板中
                jf1.add(jsp);
                jf1.setSize(800, 300);
                jf1. setLocationRelativeTo(null);
                jf1.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                jf1.setVisible(true);
            }else {
                JOptionPane.showMessageDialog(null,"无该学号!\n请重新输入","提示消息",JOptionPane.ERROR_MESSAGE);
                jt1.setText("");
                return;
            }

        }

    }

    private String[] getTitles1() {
        String[] titles = {"学号", "姓名", "性别", "年龄", "专业", "入学时间", "JAVA成绩", "数学成绩", "英语成绩", "总成绩", "平均成绩"};
        return titles;
    }
}

 姓名查询(重名)

姓名查询(NameSearch.java)

package com.zj.java.view;

import com.zj.java.dao.StudentDAO;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class NameSearch extends  JDialog{
    private JLabel jl1;
    private JTextField  jt2;
    private JButton button;
    private JScrollPane jsp;
    private JTable table;
    private StudentDAO sdao;

    public NameSearch() {

        jl1 = new JLabel("请输入想查询的学生姓名");

        jt2 = new JTextField();
        button = new JButton("查询");

        jl1.setBounds(150, 50, 150, 20);
        jt2.setBounds(310, 50, 150, 20);
        button.setBounds(460, 50, 70, 20);
        add(jl1);
        add(jt2);
        add(button);
        button.addActionListener(new Search());
        setLayout(null);
        setSize( 800, 300);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setVisible(true);
    }
    public class Search implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            sdao = new StudentDAO();
            String name=jt2.getText();
            char[]ch=name.toCharArray();
               int NameNum=sdao.findName(name);

            System.out.println(NameNum);

            //如果没输入
            if (name.equals("")) {
                JOptionPane.showMessageDialog(null, "姓名不能为空!\n请重新输入", "提示消息", JOptionPane.ERROR_MESSAGE);
                return;
            } else {
                //判断是否是汉字
                for (char i : ch) {
                    if (!(i >= 0x0391 && i <= 0xFFE5)) {
                        JOptionPane.showMessageDialog(null, "非法姓名!\n请重新输入", "提示消息", JOptionPane.ERROR_MESSAGE);
                        jt2.setText("");
                        return;
                    }
                }
                        //如果姓名查到了
                        if (NameNum > 0) {
                            JFrame jf1 = new JFrame();
                            String[][] dates = sdao.namefind(name, NameNum);
                            table = new JTable(dates, getTitles());
                            jsp = new JScrollPane(table);// 存放到了面f板中
                            jf1.add(jsp);
                            jf1.setSize(800, 300);
                            jf1.setLocationRelativeTo(null);
                            jf1.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                            jf1.setVisible(true);
                        } else {
                            JOptionPane.showMessageDialog(null, "无该学生!\n请重新查询", "提示消息", JOptionPane.ERROR_MESSAGE);
                            return;
                        }
                        jt2.setText("");
                    }

            }
        }


    private String[] getTitles() {
        String[] titles = {"学号", "姓名", "性别", "年龄", "专业", "入学时间", "java成绩", "数学成绩", "英语成绩", "总成绩", "平均成绩"};
        return titles;
    }
}

学生注册

要输入的内容都是加了校验的,学号不能为空且必须是数据库里没有的,新的六位数。

姓名能是非法姓名,年龄是10和35之间的整数(可以自己设置条件),成绩不能为空,0-100之间的数。

(SaveView.java)

package com.zj.java.view;

import com.zj.java.dao.StudentDAO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class SaveView extends JDialog {

    private JLabel idLab, nameLab,sexLab,ageLab,subjectLab,grade1Lab,grade2Lab,grade3Lab;
    private JTextField  idVal, nameVal,ageVal,grade1Val,grade2Val,grade3Val;
    private JComboBox subjectBox,sexBox;
    private JButton button;
    private StudentDAO stu = new StudentDAO();
    private String[][] datas;
    public SaveView(JFrame frame, JTable table, JScrollPane jsp) {
        super(frame, true);
        setTitle("注册新学生");
        setLayout(null);
        idLab=new JLabel("学号");
        nameLab = new JLabel("姓名:");
        sexLab = new JLabel("性别:");
        ageLab = new JLabel("年龄:");
        subjectLab = new JLabel("专业:");
        grade1Lab = new JLabel("JAVA成绩:");
        grade2Lab = new JLabel("数学成绩:");
        grade3Lab = new JLabel("英语成绩:");
        idVal=new JTextField();
        nameVal = new JTextField();
        sexBox=new JComboBox();
        ageVal= new JTextField();
        subjectBox = new JComboBox();
        grade1Val= new JTextField();
        grade2Val = new JTextField();
        grade3Val = new JTextField();
        button = new JButton("注册新学生");

        //向性别下拉菜单中填充内容
        String SexStr = stu.findAllsex();
        String[] sexs = SexStr.split("%");
        for (int i = 0; i < sexs.length; i++) {
            sexBox.addItem(sexs[i]);
        }
        // 向专业下拉菜单中填充内容
        String stuStr = stu.findAllSubject();
        String[] stus = stuStr.split("%");
        for (int i = 0; i < stus.length; i++) {
            subjectBox.addItem(stus[i]);
        }

        // 设置位置
        idLab.setBounds(20,20,60,20);
        nameLab.setBounds(20,50,60,20);
        sexLab.setBounds(20,80,60,20);
        ageLab.setBounds(20,110,60,20);
        subjectLab.setBounds(20,140,60,20);
        grade1Lab.setBounds(10,170,70,20);
        grade2Lab.setBounds(20,200,60,20);
        grade3Lab.setBounds(20,230,60,20);
        idVal.setBounds(80,20,100,20);
        nameVal.setBounds(80,50,100,20);
        sexBox.setBounds(80,80,100,20);
        ageVal.setBounds(80,110,100,20);
        subjectBox.setBounds(80,140,100,20);
        grade1Val.setBounds(80,170,100,20);
        grade2Val.setBounds(80,200,100,20);
        grade3Val.setBounds(80,230,100,20);

        button.setBounds(60,260,100,20);
        // 放入控件
        add(idLab);
        add(nameLab);
        add(sexLab);
        add(ageLab);
        add(subjectLab);
        add(grade1Lab);
        add(grade2Lab);
        add(grade3Lab);
       add(idVal);
        add(nameVal);
        add(sexBox);
        add(ageVal);
        add(grade1Val);
        add(grade2Val);
        add(grade3Val);
        add(subjectBox);
        add(button);

        button.addActionListener(new SaveEmp(table, jsp));

        setBounds(0, 3, 260, 400);
        setLocationRelativeTo(null);
        setVisible(true);
    }

    private class SaveEmp implements ActionListener {
        private JTable table;
        private JScrollPane jsp;

        public SaveEmp(JTable table, JScrollPane jsp) {
            this.table = table;
            this.jsp = jsp;
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            // 获取到用户输入的内容

            // 获取到用户输入的内容

            String s = e.getActionCommand();
            if (s.equals("注册新学生")) {
                String num=idVal.getText();
                String name = nameVal.getText();
                String sex = sexBox.getSelectedItem() + "";
                String age1=ageVal.getText();//
                String subject = subjectBox.getSelectedItem() + "";
                String g1=grade1Val.getText();
                String g2=grade2Val.getText();
                String g3=grade3Val.getText();
                int age = 0,id=0;
                double grade1 =0.0;
                double grade2 = 0.0;
                double grade3 = 0.0;
                char[]ch=name.toCharArray();
                try {
                    boolean flag=stu.findID(num);
                    if(flag){
                        JOptionPane.showMessageDialog(null,"学号已存在!\n请重新输入","提示消息",JOptionPane.ERROR_MESSAGE);
                       idVal.setText("");
                        return;
                    }
                    if(num.equals("")){
                        JOptionPane.showMessageDialog(null,"学号不能为空!\n请重新输入","提示消息",JOptionPane.ERROR_MESSAGE);
                        return;
                    }else{
                        try {

                            id = Integer.parseInt(idVal.getText());
                        } catch (NumberFormatException ex) {
                            JOptionPane.showMessageDialog(null,"学号错误!\n请重新输入","提示消息",JOptionPane.ERROR_MESSAGE);
                            return;
                        }
                    }
                    if(id<100000||id>=1000000){
                        JOptionPane.showMessageDialog(null,"学号错误!\n(学号六位)请重新输入","提示消息",JOptionPane.ERROR_MESSAGE);
                        return;
                    }

                    if (name.equals("")) {
                        JOptionPane.showMessageDialog(null,"姓名不能为空!\n请重新输入","提示消息",JOptionPane.ERROR_MESSAGE);
                        return;
                    }else{
                        //判断是否是汉字
                        for (char i:ch) {
                            if (!(i>=0x0391&&i<=0xFFE5)){
                                JOptionPane.showMessageDialog(null,"非法姓名!\n请重新输入","提示消息",JOptionPane.ERROR_MESSAGE);
                                 nameVal.setText("");
                                return;
                            }else {
                                break;
                            }
                        }
                    }
                    if(age1.equals("")){
                        JOptionPane.showMessageDialog(null,"年龄不能为空!\n请重新输入","提示消息",JOptionPane.ERROR_MESSAGE);
                        return;
                    } else {
                        try {
                            age = Integer.parseInt(ageVal.getText());
                        } catch (NumberFormatException ex) {
                            JOptionPane.showMessageDialog(null,"年龄只能为两位数的整数!\n请重新输入","提示消息",JOptionPane.ERROR_MESSAGE);
                           return;
                        }
                    }
                    if(age<=10||age>=35){
                        JOptionPane.showMessageDialog(null,"年龄错误!\n请重新输入","提示消息",JOptionPane.ERROR_MESSAGE);
                        return;
                    }

                    if( g1.equals("")||g2.equals("")||g3.equals("")){
                        JOptionPane.showMessageDialog(null,"成绩不能为空!\n请重新输入","提示消息",JOptionPane.ERROR_MESSAGE);
                        return;
                    }else {
                        grade1 = Double.parseDouble(grade1Val.getText());
                        grade2 = Double.parseDouble(grade2Val.getText());
                        grade3 = Double.parseDouble(grade3Val.getText());
                    }
                    if(grade1<0||grade1>100){
                        JOptionPane.showMessageDialog(null,"JAVA成绩错误!\n请重新输入","提示消息",JOptionPane.ERROR_MESSAGE);
                        return;
                    }
                    if(grade2<0||grade2>100){
                        JOptionPane.showMessageDialog(null,"数学成绩错误!\n请重新输入","提示消息",JOptionPane.ERROR_MESSAGE);
                        return;
                    }
                    if(grade3<0||grade3>100){
                        JOptionPane.showMessageDialog(null,"英语成绩错误!\n请重新输入","提示消息",JOptionPane.ERROR_MESSAGE);
                        return;
                    }
                } catch (HeadlessException ex) {
                    ex.printStackTrace();
                    return;

                }

                JOptionPane.showMessageDialog(null, "注册成功");
                // 调用StudentDAO的方法执行添加操作

                stu.save( id,name,sex,age,subject, grade1, grade2, grade3);
                // 关闭当前窗体
                setVisible(false);
                // 刷新table
                datas = stu.page(1); // 重新查询
                table = new JTable(datas, getTitles());
                jsp.setViewportView(table); // 刷新面板

            }

        }

    }

    private String[] getTitles() {
        String[] titles = {"学号","姓名","性别","年龄","专业","入学时间","JAVA成绩","数学成绩","英语成绩","总成绩","平均成绩"};
        return titles;
    }
}

 信息修改

选中某个人的信息然后点击信息修改按钮, 学号是主键,不能修改

(UpdateView.java) 

package com.zj.java.view;

import com.zj.java.dao.StudentDAO;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class UpdateView extends JDialog {

    private JLabel idLab, nameLab, sexLab, ageLab, subjectLab, grade1Lab, grade2Lab, grade3Lab;
    private JTextField idVal, nameVal, ageVal, grade1Val, grade2Val, grade3Val;
    private JComboBox subjectBox, sexBox;
    private JButton button;
    private StudentDAO stu = new StudentDAO();
    private String[][] datas;

    public UpdateView(JFrame frame, JTable table, JScrollPane jsp, String id, String name, String sex, String age, String subject, String grade1, String grade2, String grade3) {
        super(frame, true);
        setTitle("修改学生信息");
        setLayout(null);
        idLab = new JLabel("学号:");
        nameLab = new JLabel("姓名:");
        sexLab = new JLabel("性别:");
        ageLab = new JLabel("年龄:");
        subjectLab = new JLabel("专业:");
        grade1Lab = new JLabel("JAVA成绩:");
        grade2Lab = new JLabel("数学成绩:");
        grade3Lab = new JLabel("英语成绩:");

        idVal = new JTextField();
        nameVal = new JTextField();
        sexBox = new JComboBox();
        ageVal = new JTextField();
        subjectBox = new JComboBox();


        grade1Val = new JTextField();
        grade2Val = new JTextField();
        grade3Val = new JTextField();
        button = new JButton("修改信息");

        // 给这些内容赋予合适的初值
        idVal.setText(id);
        idVal.setEditable(false);
        nameVal.setText(name);
        ageVal.setText(age);
        grade1Val.setText(grade1);
        grade2Val.setText(grade2);
        grade3Val.setText(grade3);

        //向性别下拉菜单中填充内容
        String SexStr = stu.findAllsex();
        String[] sexs = SexStr.split("%");
        for (int i = 0; i < sexs.length; i++) {
            sexBox.addItem(sexs[i]);
        }
        // 设置下拉菜单被选中的初值
        sexBox.setSelectedItem(sex);
        // 向专业下拉菜单中填充内容
        String stuStr = stu.findAllSubject();
        String[] stus = stuStr.split("%");
        for (int i = 0; i < stus.length; i++) {
            subjectBox.addItem(stus[i]);
        }
        // 设置下拉菜单被选中的初值
        subjectBox.setSelectedItem(subject);
        // 设置位置
        idLab.setBounds(20, 20, 60, 20);
        nameLab.setBounds(20, 50, 60, 20);
        sexLab.setBounds(20, 80, 60, 20);
        ageLab.setBounds(20, 110, 60, 20);
        subjectLab.setBounds(20, 140, 60, 20);
        grade1Lab.setBounds(10, 170, 70, 20);
        grade2Lab.setBounds(20, 200, 60, 20);
        grade3Lab.setBounds(20, 230, 60, 20);

        idVal.setBounds(80, 20, 100, 20);
        nameVal.setBounds(80, 50, 100, 20);
        sexBox.setBounds(80, 80, 100, 20);
        ageVal.setBounds(80, 110, 100, 20);
        subjectBox.setBounds(80, 140, 100, 20);
        grade1Val.setBounds(80, 170, 100, 20);
        grade2Val.setBounds(80, 200, 100, 20);
        grade3Val.setBounds(80, 230, 100, 20);
        button.setBounds(60, 260, 100, 20);

        // 放入控件
        add(idLab);
        add(nameLab);
        add(sexLab);
        add(ageLab);
        add(subjectLab);
        add(grade1Lab);
        add(grade2Lab);
        add(grade3Lab);
        add(idVal);
        add(nameVal);
        add(sexBox);
        add(ageVal);
        add(grade1Val);
        add(grade2Val);
        add(grade3Val);
        add(subjectBox);
        add(button);

        button.addActionListener(new UpdateStu(table, jsp, id));

        setBounds(0, 3, 300, 400);
        setLocationRelativeTo(null);
        setVisible(true);
    }

    private class UpdateStu implements ActionListener {
        private JTable table;
        private JScrollPane jsp;
        private String id;

        public UpdateStu(JTable table, JScrollPane jsp, String id) {
            this.table = table;
            this.jsp = jsp;
            this.id = id;
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            // 获取到用户输入的内容
            String s = e.getActionCommand();
            if (s.equals("修改信息")) {
                String name = nameVal.getText();
                String sex = sexBox.getSelectedItem() + "";
                String age1 = ageVal.getText();//
                String subject = subjectBox.getSelectedItem() + "";
                String g1 = grade1Val.getText();
                String g2 = grade2Val.getText();
                String g3 = grade3Val.getText();
                int age = 0;
                double grade1 = 0.0;
                double grade2 = 0.0;
                double grade3 = 0.0;
                char[] ch = name.toCharArray();
                try {
                    if (name.equals("")) {
                        JOptionPane.showMessageDialog(null, "姓名不能为空!\n请重新输入", "提示消息", JOptionPane.ERROR_MESSAGE);
                        return;
                    } else {
                        for (char i : ch) {
                            if (!(i >= 0x0391 && i <= 0xFFE5)) {
                                JOptionPane.showMessageDialog(null, "非法姓名!\n请重新输入", "提示消息", JOptionPane.ERROR_MESSAGE);
                                nameVal.setText("");
                                return;
                            } else {
                                break;
                            }
                        }
                    }
                    if (age1.equals("")) {
                        JOptionPane.showMessageDialog(null, "年龄不能为空!\n请重新输入", "提示消息", JOptionPane.ERROR_MESSAGE);
                        return;
                    } else {
                        try {
                            age = Integer.parseInt(ageVal.getText());
                        } catch (NumberFormatException ex) {
                            JOptionPane.showMessageDialog(null, "年龄只能为两位数的整数!\n请重新输入", "提示消息", JOptionPane.ERROR_MESSAGE);
                            return;
                        }
                    }
                    if (age <= 10 || age >= 35) {
                        JOptionPane.showMessageDialog(null, "年龄错误!\n请重新输入", "提示消息", JOptionPane.ERROR_MESSAGE);
                        return;
                    }
                    if (g1.equals("") || g2.equals("") || g3.equals("")) {
                        JOptionPane.showMessageDialog(null, "成绩不能为空!\n请重新输入", "提示消息", JOptionPane.ERROR_MESSAGE);
                        return;
                    } else {
                        grade1 = Double.parseDouble(grade1Val.getText());
                        grade2 = Double.parseDouble(grade2Val.getText());
                        grade3 = Double.parseDouble(grade3Val.getText());
                    }
                    if (grade1 < 0 || grade2 > 100) {
                        JOptionPane.showMessageDialog(null, "JAVA成绩错误!\n请重新输入", "提示消息", JOptionPane.ERROR_MESSAGE);
                        return;
                    }
                    if (grade2 < 0 || grade2 > 100) {
                        JOptionPane.showMessageDialog(null, "数学成绩错误!\n请重新输入", "提示消息", JOptionPane.ERROR_MESSAGE);
                        return;
                    }
                    if (grade3 < 0 || grade3 > 100) {
                        JOptionPane.showMessageDialog(null, "英语成绩错误!\n请重新输入", "提示消息", JOptionPane.ERROR_MESSAGE);
                        return;
                    }
                } catch (HeadlessException ex) {
                    ex.printStackTrace();
                    return;
                }
                JOptionPane.showMessageDialog(null, "修改成功!");
                // 调用StudentDAO的方法执行添加操作
                stu.update(id, name, sex, age, subject, grade1, grade2, grade3);
                // 关闭当前窗体
                setVisible(false);
                // 刷新table
                datas = stu.page(1); // 重新查询
                table = new JTable(datas, getTitles());
                jsp.setViewportView(table); // 刷新面板
            }
        }
    }

    private String[] getTitles() {
        String[] titles = {"学号", "姓名", "性别", "年龄", "专业", "入学时间", "JAVA成绩", "数学成绩", "英语成绩", "总成绩", "平均成绩"};
        return titles;
    }
}

信息删除

 选择想要删除的学生,点击删除学生按钮,会弹框确认是否删除该学生

 学生端登录

这里我加入了一张背景图,iew界面也是可以加入图片的 ,感兴趣的友友们可以自己试着仿学生界面的代码给其他界面加入图片

(StudentMainView.java)

package com.zj.java.view;
import com.zj.java.dao.StudentDAO;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class StudentMainView extends JFrame {
    private JPanel top, center;
    private JLabel jl1, jl2;
    private JTextField jt1;
    private JButton button;
    JLayeredPane center1;
    private final StudentDAO sdao = new StudentDAO();
    public StudentMainView() {
        setTitle("学生界面");
        top = new JPanel();
        top.setLayout(new FlowLayout(FlowLayout.LEFT));
        //jl1 = new JLabel("个人信息查询");
        // top.add(jl1);
        jl1 = new JLabel("个人信息查询");
        jl2 = new JLabel("请输入你的学号:");
        jt1 = new JTextField();
        button = new JButton("查询");
        jl1.setFont(new Font("华文行楷", Font.PLAIN, 25));
        jl2.setFont(new Font("宋体", Font.BOLD, 13));
        //   button.setContentAreaFilled(false);
        center = new JPanel();
        JPanel jp = new JPanel(null);//可以在这个面板中随意放置组件
        setLayout(new GridLayout(1, 1)); //设置只有一行一列
        center1 = new JLayeredPane() {
            public void paintComponent(Graphics g) {//重写绘制面板的方法
                super.paintComponent(g);
                ImageIcon image = new ImageIcon("D:\\a\\img.jpg");//导入背景图片,这里是图片的绝对路径,不添加图片也可以
                image.setImage(image.getImage().getScaledInstance(this.getWidth(), this.getHeight(), Image.SCALE_AREA_AVERAGING)); //设置大小
                g.drawImage(image.getImage(), 0, 0, this); //重新绘制面板
            }
        };

        center.setLayout(null);
        jl1.setBounds(280, 50, 300, 25);
        jl2.setBounds(180, 200, 120, 25);
        jt1.setBounds(300, 200, 150, 25);
        button.setBounds(460, 200, 70, 25);
        center1.add(jl1);
        center1.add(jl2);
        center1.add(jt1);
        center1.add(button);
        this.add(center1);
        //给按钮添加事件

        center1.add(top, BorderLayout.NORTH);

        center1.add(center, BorderLayout.CENTER);


        button.addActionListener(new IdSearch());

        setBounds(0, 3, 800, 500);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setVisible(true);
    }

    private class IdSearch implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            String id = jt1.getText();
            int num = 0;

            if (id.equals("")) {
                JOptionPane.showMessageDialog(null, "学号不能为空!\n请重新输入", "提示消息", JOptionPane.ERROR_MESSAGE);
                return;
            } else {
                try {
                    num = Integer.parseInt(jt1.getText());
                } catch (NumberFormatException ex) {
                    JOptionPane.showMessageDialog(null, "学号错误!\n(六位整数)请重新输入", "提示消息", JOptionPane.ERROR_MESSAGE);
                    jt1.setText("");
                    return;
                }
            }
            if (num < 100000 || num >= 1000000) {
                JOptionPane.showMessageDialog(null, "学号错误!\n(学号六位)请重新输入", "提示消息", JOptionPane.ERROR_MESSAGE);
                jt1.setText("");
                return;
            }
            boolean flag = sdao.findID(id);
            if (flag) {
                JFrame jf1 = new JFrame();
                String[][] dates = sdao.findstudent(id);
                JScrollPane jsp = new JScrollPane();
                JTable table = new JTable(dates, getTitles1());
                jsp = new JScrollPane(table);// 存放到了面f板中
                jf1.add(jsp);
                jf1.setSize(800, 300);
                jf1.setLocationRelativeTo(null);
                jf1.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                jf1.setVisible(true);
                jt1.setText("");
            } else {
                JOptionPane.showMessageDialog(null, "无该学号!\n请重新输入", "提示消息", JOptionPane.ERROR_MESSAGE);
                jt1.setText("");
                return;
            }
        }
    }

    private String[] getTitles1() {
        String[] titles = {"学号", "姓名", "性别", "年龄", "专业", "入学时间", "JAVA成绩", "数学成绩", "英语成绩", "总成绩", "平均成绩"};
        return titles;
    }

    public static void main(String[] args) {
        new StudentMainView();

    }
}

数据库连接(DBManager.java)

package com.zj.java.db;

import java.sql.*;

// 建立数据库链接
public class DBManager {
    public static Connection getConnection(){
        Connection conn = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            String url = "jdbc:mysql://localhost:3306/xsgl?serverTimezone=Asia/Shanghai&useSSL=false&allowPublicKeyRetrieval=true";
            String user = "root";
            String password = "  ";//这里填写自己的MySQL数据库密码
            conn = DriverManager.getConnection(url,user,password);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return conn;
    }

    // 关闭数据库链接
    public static void close(Connection conn, PreparedStatement ps, ResultSet rs) {
        try {
            if (rs != null) {
                rs.close();
            }
            ps.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}

对数据库进行增删改查的具体操作(StudentDAO.java)

package com.zj.java.dao;

import com.zj.java.db.DBManager;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class StudentDAO {

    private Connection conn;
    private PreparedStatement ps;
    private ResultSet rs;


    //学号查询,判断是否有此学号
    public boolean findID(String id) {
        String sql = "select 学号 from student where studentState=1";
        String[] Sid = new String[findAllCount()];
        conn = DBManager.getConnection();
        try {
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            int i = 0;
            while (rs.next()) {
                Sid[i] = rs.getInt(1) + "";
                i++;
            }
            for (int j = 0; j < Sid.length; j++) {
                String id2 = Sid[j];
                if (id2 != null && id2.equals(id)) {
                    return true;
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }


    //姓名查询,判断是否有此姓名(可重名查询)
    public int findName(String name) {

        String sql = "select 姓名 from student where studentState=1";
        String[] Sname = new String[findAllCount()];
        conn = DBManager.getConnection();
        try {
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            int i = 0;
            while (rs.next()) {
                Sname[i] = rs.getString(1) + "";
                i++;
            }
            int NameNum = 0;//记录重名人的个数
            for (int j = 0; j < Sname.length; j++) {
                String id2 = Sname[j];
                if (id2.equals(name)) {
                    NameNum += 1;
                }
            }
            if (NameNum != 0) {
                return NameNum;
                //如果查到此姓名,返回值为重名人的个数(查有此人,且唯一及返回1,如查有3人,即返回3)
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return -1;
    }


    //分页展示
    public String[][] page(int page) {
        String sql = "select  学号,姓名,性别,年龄,专业,入学时间,JAVA成绩,数学成绩,英语成绩,\n" +
                "  (`JAVA成绩`+`数学成绩`+`英语成绩`) 总成绩,ROUND((`JAVA成绩`+`数学成绩`+`英语成绩`)/3,2) 平均成绩\n" +
                " from student  where studentState=1 limit ?,25";
        String[][] datas = new String[25][11];
        conn = DBManager.getConnection();
        try {
            ps = conn.prepareStatement(sql);

            ps.setInt(1, (page - 1) * 25);
            rs = ps.executeQuery();

            int i = 0;
            while (rs.next()) {
                // rs ---> thisRow
                datas[i][0] = rs.getInt(1) + "";
                datas[i][1] = rs.getString(2);
                datas[i][2] = rs.getString(3);
                datas[i][3] = rs.getInt(4) + "";
                datas[i][4] = rs.getString(5);
                datas[i][5] = rs.getString(6) + "";
                datas[i][6] = rs.getDouble(7) + "";
                datas[i][7] = rs.getDouble(8) + "";
                datas[i][8] = rs.getDouble(9) + "";
                datas[i][9] = rs.getDouble(10) + "";
                datas[i][10] = rs.getDouble(11) + "";

                i++;
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }

        return datas;
    }

    //查询一共有多少名学生,方便确定分页按钮
    public int findAllCount() {
        String sq = "select count(*) from student where studentState=1";
        conn = DBManager.getConnection();
        try {
            ps = conn.prepareStatement(sq);
            rs = ps.executeQuery();
            if (rs.next()) {
                return rs.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return -1;
    }

    //查询所以专业,修改信息时下拉框展示
    public String findAllSubject() {
        String str = "";
        String sql = "select distinct 专业 from student where studentState=1"; // 唯一班级
        conn = DBManager.getConnection();
        try {
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()) {
                str += rs.getString(1) + "%";
            }
        } catch (SQLException e) {

        }
        // a%b%c%d%
        str = str.substring(0, str.length() - 1); // 删除最后一个字符
        return str;
    }

    //性别,下拉框展示
    public String findAllsex() {
        String str = "";
        String sql = "select distinct 性别 from student where studentState=1"; // 唯一性别
        conn = DBManager.getConnection();
        try {
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()) {
                str += rs.getString(1) + "%";
            }
        } catch (SQLException e) {

        }
        // a%b%c%d%
        str = str.substring(0, str.length() - 1); // 删除最后一个字符
        return str;
    }


    //删除操作
    public void delete(String id) {
        String sql = "update student set studentState=0 where 学号=" + id;
        conn = DBManager.getConnection();
        try {
            ps = conn.prepareStatement(sql);
            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    //新增
    public void save(int id, String name, String sex, int age, String subject, double grade1, double grade2, double grade3) {
        String sql = "insert into student values(?,?,?,?,?,now(),?,?,?,1)";
        conn = DBManager.getConnection();
        try {
            ps = conn.prepareStatement(sql);
            ps.setInt(1, id);
            ps.setString(2, name);
            ps.setString(3, sex);
            ps.setInt(4, age);
            ps.setString(5, subject);
            ps.setDouble(6, grade1);
            ps.setDouble(7, grade2);
            ps.setDouble(8, grade3);
            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    //修改
    public void update(String id, String name, String sex, int age, String subject, double grade1, double grade2, double grade3) {
        String sql = "update student set  姓名=?,性别=?,年龄=?,专业=?,JAVA成绩=? ,数学成绩=?,英语成绩=? where 学号=?";
        conn = DBManager.getConnection();
        try {
            ps = conn.prepareStatement(sql);

            ps.setString(1, name);
            ps.setString(2, sex);
            ps.setInt(3, age);
            ps.setString(4, subject);
            ps.setDouble(5, grade1);
            ps.setDouble(6, grade2);
            ps.setDouble(7, grade3);
            ps.setString(8, id);
            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    //按学号查询学生信息
    public String[][] findstudent(String id) {

        String sql = "select 学号,姓名,性别,年龄,专业,入学时间,JAVA成绩,数学成绩,英语成绩,\n " +
                " (`JAVA成绩`+`数学成绩`+`英语成绩`) 总成绩,ROUND((`JAVA成绩`+`数学成绩`+`英语成绩`)/3,2) 平均成绩\n" +
                "from student WHERE 学号=? ";
        String[][] datas1 = new String[1][11];

        conn = DBManager.getConnection();
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1, id);
            rs = ps.executeQuery();
            int i = 0;
            while (rs.next()) {
                datas1[i][0] = rs.getString(1)/*+""*/;
                datas1[i][1] = rs.getString(2);
                datas1[i][2] = rs.getString(3);
                datas1[i][3] = rs.getString(4);
                datas1[i][4] = rs.getString(5);
                datas1[i][5] = rs.getString(6);
                datas1[i][6] = rs.getString(7);
                datas1[i][7] = rs.getString(8);
                datas1[i][8] = rs.getString(9);
                datas1[i][9] = rs.getString(10);
                datas1[i][10] = rs.getString(11);
                i++;

            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return datas1;

    }

    //按姓名查询学生信息
    public String[][] namefind(String name, int NameNum) {

        String sql = "select 学号,姓名,性别,年龄,专业,入学时间,JAVA成绩,数学成绩,英语成绩,\n " +
                " (`JAVA成绩`+`数学成绩`+`英语成绩`) 总成绩,ROUND((`JAVA成绩`+`数学成绩`+`英语成绩`)/3,2) 平均成绩\n" +
                "from student WHERE 姓名=?  ";

        String[][] dates1 = new String[NameNum][11];//NameNum为重名人的个数
        conn = DBManager.getConnection();
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1, name);
            rs = ps.executeQuery();
            int i = 0;
            while (rs.next()) {
                dates1[i][0] = rs.getString(1);
                dates1[i][1] = rs.getString(2);
                dates1[i][2] = rs.getString(3);
                dates1[i][3] = rs.getString(4);
                dates1[i][4] = rs.getString(5);
                dates1[i][5] = rs.getString(6);
                dates1[i][6] = rs.getString(7);
                dates1[i][7] = rs.getString(8);
                dates1[i][8] = rs.getString(9);
                dates1[i][9] = rs.getString(10);
                dates1[i][10] = rs.getString(11);
                i++;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return dates1;

    }


}

 数据库(xsgl)



SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for student
-- ----------------------------
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student`  (
  `学号` int NOT NULL,
  `姓名` char(10) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `性别` char(10) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `年龄` int NOT NULL,
  `专业` char(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `入学时间` date NULL DEFAULT NULL,
  `JAVA成绩` double NOT NULL,
  `数学成绩` double NOT NULL,
  `英语成绩` double NOT NULL,
  `studentSTATE` int NULL DEFAULT 1,
  PRIMARY KEY (`学号`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of student
-- ----------------------------
INSERT INTO `student` VALUES (102140, '赵芸', '男', 19, '电商专业', '2021-12-24', 78.8, 78, 78, 1);
INSERT INTO `student` VALUES (102153, '谢晓晓', '女', 21, '信息专业', '2021-12-25', 67.8, 78.7, 67.8, 1);
INSERT INTO `student` VALUES (102154, '刘潇', '女', 21, '信息专业', '2021-12-25', 78, 67, 78.8, 1);
INSERT INTO `student` VALUES (102339, '赵芸', '女', 21, '信息专业', '2021-12-24', 78, 59.7, 76, 1);
INSERT INTO `student` VALUES (102342, '张思', '女', 21, '信息专业', '2021-12-16', 89, 78, 67.8, 1);
INSERT INTO `student` VALUES (102355, '谢亦', '男', 21, '信息专业', '2021-12-24', 67, 69, 67.8, 1);
INSERT INTO `student` VALUES (109201, '张宁', '女', 21, '信息专业', '2021-12-14', 67, 78, 76, 1);
INSERT INTO `student` VALUES (109202, '张思', '女', 21, '计算机专业', '2021-12-14', 679, 89, 67, 1);
INSERT INTO `student` VALUES (109203, '李瑶', '女', 19, '信息专业', '2021-12-13', 87, 89, 78, 1);
INSERT INTO `student` VALUES (109212, '李晓', '女', 21, '信息专业', '2021-12-13', 78, 78, 89, 1);
INSERT INTO `student` VALUES (109213, '张洪', '男', 19, '信息专业', '2021-12-13', 23, 34, 56, 1);
INSERT INTO `student` VALUES (109214, '刘丽', '女', 19, '信息专业', '2021-09-03', 78, 89, 67, 1);
INSERT INTO `student` VALUES (109217, '张密', '女', 19, '信息专业', '2021-12-13', 78.7, 89, 89, 1);
INSERT INTO `student` VALUES (109218, '伽罗', '女', 20, '信息专业', '2021-12-15', 89.6, 89, 89, 1);
INSERT INTO `student` VALUES (109219, '谢宁', '女', 21, '信息专业', '2021-12-15', 78, 89.6, 89, 1);
INSERT INTO `student` VALUES (109220, '小敏', '女', 20, '信息专业', '2021-12-11', 78.7, 87, 87, 1);
INSERT INTO `student` VALUES (109221, '赵五', '女', 23, '信息专业', '2021-12-13', 45, 99, 78, 0);
INSERT INTO `student` VALUES (109222, '小梦', '女', 21, '信息专业', '2021-12-11', 78.6, 88, 98, 0);
INSERT INTO `student` VALUES (109223, '小明', '男', 20, '信息专业', '2021-12-11', 68.9, 89, 78, 1);
INSERT INTO `student` VALUES (109224, '小黑', '男', 19, '信息专业', '2021-12-11', 67, 98, 88, 1);
INSERT INTO `student` VALUES (109225, '小刘', '女', 19, '信息专业', '2021-12-11', 67, 89, 87, 1);
INSERT INTO `student` VALUES (109226, '西施', '女', 20, '信息专业', '2021-12-13', 78, 54, 65, 1);
INSERT INTO `student` VALUES (109227, '谢意', '女', 19, '信息专业', '2021-12-13', 23, 76, 88, 1);
INSERT INTO `student` VALUES (109228, '张帅', '男', 20, '信息专业', '2021-12-13', 89, 78, 87, 1);
INSERT INTO `student` VALUES (109229, '花花', '女', 19, '信息专业', '2021-12-13', 78, 78, 67, 0);
INSERT INTO `student` VALUES (109230, '萌萌', '女', 20, '信息专业', '2021-12-11', 78, 89, 67, 0);
INSERT INTO `student` VALUES (109231, '马丁蓉', '女', 21, '信息专业', '2021-12-14', 54, 78, 78, 1);
INSERT INTO `student` VALUES (109232, '李白', '男', 21, '信息专业', '2021-12-13', 67.7, 98, 98, 1);
INSERT INTO `student` VALUES (109234, '张雪', '女', 20, '信息专业', '2021-12-10', 89.5, 85, 89, 0);
INSERT INTO `student` VALUES (109245, '吴晓', '女', 20, '信息专业', '2021-12-10', 99, 0, 78, 0);
INSERT INTO `student` VALUES (109256, '刘小晓', '女', 21, '信息专业', '2021-12-10', 78, 67, 87, 1);
INSERT INTO `student` VALUES (109278, '杜丹', '女', 19, '信息专业', '2021-12-10', 89, 89.5, 67, 1);
INSERT INTO `student` VALUES (109367, '米莱迪', '女', 20, '计算机专业', '2021-12-13', 67, 67, 65, 0);
INSERT INTO `student` VALUES (324976, '赵云', '男', 21, '计算机专业', '2021-12-13', 76, 76, 56, 0);
INSERT INTO `student` VALUES (666987, '貂蝉', '女', 19, '信息专业', '2021-12-12', 78, 89, 89, 0);
INSERT INTO `student` VALUES (677647, '李四', '男', 17, '计算机专业', '2020-09-01', 70, 85, 90, 0);
INSERT INTO `student` VALUES (752187, '王五', '男', 22, '计算机专业', '2019-09-03', 67, 79, 89, 0);
INSERT INTO `student` VALUES (756687, '赵六', '男', 20, '信息专业', '2018-09-02', 75, 19.4, 80, 1);
INSERT INTO `student` VALUES (756776, '张三', '男', 19, '计算机专业', '2020-09-02', 80, 0, 98, 0);
INSERT INTO `student` VALUES (765487, '李军', '男', 20, '信息专业', '2021-09-05', 56, 70, 76, 0);
INSERT INTO `student` VALUES (768947, '大乔', '女', 21, '信息专业', '2021-12-14', 55, 67, 89, 1);
INSERT INTO `student` VALUES (768978, '谢意', '女', 21, '信息专业', '2021-12-13', 67, 89, 98, 1);
INSERT INTO `student` VALUES (769887, '刘美', '女', 21, '信息专业', '2021-09-01', 85, 0, 83, 0);
INSERT INTO `student` VALUES (778278, '王晓', '女', 19, '电商专业', '2021-09-09', 79, 78, 93, 0);
INSERT INTO `student` VALUES (778887, '董笑笑', '女', 18, '电商专业', '2018-08-24', 89, 84, 90, 0);
INSERT INTO `student` VALUES (783987, '刘武', '男', 18, '电商专业', '2018-08-24', 78, 72, 80, 1);
INSERT INTO `student` VALUES (784478, '赵蓉', '女', 17, '电商专业', '2019-09-05', 80.6, 70, 77, 0);
INSERT INTO `student` VALUES (786778, '谢苗', '女', 22, '信息专业', '2021-12-13', 87, 34, 56, 1);
INSERT INTO `student` VALUES (790076, '张燕', '女', 21, '信息专业', '2021-09-03', 95, 67, 78, 1);
INSERT INTO `student` VALUES (790276, '胡小月', '女', 22, '信息专业', '2021-09-04', 78, 87, 80, 1);
INSERT INTO `student` VALUES (793476, '王娜', '女', 19, '信息专业', '2020-08-30', 87, 87, 70, 1);
INSERT INTO `student` VALUES (793567, '谢敏', '女', 20, '计算机专业', '2021-12-11', 78.5, 76, 78, 1);
INSERT INTO `student` VALUES (793676, '胡笑', '女', 20, '信息专业', '2021-12-11', 87, 68, 78, 1);
INSERT INTO `student` VALUES (793765, '邱月', '女', 21, '信息专业', '2021-12-11', 78, 78, 67, 1);
INSERT INTO `student` VALUES (793867, '王萌', '女', 19, '信息专业', '2021-12-11', 78, 78, 98, 1);
INSERT INTO `student` VALUES (793976, '小军', '男', 19, '信息专业', '2021-12-11', 78, 87, 87, 1);
INSERT INTO `student` VALUES (794067, '张茹', '女', 21, '信息专业', '2021-12-12', 67, 87, 67, 1);
INSERT INTO `student` VALUES (794176, '杨蓉', '女', 21, '信息专业', '2021-12-12', 57, 67, 89, 1);
INSERT INTO `student` VALUES (794276, '杨玉华', '女', 19, '信息专业', '2021-12-12', 67, 78, 89, 0);
INSERT INTO `student` VALUES (794376, '阿狸', '女', 19, '信息专业', '2021-12-12', 78, 76, 67, 0);
INSERT INTO `student` VALUES (794467, '伽罗', '女', 19, '信息专业', '2021-12-12', 76, 67, 87, 0);
INSERT INTO `student` VALUES (794776, '艾琳', '女', 19, '信息专业', '2021-12-12', 78, 78, 88, 0);
INSERT INTO `student` VALUES (794967, '孙尚香', '女', 19, '信息专业', '2021-12-12', 88, 78, 78, 0);
INSERT INTO `student` VALUES (795057, '小马', '女', 19, '信息专业', '2021-12-12', 67, 78, 78, 1);
INSERT INTO `student` VALUES (795175, '王娟', '女', 19, '信息专业', '2021-12-12', 43, 54, 46, 0);
INSERT INTO `student` VALUES (795256, '王佳铭', '男', 19, '信息专业', '2021-12-12', 56, 89, 98, 1);
INSERT INTO `student` VALUES (795346, '谢苗1', '女', 19, '信息专业', '2021-12-13', 45, 89.7, 78, 0);
INSERT INTO `student` VALUES (795487, '谢总', '男', 20, '信息专业', '2021-12-13', 99, 100, 100, 0);
INSERT INTO `student` VALUES (795576, '林一', '男', 21, '信息专业', '2021-12-13', 78, 89, 76, 1);
INSERT INTO `student` VALUES (795665, '刘芳', '女', 20, '信息专业', '2021-12-13', 98, 88, 89, 1);
INSERT INTO `student` VALUES (795765, '张文涛', '男', 21, '计算机专业', '2021-12-13', 99, 99, 78, 1);
INSERT INTO `student` VALUES (795865, '林心怡', '女', 20, '电商专业', '2021-12-13', 88, 78, 89, 1);
INSERT INTO `student` VALUES (795976, '马欣杰', '男', 22, '计算机专业', '2021-12-13', 99, 87, 76, 1);
INSERT INTO `student` VALUES (796087, '张云霞', '女', 22, '电商专业', '2021-12-13', 88, 78, 99, 1);
INSERT INTO `student` VALUES (796157, '刘璇', '女', 21, '信息专业', '2021-12-13', 77, 68, 89, 1);
INSERT INTO `student` VALUES (796387, '李佳敏', '女', 22, '信息专业', '2021-12-13', 59, 78, 89, 1);
INSERT INTO `student` VALUES (796468, '丁妍妍', '女', 21, '信息专业', '2021-12-13', 89, 89, 100, 1);
INSERT INTO `student` VALUES (796568, '韩辉', '男', 21, '信息专业', '2021-12-13', 78, 88, 67, 1);
INSERT INTO `student` VALUES (796687, '曹玉婷', '女', 21, '信息专业', '2021-12-13', 89, 97, 86, 1);
INSERT INTO `student` VALUES (796767, '李翔瑞', '男', 21, '信息专业', '2021-12-13', 88, 89, 67, 1);
INSERT INTO `student` VALUES (796876, '马世豪', '男', 21, '信息专业', '2021-12-13', 88, 67, 59, 1);
INSERT INTO `student` VALUES (796956, '张文静', '女', 21, '信息专业', '2021-12-13', 87, 88, 78, 1);
INSERT INTO `student` VALUES (797045, '杨玉环', '女', 21, '信息专业', '2021-12-13', 89, 79, 100, 1);
INSERT INTO `student` VALUES (797124, '李白', '男', 22, '信息专业', '2021-12-13', 78, 88, 98, 1);
INSERT INTO `student` VALUES (797267, '侯玉杰', '男', 22, '信息专业', '2021-12-13', 98, 95, 78, 1);
INSERT INTO `student` VALUES (797365, '甜梦', '女', 19, '信息专业', '2021-12-13', 78, 90, 90, 1);
INSERT INTO `student` VALUES (797446, '张静', '女', 21, '信息专业', '2021-12-13', 76, 89, 89, 0);
INSERT INTO `student` VALUES (797667, '防护', '女', 19, '信息专业', '2021-12-13', 78, 99, 22, 1);
INSERT INTO `student` VALUES (797767, '赵淼', '女', 18, '信息专业', '2021-12-13', 67, 87, 76, 1);
INSERT INTO `student` VALUES (797845, '谢苗', '女', 19, '信息专业', '2021-12-13', 67, 87, 68, 1);
INSERT INTO `student` VALUES (797946, '花花', '女', 19, '信息专业', '2021-12-13', 34, 54, 56, 1);
INSERT INTO `student` VALUES (878878, '郭梦雪', '女', 21, '信息专业', '2021-12-13', 88, 89, 98, 1);
INSERT INTO `student` VALUES (879889, '小乔', '女', 19, '信息专业', '2021-12-12', 88, 87, 77, 0);
INSERT INTO `student` VALUES (887864, '王璐', '男', 20, '信息专业', '2019-08-29', 78, 78, 67, 1);
INSERT INTO `student` VALUES (887867, '妲己', '女', 18, '信息专业', '2021-12-12', 66, 67, 78, 0);
INSERT INTO `student` VALUES (897360, '马丁蓉', '女', 21, '电商专业', '2021-12-13', 78, 76, 67.7, 1);
INSERT INTO `student` VALUES (899797, '田蒙', '女', 21, '信息专业', '2021-12-10', 89, 90, 67.9, 1);
INSERT INTO `student` VALUES (1897365, '马丁蓉', '女', 21, '信息专业', '2021-12-13', 35, 89, 68, 1);

-- ----------------------------
-- Table structure for suser
-- ----------------------------
DROP TABLE IF EXISTS `suser`;
CREATE TABLE `suser`  (
  `username` char(10) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `userpass` char(10) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  PRIMARY KEY (`username`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of suser
-- ----------------------------
INSERT INTO `suser` VALUES ('123456', 'a123456');

-- ----------------------------
-- Table structure for tuser
-- ----------------------------
DROP TABLE IF EXISTS `tuser`;
CREATE TABLE `tuser`  (
  `username` char(10) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `userpass` char(10) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  PRIMARY KEY (`username`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of tuser
-- ----------------------------
INSERT INTO `tuser` VALUES ('654321', 'a654321');

-- ----------------------------
-- Triggers structure for table suser
-- ----------------------------
DROP TRIGGER IF EXISTS `trig_1`;
delimiter ;;
CREATE TRIGGER `trig_1` BEFORE INSERT ON `suser` FOR EACH ROW ## 在触发器中不能查询显示内容
    insert into tuser values('张三','12345')
;;
delimiter ;

SET FOREIGN_KEY_CHECKS = 1;

这是自己之前做java实验报告做的一个学生信息管理系统,老师布置作业的时候自己还不会swing,swing已经比较老了,我也是自己在哔哩哔哩上学了些swing的知识做的,功能上比较简单,界面也不是很好看,代码上有的地方也不是很简洁,但运行上是没有问题的,希望能对大家有帮助吧!

Logo

本社区面向用户介绍CSDN开发云部门内部产品使用和产品迭代功能,产品功能迭代和产品建议更透明和便捷

更多推荐