Java Swing+SQL sever student management system code

Main interface (Interface class):

Main interface: I set the global font. If the font is set in each class, it will look very bloated, so I wrote a method to define a global font. Of course, there are some special needs at the end For the font, I redefine the font separately, and this global font is to standardize all fonts.

Then added the background image:

The method of setting the background image is basically the same, but you need to know that when you add these components to the Java Swing window, if you do not set transparency, it will be covered and cannot be displayed. This is what I did before The difficulties encountered, so when adding these things to each class later, I will set up three plates to accommodate the components that need to be added. If you are using C#, it will be much more convenient!

        //设置背景图片
        ImageIcon img = new ImageIcon("C:\\Users\\god36c\\OneDrive\\桌面\\图一.jpg");
        JLabel bgimg = new JLabel(img);
        bgimg.setBounds(0,0,img.getIconWidth(),img.getIconHeight());

The source code of the main interface (Interface class):

package 高校学生管理库;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Enumeration;

import javax.swing.*;
import javax.swing.plaf.FontUIResource;

public class Interface extends JFrame{
    private static final long serialVersionUID = 1L;
    final int WIDTH = 500;
    final int HEIGHT = 315;
    // 统一设置字体,父界面设置之后,所有由父界面进入的子界面都不需要再次设置字体
    private static void InitGlobalFont(Font font) {
        FontUIResource fontRes = new FontUIResource(font);
        for (Enumeration<Object> keys = UIManager.getDefaults().keys(); keys.hasMoreElements(); ) {
            Object key = keys.nextElement();
            Object value = UIManager.get(key);
            if (value instanceof FontUIResource) {
                UIManager.put(key, fontRes);
            }
        }
    }
    public Interface() {
        setTitle("登录");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        this.setSize(520,370);
        this.setLocationRelativeTo(null);
        setVisible(true);

        //给盘子1创建对象
        javax.swing.JPanel jpanel_1 = new javax.swing.JPanel();
        jpanel_1.setBounds(0,0,WIDTH,HEIGHT);
        jpanel_1.setLayout(null);//设置布局为空
        //给盘子2创建对象
        javax.swing.JPanel jpanel_2 = new javax.swing.JPanel();
        jpanel_2.setBounds(0,30,WIDTH,100);
        FlowLayout flowLayout = new FlowLayout(FlowLayout.CENTER);
        jpanel_2.setLayout(flowLayout);//设置布局为居中
        jpanel_2.setOpaque(false);//设置盘子为透明
        //给盘子3创建对象
        javax.swing.JPanel jpanel_3 = new javax.swing.JPanel();
        jpanel_3.setBounds(0,50,WIDTH,350);
        jpanel_3.setLayout(null);//设置布局为空
        jpanel_3.setOpaque(false);//设置盘子为透明
        //初始化字体
        font fonts = new font();
        //设置背景图片
        ImageIcon img = new ImageIcon("C:\\Users\\god36c\\OneDrive\\桌面\\图一.jpg");
        JLabel bgimg = new JLabel(img);
        bgimg.setBounds(0,0,img.getIconWidth(),img.getIconHeight());

        JLabel title = new JLabel(" 我  是 ");
        title.setFont(font.title);
        title.setHorizontalAlignment(SwingConstants.CENTER);//居中

        JButton g=new JButton("管理员");g.setBounds(200, 80, 100, 40);
        JButton x=new JButton("学生");x.setBounds(200, 200, 100, 40);
        g.setContentAreaFilled(false);x.setContentAreaFilled(false);

        jpanel_3.add(g);jpanel_3.add(x);jpanel_2.add(title);
        jpanel_1.add(bgimg);this.add(jpanel_3);this.add(jpanel_2);
        this.add(jpanel_1);

        x.addActionListener(new ActionListener() {//监听学生按钮
            public void actionPerformed(ActionEvent e) {
                new Slogin();
            }
        });
        g.addActionListener(new ActionListener() {//监听管理员按钮
            public void actionPerformed(ActionEvent e) {
                new Glogin();
            }
        });
    }
    public static void main(String[] args) {
        new Interface().setVisible(true);
        InitGlobalFont(new Font("alias", Font.PLAIN, 18)); //统一设置字体
    }
}

8e5a57c81f914215acce46723115edb6.png

Administrator (Glogin) and student (Slogin) login interface:

The implementation of this login interface is relatively simple, but the verification code and student password are set and encrypted with MD5! ! The use of these two should be the soul of the system.

And what is MD5 encryption?

MD5 overview:
MD5 message digest algorithm belongs to the category of Hash algorithm. The MD5 algorithm operates on an input message of any length to generate a 128-bit message digest (32-bit alphanumeric mixed code).

The main features of MD5:
irreversible, the MD5 value of the same data must be the same, and the MD5 value of different data is different

(In theory, an MD5 may indeed correspond to an infinite number of original texts, because MD5 is limited in number and the original text can be innumerable. For example, the mainstream MD5 maps a "byte string of arbitrary length" to a 128-bit large integer That is to say, there are 2^128 possibilities in total, which is about 3.4*10^38. This number is limited, but there are countless possibilities for the original text that can be used for encryption in the world)

Properties of MD5:
1. Compressibility: For data of any length, the length of the calculated MD5 value is fixed (equivalent to overloss compression).

2. Easy to calculate: It is easy to calculate the MD5 value from the original data.

3. Anti-modification: Any modification to the original data, even if only one byte is modified, will result in a very different MD5 value.

4. Weak anti-collision: Knowing the original data and its MD5 value, it is very difficult to find a data with the same MD5 value (that is, forged data).

5. Strong anti-collision: It is very difficult to find two different data so that they have the same MD5 value.

For everyone to understand better, I recommend an article: MD5 encryption in java - zpk-aaron - 博客园 (cnblogs.com)

Administrator (Glogin) interface source code:

package 高校学生管理库;

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.SQLException;

import javax.swing.*;

public class Glogin extends JFrame{
    private static final long serialVersionUID = 1L;
    private Captcha vcode = new Captcha();
    JTextField co;
    //窗口常量
    final int WIDTH = 500;
    final int HEIGHT = 315;
    public Glogin() {
        super("管理员登陆");
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        this.setSize(520,370);
        this.setLocationRelativeTo(null);
        setVisible(true);
        //初始化字体
        font fonts = new font();
        //给盘子1创建对象
        javax.swing.JPanel jpanel_1 = new javax.swing.JPanel();
        jpanel_1.setBounds(0,0,WIDTH,HEIGHT);
        jpanel_1.setLayout(null);//设置布局为空
        //给盘子2创建对象
        javax.swing.JPanel jpanel_2 = new javax.swing.JPanel();
        jpanel_2.setBounds(0,30,WIDTH,100);
        FlowLayout flowLayout = new FlowLayout(FlowLayout.CENTER);
        jpanel_2.setLayout(flowLayout);//设置布局为居中
        jpanel_2.setOpaque(false);//设置盘子为透明
        //给盘子3创建对象
        javax.swing.JPanel jpanel_3 = new javax.swing.JPanel();
        jpanel_3.setBounds(0,50,WIDTH,350);
        jpanel_3.setLayout(null);//设置布局为空
        jpanel_3.setOpaque(false);//设置盘子为透明
        //设置背景图片
        ImageIcon img = new ImageIcon("C:\\Users\\god36c\\OneDrive\\桌面\\图三.jpg");
        JLabel bgimg = new JLabel(img);
        bgimg.setBounds(0,0,img.getIconWidth(),img.getIconHeight());
        //登录界面
        JLabel title = new JLabel("管理员登录界面");
        title.setFont(font.title);
        JLabel l= new JLabel("账号");l.setBounds(80, 50, 60, 40);
        JLabel p= new JLabel("密码");p.setBounds(80, 100, 60, 40);
        JLabel code=new JLabel("验证码");code.setBounds(70, 150, 60, 40);
        JTextField user=new JTextField();user.setBounds(150, 50, 150, 30);
        JPasswordField pass=new JPasswordField();pass.setBounds(150, 100, 150, 30);
        co=new JTextField();co.setBounds(150, 150, 150, 30);
        JButton ok=new JButton("登录");ok.setBounds(180, 220, 70, 30);
        ok.setContentAreaFilled(false);vcode.setBounds(310, 145, 100, 40);

        jpanel_3.add(l);jpanel_3.add(p);jpanel_3.add(code);
        jpanel_3.add(user);jpanel_3.add(co);jpanel_3.add(vcode);
        jpanel_3.add(pass);jpanel_3.add(ok);jpanel_2.add(title);
        jpanel_1.add(bgimg);
        this.add(jpanel_3);this.add(jpanel_2);this.add(jpanel_1);

        ok.addActionListener(new ActionListener() {//监听登录按钮
            public void actionPerformed(ActionEvent e) {
                String jusername=user.getText();
                char s[]=pass.getPassword();
                String jpassword=new String(s);
                String coo=co.getText();
                try {
                    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
                    //加载对应的jdbc驱动
                    String url="jdbc:sqlserver://localhost:1433;DatabaseName=高校学生管理库";
                    //配置连接字符串
                    String user="sa";//sa超级管理员
                    String password="你的密码";//密码
                    Connection conn=DriverManager.getConnection(url,user,password);
                    //创建数据库连接对象
                    Statement st=conn.createStatement();
                    //创建SQL语句执行对象
                    String  strSQL="(Select * from  dbo.GLY where ID1='"+jusername+"'And PAWD1='"+jpassword+"' )";
                    ResultSet rs=st.executeQuery(strSQL);
                    if(coo.isEmpty()) {
                        JOptionPane.showMessageDialog(null, "请输入验证码!","提示消息",JOptionPane.WARNING_MESSAGE);
                    }
                    else {
                        if(!isValidCodeRight()) {
                            JOptionPane.showMessageDialog(null, "验证码错误,请重新输入!","提示消息",JOptionPane.WARNING_MESSAGE);
                        }
                        else {
                            if(rs.next()) {
                                new GLY();
                                closeThis();
                            }
                            else {
                                JOptionPane.showMessageDialog(null,"用户名不存在或密码错误","错误!",JOptionPane.ERROR_MESSAGE);
                            }
                            conn.close();//关闭数据库连接
                        }
                    }
                }
                catch (ClassNotFoundException ex) {
                    System.out.println("没有找到对应的数据库驱动类");
                }
                catch (SQLException ex) {
                    System.out.println("数据库连接或者是数据库操作失败");
                }
            }
        });
    }
    public boolean isValidCodeRight() {
        if(co == null) {
            return false;
        }else if(vcode == null) {
            return true;
        }else if(vcode.getCode() .equals(co.getText())) {
            return true;
        }else
            return false;
    }
    public  void closeThis() {//关闭当前界面
        this.dispose();
    }
}

0555bc96ca1e4d63a57e1816cf423e1c.png

Student (Slogin) interface source code:

package 高校学生管理库;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.*;

public class Slogin extends JFrame {
    private static final long serialVersionUID = 1L;
    private Captcha vcode = new Captcha();
    JTextField co;
    static JTextField user;
    //窗口常量
    final int WIDTH = 500;
    final int HEIGHT = 315;
    public Slogin() {
        super("学生登陆");
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        this.setSize(520,370);
        this.setLocationRelativeTo(null);
        setVisible(true);
        //初始化字体
        font fonts = new font();
        //给盘子1创建对象
        javax.swing.JPanel jpanel_1 = new javax.swing.JPanel();
        jpanel_1.setBounds(0,0,WIDTH,HEIGHT);
        jpanel_1.setLayout(null);//设置布局为空
        //给盘子2创建对象
        javax.swing.JPanel jpanel_2 = new javax.swing.JPanel();
        jpanel_2.setBounds(0,30,WIDTH,100);
        FlowLayout flowLayout = new FlowLayout(FlowLayout.CENTER);//设置布局为居中
        jpanel_2.setLayout(flowLayout);
        jpanel_2.setOpaque(false);//设置盘子为透明
        //给盘子3创建对象
        javax.swing.JPanel jpanel_3 = new javax.swing.JPanel();
        jpanel_3.setBounds(0,50,WIDTH,350);
        jpanel_3.setLayout(null);//设置布局为空
        jpanel_3.setOpaque(false);//设置盘子为透明
        //设置背景图片
        ImageIcon img = new ImageIcon("C:\\Users\\god36c\\OneDrive\\桌面\\图二.jpg");
        JLabel bgimg = new JLabel(img);
        bgimg.setBounds(0,0,img.getIconWidth(),img.getIconHeight());
        //登录界面
        JLabel title = new JLabel("学生登录界面");
        title.setFont(font.title);
        JLabel l= new JLabel("账号");l.setBounds(80, 50, 60, 40);
        JLabel p= new JLabel("密码");p.setBounds(80, 100, 60, 40);
        JLabel code=new JLabel("验证码");code.setBounds(70, 150, 60, 40);
        user=new JTextField();user.setBounds(150, 50, 150, 30);
        JPasswordField pass=new JPasswordField();pass.setBounds(150, 100, 150, 30);
        co=new JTextField();co.setBounds(150, 150, 150, 30);
        JButton ok=new JButton("登录"); ok.setBounds(120, 220, 70, 30);
        ok.setContentAreaFilled(false);
        JButton register=new JButton("注册");register.setBounds(250, 220, 70, 30);
        register.setContentAreaFilled(false);
        vcode.setBounds(310, 145, 100, 40);

        jpanel_3.add(l);jpanel_3.add(p);jpanel_3.add(code);
        jpanel_3.add(user);jpanel_3.add(co);jpanel_3.add(vcode);
        jpanel_3.add(register);jpanel_3.add(pass);jpanel_3.add(ok);
        jpanel_2.add(title);jpanel_1.add(bgimg);
        this.add(jpanel_3);this.add(jpanel_2);this.add(jpanel_1);

        register.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                new Enroll();
                closeThis();
            }
        });
        ok.addActionListener(new ActionListener() {   //监听登录按钮
            public void actionPerformed(ActionEvent e) {
                String jusername=user.getText();
                char s[]=pass.getPassword();
                String jpassword=new String(s);
                String coo=co.getText();
                try {
                    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
                    //加载对应的jdbc驱动
                    String url="jdbc:sqlserver://localhost:1433;DatabaseName=高校学生管理库";
                    //配置连接字符串
                    String user="sa";//sa超级管理员
                    String password="你的密码";//密码
                    Connection conn=DriverManager.getConnection(url,user,password);
                    //创建数据库连接对象
                    Statement st=conn.createStatement();
                    //创建SQL语句执行对象

                    Md5 md5 = new Md5();//使用MD5加密
                    String newString = md5.EncoderByMd5(jpassword);
                    String  strSQL="(Select * from  dbo.PY where ID='"+jusername+"'And PAWD='"+newString+"' )";
                    ResultSet rs=st.executeQuery(strSQL);
                    if(coo.isEmpty()) {
                        JOptionPane.showMessageDialog(null, "请输入验证码!","提示消息",JOptionPane.WARNING_MESSAGE);
                    }
                    else {
                        if(!isValidCodeRight()) {
                            JOptionPane.showMessageDialog(null, "验证码错误,请重新输入!","提示消息",JOptionPane.WARNING_MESSAGE);
                        }
                        else {
                            if(rs.next()) {
                                new Student();
                                closeThis();
                            }
                            else {
                                JOptionPane.showMessageDialog(null,"用户名不存在或密码错误","错误!",JOptionPane.ERROR_MESSAGE);
                            }
                            conn.close();//关闭数据库连接
                        }
                    }
                }
                catch (ClassNotFoundException ex) {
                    System.out.println("没有找到对应的数据库驱动类");
                }
                catch (SQLException ex) {
                    System.out.println("数据库连接或者是数据库操作失败");
                }catch (NoSuchAlgorithmException e1) {
                    e1.printStackTrace();
                } catch (UnsupportedEncodingException e1) {
                    e1.printStackTrace();
                }
            }
        });
    }
    public boolean isValidCodeRight() {
        if(co == null) {
            return false;
        }else if(vcode == null) {
            return true;
        }else if(vcode.getCode() .equals(co.getText())) {
            return true;
        }else
            return false;
    }
    public  void closeThis() {//关闭当前界面
        this.dispose();
    }
}

e47d12e7c2c248f49b34fb49a8744a6a.png

 MD5 class source code:

package 高校学生管理库;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import sun.misc.BASE64Encoder;

public class Md5 {    /**利用MD5进行加密*/
    public String EncoderByMd5(String str) throws NoSuchAlgorithmException, UnsupportedEncodingException{
        //确定计算方法
        MessageDigest md5=MessageDigest.getInstance("MD5");
        BASE64Encoder base64en = new BASE64Encoder();
        //加密后的字符串
        String newstr=base64en.encode(md5.digest(str.getBytes("utf-8")));
        return newstr;
    }
}

Verification code function (Captcha class) source code: case-sensitive, click to replace

package 高校学生管理库;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import java.util.Random;

import javax.swing.JComponent;

public class Captcha extends JComponent implements MouseListener {
    private static final long serialVersionUID = 1L;
    private String code;
    private int width, height = 40;
    private int codeLength = 4;
    private Random random = new Random();
    public Captcha() {
        width = this.codeLength * 16 + (this.codeLength - 1) * 10;
        setPreferredSize(new Dimension(width, height));
        setSize(width, height);
        this.addMouseListener(this);
        setToolTipText("点击可以更换验证码");
    }
    public int getCodeLength() {
        return codeLength;
    }
    //设置验证码文字的长度
    public void setCodeLength(int codeLength) {
        if(codeLength < 4) {
            this.codeLength = 4;
        } else {
            this.codeLength = codeLength;
        }
    }
    public String getCode() {
        return code;
    }
    //产生随机的颜色
    public Color getRandColor(int min, int max) {
        if (min > 255)
            min = 255;
        if (max > 255)
            max = 255;
        int red = random.nextInt(max - min) + min;
        int green = random.nextInt(max - min) + min;
        int blue = random.nextInt(max - min) + min;
        return new Color(red, green, blue);
    }
    //设置验证码具体的字母是什么
    protected String generateCode() {
        char[] codes = new char[this.codeLength];
        for (int i = 0, len = codes.length; i < len; i++) {
            if (random.nextBoolean()) {
                codes[i] = (char) (random.nextInt(26) + 65);
            } else {
                codes[i] = (char) (random.nextInt(26) + 97);
            }
        }
        this.code = new String(codes);
        return this.code;
    }
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if(this.code == null || this.code.length() != this.codeLength) {
            this.code = generateCode();
        }
        width = this.codeLength * 16 + (this.codeLength - 1) * 10;
        super.setSize(width, height);
        super.setPreferredSize(new Dimension(width, height));
        Font mFont = new Font("Arial", Font.BOLD | Font.ITALIC, 25);
        g.setFont(mFont);
        //绘制出验证码的背景的矩形轮廓
        Graphics2D g2d = (Graphics2D) g;
        g2d.setColor(getRandColor(200, 250));
        g2d.fillRect(0, 0, width, height);
        g2d.setColor(getRandColor(180, 200));
        g2d.drawRect(0, 0, width - 1, height - 1);
        //绘制出验证码背景的线
        int i = 0, len = 150;
        for (; i < len; i++) {
            int x = random.nextInt(width - 1);
            int y = random.nextInt(height - 1);
            int x1 = random.nextInt(width - 10) + 10;
            int y1 = random.nextInt(height - 4) + 4;
            g2d.setColor(getRandColor(180, 200));
            g2d.drawLine(x, y, x1, y1);
        }
        //绘制出验证码的具体字母
        i = 0; len = this.codeLength;
        FontMetrics fm = g2d.getFontMetrics();
        int base = (height - fm.getHeight())/2 + fm.getAscent();
        for(;i<len;i++) {
            int b = random.nextBoolean() ? 1 : -1;
            g2d.rotate(random.nextInt(10)*0.01*b);
            g2d.setColor(getRandColor(20, 130));
            g2d.drawString(code.charAt(i)+"", 16 * i + 10, base);
        }
    }
    //下一个验证码
    public void nextCode() {
        generateCode();
        repaint();
    }
    @Override
    public void mouseClicked(MouseEvent e) {
        nextCode();
    }
    @Override
    public void mousePressed(MouseEvent e) {
    }
    @Override
    public void mouseReleased(MouseEvent e) {
    }
    @Override
    public void mouseEntered(MouseEvent e) {
    }
    @Override
    public void mouseExited(MouseEvent e) {
    }
}

Student Enrollment (Enroll class):

Student registration interface:
upload photo function, here I encountered a problem about escape characters, because I want to get the path of the selected picture and add the photo to JLabel to display it, when we need to use ordinary in the string When using backslashes, you need to use double backslashes \\; the java compiler will interpret \\\\ as \\, and in regular expressions, double slashes \\ represent single slashes \, which means That is why \\\\ is used to represent \, and \\\\\\\\ is used to represent \\.

        pho.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JFileChooser fileChooser = new JFileChooser("C:\\Users\\god36c\\OneDrive\\桌面");
                fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
                int returnVal = fileChooser.showOpenDialog(fileChooser);
                if(returnVal == JFileChooser.APPROVE_OPTION) {
                    File filePath = fileChooser.getSelectedFile();//获取图片路径
                    System.out.println(filePath);
                    String f=filePath.getPath();
                    String filePath1=f.replaceAll("\\\\\\\\", "\\\\\\\\");	//将\转义为\\
                    ImageIcon p = new ImageIcon(filePath1);
                    photo.setIcon(p);
                }
            }
        });

Then the table in the database is used here to determine whether the person already exists. If there is a registration failure, if it does not exist and the information is filled in to meet the requirements, it will succeed (the photo will save the path of the photo).

Student registration interface source code:

package 高校学生管理库;

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

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;

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

import javax.swing.*;

public class Enroll extends JFrame{
    private static final long serialVersionUID = 1L;
    JTextField co;
    private Captcha vcode = new Captcha();
    final int WIDTH = 1020;
    final int HEIGHT = 1615;
    public Enroll() {
        super("注册");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(1020,700);
        this.setLocationRelativeTo(null); //此窗口将置于屏幕的中央
        setVisible(true);

        //初始化字体
        font fonts = new font();
        //设置背景图片
        ImageIcon img = new ImageIcon("C:\\Users\\god36c\\OneDrive\\桌面\\图七.jpg");
        JLabel bgimg = new JLabel(img);
        bgimg.setBounds(0,0,img.getIconWidth(),img.getIconHeight());
        bgimg.setOpaque(false);//背景照片透明
        //给盘子1创建对象
        javax.swing.JPanel jpanel_1 = new javax.swing.JPanel();
        jpanel_1.setBounds(0,0,WIDTH,HEIGHT);
        jpanel_1.setLayout(null);//设置布局为空
        jpanel_1.setOpaque(false);
        //给盘子2创建对象
        javax.swing.JPanel jpanel_2 = new javax.swing.JPanel();
        jpanel_2.setBounds(0,30,WIDTH,100);
        FlowLayout flowLayout = new FlowLayout(FlowLayout.CENTER);
        jpanel_2.setLayout(flowLayout);//设置布局为居中
        jpanel_2.setOpaque(false);//设置盘子为透明
        //给盘子3创建对象
        javax.swing.JPanel jpanel_3 = new javax.swing.JPanel();
        jpanel_3.setBounds(0,50,WIDTH,350);
        jpanel_3.setLayout(null);//设置布局为空
        jpanel_3.setOpaque(false);//设置盘子为透明

        JLabel title = new JLabel(" 注 册 界 面 ");
        title.setFont(font.title);
        title.setHorizontalAlignment(SwingConstants.CENTER);//居中

        JLabel username=new JLabel("账号");username.setBounds(58, 46, 60, 40);
        JLabel password=new JLabel("密码");password.setBounds(54,100,120,30);
        JLabel id=new JLabel("学号");id.setBounds(54,150,120,30);
        JLabel phone=new JLabel("手机号码");phone.setBounds(22,200,120,30);
        JLabel code=new JLabel("验证码");code.setBounds(41,250,120,30);
        JLabel photo=new JLabel();photo.setBounds(280,100,200,200);
        JTextField user=new JTextField();user.setBounds(100,50,120,30);
        JTextField pass=new JTextField(); pass.setBounds(100,100,120,30);
        JTextField idd=new JTextField();idd.setBounds(100,150,120,30);
        JTextField ph=new JTextField();ph.setBounds(100,200,120,30);
        co=new JTextField();co.setBounds(100,250,120,30);
        JButton pho=new JButton("上传照片");pho.setBounds(305,280,100,30);
        JButton register=new JButton("注  册"); register.setBounds(150,380,80,30);
        JButton exit=new JButton("退  出");exit.setBounds(250,380,80,30);
        pho.setContentAreaFilled(false);register.setContentAreaFilled(false);exit.setContentAreaFilled(false);
        vcode.setBounds(112, 300, 100, 40);

        jpanel_1.add(username);jpanel_1.add(user);jpanel_1.add(password);
        jpanel_1.add(pass);jpanel_1.add(id);jpanel_1.add(idd);
        jpanel_1.add(phone);jpanel_1.add(ph);jpanel_1.add(code);
        jpanel_1.add(co);jpanel_1.add(photo);jpanel_1.add(pho);
        jpanel_1.add(register);jpanel_1.add(exit);jpanel_1.add(vcode);
        jpanel_1.add(bgimg);this.add(jpanel_1);jpanel_2.add(title);
        this.add(jpanel_2);this.add(jpanel_3);

        pho.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JFileChooser fileChooser = new JFileChooser("C:\\Users\\god36c\\OneDrive\\桌面");
                fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
                int returnVal = fileChooser.showOpenDialog(fileChooser);
                if(returnVal == JFileChooser.APPROVE_OPTION) {
                    File filePath = fileChooser.getSelectedFile();//获取图片路径
                    System.out.println(filePath);
                    String f=filePath.getPath();
                    String filePath1=f.replaceAll("\\\\\\\\", "\\\\\\\\");	//将\转义为\\
                    ImageIcon p = new ImageIcon(filePath1);
                    photo.setIcon(p);
                }
            }
        });
        register.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String user1=user.getText().trim();
                String pass1=pass.getText().trim();
                String id1=idd.getText().trim();
                String ph1=ph.getText().trim();
                String co1=co.getText();
                try {
                    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
                    //加载对应的jdbc驱动
                    String url="jdbc:sqlserver://localhost:1433;DatabaseName=高校学生管理库";
                    //配置连接字符串
                    String user="sa";//sa超级管理员
                    String password="你的密码";//密码
                    Connection conn=DriverManager.getConnection(url,user,password);
                    //创建数据库连接对象
                    Statement st=conn.createStatement();
                    //创建SQL语句执行对象
                    String  strSQL="(Select * from  dbo.PY where ID='"+user1+"' )";
                    ResultSet rs=st.executeQuery(strSQL);
                    Md5 md5 = new Md5();
                    String newString = md5.EncoderByMd5(pass1);
                    if(co1.isEmpty()) {
                        JOptionPane.showMessageDialog(null, "请输入验证码!","提示消息",JOptionPane.WARNING_MESSAGE);
                    }
                    else {
                        if(!isValidCodeRight()) {
                            JOptionPane.showMessageDialog(null, "验证码错误,请重新输入!","提示消息",JOptionPane.WARNING_MESSAGE);
                        }
                        else {
                            if(rs.next()) {
                                JOptionPane.showMessageDialog(null,"用户名已存在","错误!", JOptionPane.ERROR_MESSAGE);
                            }
                            else {
                                String sql = "insert into dbo.PY(ID,PAWD,Sno,phone) values('"+user1+"','"+newString+"','"+id1+"','"+ph1+"')";
                                PreparedStatement pst = conn.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
                                pst.executeUpdate();
                                pst.close();
                                JOptionPane.showMessageDialog(null,"注册成功");
                            }
                            conn.close();//关闭数据库连接
                        }
                    }
                }
                catch (ClassNotFoundException ex) {
                    System.out.println("没有找到对应的数据库驱动类");
                }
                catch (SQLException ex) {
                    System.out.println("数据库连接或者是数据库操作失败");
                }
                catch (NoSuchAlgorithmException e1) {
                    e1.printStackTrace();
                } catch (UnsupportedEncodingException e1) {
                    e1.printStackTrace();
                }
            }
        });
        exit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                closeThis();
                new Slogin();
            }
        });
    }
    public boolean isValidCodeRight() {
        if(co == null) {
            return false;
        }else if(vcode == null) {
            return true;
        }else if(vcode.getCode() .equals(co.getText())) {
            return true;
        }else
            return false;
    }
    public  void closeThis(){//关闭当前界面
        this.dispose();
    }
}

Student class (Student):

In addition, everyone should pay attention to the database link. My database is different from yours, so you should pay attention to whether your database is the same as mine. If it is different, you should change it according to your own database, otherwise the link will not work successful! ! The next class of students and administrators is the core of this system design.

Student's source code:

package 高校学生管理库;

import java.awt.Dimension;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.Statement;
import java.util.Vector;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

public class Student extends JFrame{
    private static final long serialVersionUID = 1L;
    public Student() throws SQLException, ClassNotFoundException
    {
        super("学生登陆");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(800,600);
        this.setLocationRelativeTo(null);
        setVisible(true);

        MenuBar bar = new MenuBar();// 创建菜单栏
        Menu fileMenu = new Menu("FILE");// 创建“文件”菜单
        MenuItem open = new MenuItem("OPEN");
        MenuItem exit = new MenuItem("EXIT");
        Menu help = new Menu("HELP");// 创建“帮助"菜单
        MenuItem print = new MenuItem("PRINT");

        exit.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e)
            {
                new Slogin();
                closeThis();
            }
        });
        //设置背景图片
        ImageIcon img = new ImageIcon("C:\\Users\\god36c\\OneDrive\\桌面\\图八.jpg");
        JLabel bgimg = new JLabel(img);
        bgimg.setBounds(0,0,img.getIconWidth(),img.getIconHeight());
        bgimg.setOpaque(false);//背景照片透明
        //设置背景图片
        ImageIcon img1 = new ImageIcon("C:\\Users\\god36c\\OneDrive\\桌面\\图十.jpg");
        JLabel bgimg1 = new JLabel(img1);
        bgimg1.setBounds(0,0,img1.getIconWidth(),img1.getIconHeight());
        bgimg1.setOpaque(false);//背景照片透明
        //设置背景图片
        ImageIcon img2 = new ImageIcon("C:\\Users\\god36c\\OneDrive\\桌面\\图九.jpg");
        JLabel bgimg2 = new JLabel(img2);
        bgimg2.setBounds(0,0,img2.getIconWidth(),img2.getIconHeight());
        bgimg2.setOpaque(false);//背景照片透明

        fileMenu.add(print);fileMenu.add(open);
        fileMenu.addSeparator();// 设置菜单分隔符
        fileMenu.add(exit);
        bar.add(fileMenu);// 将文件添加到菜单栏上
        bar.add(help);// 将文件添加到菜单栏上
        setMenuBar(bar);// 设置菜单栏,使用这种方式设置菜单栏可以不占用布局空间
        //创建组件
        JPanel jp1= new JPanel();//定义面板
        JPanel jp2= new JPanel();
        JPanel jp3= new JPanel();
        jp1.setLayout(null);//自由布局
        jp2.setLayout(null);//自由布局
        jp3.setLayout(null);//自由布局
        //jp1面板上上的内容
        String[][] datas = {};
        String[] titles = { "学号", "姓名","性别","年龄","专业" };
        String[][] datas1 = {};
        String[] titles1 = { "课程号", "分数","等级"};

        DefaultTableModel myModel  = new DefaultTableModel(datas, titles);// myModel存放表格的数据
        DefaultTableModel myModel1 = new DefaultTableModel(datas1, titles1);
        JTable table  = new JTable(myModel);// 表格对象table的数据来源是myModel对象
        JTable table1 = new JTable(myModel1);
        table.setPreferredScrollableViewportSize(new Dimension(550, 100));// 表格的显示尺寸
        table1.setPreferredScrollableViewportSize(new Dimension(550, 100));
        // 产生一个带滚动条的面板
        JScrollPane scrollPane = new JScrollPane(table);scrollPane.setBounds(50, 190, 550, 70);
        JScrollPane scrollPane1 = new JScrollPane(table1);scrollPane1.setBounds(50, 290, 550, 100);
        //行高
        table.setRowHeight(20);
        table1.setRowHeight(20);

        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        //加载对应的jdbc驱动
        String url="jdbc:sqlserver://localhost:1433;DatabaseName=高校学生管理库";
        //配置连接字符串
        String user="sa";//sa超级管理员
        String password="123456";//密码
        Connection conn=DriverManager.getConnection(url,user,password);
        //创建数据库连接对象
        Statement st=conn.createStatement();
        //创建SQL语句执行对象

        String  strSQL="(Select * from  dbo.Student where Sname='"+ Slogin.user.getText()+"')";
        ResultSet rs=st.executeQuery(strSQL);
        if(rs.next()) {
            Vector<String> ve = new Vector<String>();
            ve.addElement(rs.getString(1));ve.addElement(rs.getString(2));
            ve.addElement(rs.getString(3));ve.addElement(rs.getString(4));
            ve.addElement(rs.getString(5));
            myModel.addRow(ve);
        }
        String  s1="(Select * from dbo.Student,dbo.SC where Sname='"+ Slogin.user.getText()+"' And Student.Sno=SC.Sno)";
        ResultSet r1=st.executeQuery(s1);
        while(r1.next()) {
            Vector<String> ve1 = new Vector<String>();
            ve1.addElement(r1.getString(7));
            ve1.addElement(r1.getString(8));
            ve1.addElement(r1.getString(9));
            myModel1.addRow(ve1);
        }
        JButton again=new JButton("刷 新~");again.setBounds(490, 140, 80, 30);
        again.setContentAreaFilled(false);
        again.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                try {
                    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
                    //加载对应的jdbc驱动
                    String url="jdbc:sqlserver://localhost:1433;DatabaseName=高校学生管理库";
                    //配置连接字符串
                    String user="sa";//sa超级管理员
                    String password="你的密码";//密码
                    Connection conn=DriverManager.getConnection(url,user,password);
                    Statement st=conn.createStatement();
                    while(myModel1.getRowCount()>0) {
                        myModel1.removeRow(myModel1.getRowCount()-1);
                    }
                    String  s1="(Select * from dbo.Student,dbo.SC where Sname='"+ Slogin.user.getText()+"' And Student.Sno=SC.Sno)";
                    ResultSet r1=st.executeQuery(s1);
                    while(r1.next()) {
                        Vector<String> ve1 = new Vector<String>();
                        ve1.addElement(r1.getString(7));
                        ve1.addElement(r1.getString(8));
                        ve1.addElement(r1.getString(9));
                        myModel1.addRow(ve1);
                    }
                    conn.close();
                }catch (ClassNotFoundException ex) {
                    System.out.println("没有找到对应的数据库驱动类");
                }
                catch (SQLException ex) {
                    System.out.println("数据库连接或者是数据库操作失败");
                }
            }
        });
        //jp2面板上的内容
        String[][] datas2 = {};
        String[] titles2 = { "课程号", "课程名","学分" };
        DefaultTableModel myModel2 = new DefaultTableModel(datas2, titles2);
        JTable table2  = new JTable(myModel2);
        table2.setRowHeight(20);
        table2.setPreferredScrollableViewportSize(new Dimension(550, 400));
        JScrollPane scrollPane2 = new JScrollPane(table2);scrollPane2.setBounds(50, 20, 550, 400);
        String  s2="(Select * from dbo.Course)";
        ResultSet r2=st.executeQuery(s2);
        while(r2.next()) {
            Vector<String> ve2 = new Vector<String>();
            ve2.addElement(r2.getString(1));
            ve2.addElement(r2.getString(2));
            ve2.addElement(r2.getString(4));
            myModel2.addRow(ve2);
        }
        conn.close();
        //jp2上的内容
        JLabel a=new JLabel("请输入你想选的课的课程号:");a.setBounds(50, 470, 270, 30);
        JTextField b=new JTextField(20);b.setBounds(320, 470, 150, 25);
        JButton c=new JButton("确定");c.setBounds(500, 470, 80, 27);
        //jp3上的内容
        JLabel ja1=new JLabel("你想查询的科目是:");ja1.setBounds(50, 50, 200, 30);
        JLabel ja2=new JLabel("你的成绩是:");ja2.setBounds(80, 220, 150, 30);
        JLabel ja3=new JLabel("你的等级是:");ja3.setBounds(80, 270, 150, 30);
        JLabel ja4=new JLabel("(输入课程号哦~)");ja4.setBounds(255, 80, 150, 30);
        JTextField b1=new JTextField(15);b1.setBounds(260, 50, 150, 25);
        JTextField b2=new JTextField(15);b2.setBounds(260, 220, 100, 25);
        JTextField b3=new JTextField(15);b3.setBounds(260, 270, 100, 25);
        JButton c1=new JButton("查 询");c1.setBounds(450, 50, 70, 30);
        // 将组件添加入jp1窗口中
        jp1.add(scrollPane);jp1.add(scrollPane1);
        jp1.add(again);jp1.add(bgimg);
        // 将组件添加入jp2窗口中
        jp2.add(scrollPane2);jp2.add(a);jp2.add(b);
        jp2.add(c);jp2.add(bgimg1);
        // 将组件添加入jp3窗口中
        jp3.add(ja1);jp3.add(ja2);jp3.add(ja3);
        jp3.add(ja4);jp3.add(b1);jp3.add(b2);
        jp3.add(b3);jp3.add(c1);jp3.add(bgimg2);
        JTabbedPane jtbp=new JTabbedPane(JTabbedPane.LEFT); //创建选项卡并使选项卡垂直排列
        jtbp.add("个人信息",jp1);jtbp.add("选课",jp2);jtbp.add("成绩查询",jp3);
        this.add(jtbp);    //添加选项卡窗格到容器
        c.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e)
            {
                try {
                    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
                    //加载对应的jdbc驱动
                    String url="jdbc:sqlserver://localhost:1433;DatabaseName=高校学生管理库";
                    //配置连接字符串
                    String user="sa";//sa超级管理员
                    String password="你的密码";//密码
                    Connection conn=DriverManager.getConnection(url,user,password);
                    Statement st=conn.createStatement();
                    String ok=b.getText().trim();
                    String s="(Select * from dbo.Course where Cno='"+ok+"' )";
                    ResultSet r=st.executeQuery(s);
                    if(r.next()) {
                        String s1="(Select * from dbo.Student,dbo.SC where Sname='"+ Slogin.user.getText()+"' And SC.Sno=Student.Sno )";
                        ResultSet r1=st.executeQuery(s1);
                        if(r1.next()) {
                            String s2="(Select * from dbo.SC where Sno='"+r1.getString(1)+"' And Cno='"+ok+"' )";
                            ResultSet r2=st.executeQuery(s2);
                            if(r2.next()) {
                                JOptionPane.showMessageDialog(null, "你已经选过该科目了~","提示消息",JOptionPane.WARNING_MESSAGE);
                            }
                            else {
                                String ss="(Select * from dbo.Student where Sname='"+ Slogin.user.getText()+"')";
                                ResultSet rr=st.executeQuery(ss);
                                if(rr.next()) {
                                    String  strSQL="insert into dbo.SC(Sno,Cno) values('"+rr.getString(1)+"','"+ok+"')";
                                    int rr1=st.executeUpdate(strSQL);
                                    if(rr1==1) {
                                        JOptionPane.showMessageDialog(null, "选课成功","提示消息",JOptionPane.WARNING_MESSAGE);
                                    }
                                }
                            }
                        }
                    }
                    else {
                        JOptionPane.showMessageDialog(null, "没有这种科目哦~","提示消息",JOptionPane.WARNING_MESSAGE);
                    }
                    conn.close();
                }catch (ClassNotFoundException ex) {
                    System.out.println("没有找到对应的数据库驱动类");
                }
                catch (SQLException ex) {
                    System.out.println("数据库连接或者是数据库操作失败");
                }
            }
        });
        c1.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                try {
                    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
                    //加载对应的jdbc驱动
                    String url="jdbc:sqlserver://localhost:1433;DatabaseName=高校学生管理库";
                    //配置连接字符串
                    String user="sa";//sa超级管理员
                    String password="你的密码";//密码
                    Connection conn=DriverManager.getConnection(url,user,password);
                    Statement st=conn.createStatement();

                    String B1=b1.getText().trim();
                    String L="(Select * from dbo.Student,dbo.SC where Sname='"+ Slogin.user.getText()+"' And Cno='"+B1+"' And Student.Sno=SC.Sno )";
                    ResultSet M=st.executeQuery(L);
                    if(M.next()) {
                        b2.setText(M.getString(8));
                        b3.setText(M.getString(9));
                    }
                    else {
                        JOptionPane.showMessageDialog(null, "没有该科目的成绩哦~","提示消息",JOptionPane.WARNING_MESSAGE);
                    }
                    conn.close();
                }catch (ClassNotFoundException ex) {
                    System.out.println("没有找到对应的数据库驱动类");
                }
                catch (SQLException ex) {
                    System.out.println("数据库连接或者是数据库操作失败");
                }
            }
        });
    }
    public  void closeThis() {//关闭当前界面
        this.dispose();
    }
}

a9cb386529474543a814288b946150e5.png

73e265aad61441e197a3d33d09ebebda.png 

 614ef65dc4fe46f7a85bac1d1f6d606d.png

 Administrator (GLY) class:

This administrator class is mainly for display and query functions. Of course, there are still some components on the administrator, jumps for additions, deletions, and modifications, and we can query all, or query by student number, and display various, professional , but it should be remembered that if you have not registered a student account, then you can only query by student number, the selected course and course number, etc., you cannot query other information of the student, simply put That is, you can only query the information of the bottom two groups, but if you write according to my code, there will be no problem, but if you add some functions by yourself, then your Where the database columns will change, you have to adjust and change those columns yourself, otherwise, it will not be displayed! !

GLY source code;

package 高校学生管理库;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

public class GLY extends JFrame  {
    private static final long serialVersionUID = 1L;
    public GLY()
    {
        super("管理员登录");
        JPanel A=new JPanel();
        JPanel B=new JPanel();
        JSplitPane jSplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,A,B);//分屏的方式:左右HORIZONTAL_SPLIT,上下VERTICAL_SPLIT
        jSplitPane.setDividerLocation(150);
        this.add(jSplitPane);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(1020,700);
        this.setLocationRelativeTo(null);
        setVisible(true);
        //设置背景图片
        ImageIcon img = new ImageIcon("C:\\Users\\god36c\\OneDrive\\桌面\\图十一.jpg");
        JLabel bgimg = new JLabel(img);
        bgimg.setBounds(0,0,img.getIconWidth(),img.getIconHeight());
        bgimg.setOpaque(false);//背景照片透明
        //设置背景图片
        ImageIcon img1 = new ImageIcon("C:\\Users\\god36c\\OneDrive\\桌面\\图十二.jpg");
        JLabel bgimg1 = new JLabel(img1);
        bgimg1.setBounds(0,0,img1.getIconWidth(),img1.getIconHeight());
        bgimg1.setOpaque(false);//背景照片透明
        //生成A窗体中的各种组件
        A.setLayout(null);//自由布局
        JButton component1=new JButton("添加");component1.setBounds(20, 30, 70, 30);
        JButton component2=new JButton("修改");component2.setBounds(105, 30, 70, 30);
        JButton component3=new JButton("删除");component3.setBounds(20, 100, 70, 30);
        JButton component4=new JButton("退出");component4.setBounds(105, 100, 70, 30);//增删改退按钮
        JLabel component5=new JLabel("请输入学号:");component5.setBounds(395, 30, 100, 25);
        JTextField component6=new JTextField(20);component6.setBounds(500, 30, 200, 25);//后面的输入框
        JButton component7=new JButton("查询");component7.setBounds(715, 27, 80, 50);//查询按钮
        JButton component8=new JButton("全 部 信 息 展 示");component8.setBounds(460, 85, 230, 50);
        component1.setContentAreaFilled(false);component2.setContentAreaFilled(false);
        component3.setContentAreaFilled(false);component4.setContentAreaFilled(false);
        component7.setContentAreaFilled(false);component8.setContentAreaFilled(false);
        //把所有组件加入窗体对象中去
        A.add(component1);A.add(component2);A.add(component3);
        A.add(component4);A.add(component5);A.add(component6);
        A.add(component7);A.add(component8);A.add(bgimg);
        //四个表格
        String[][] datas = {};
        String[] titles = { "学号", "姓名","性别","年龄","专业" };
        String[][] datas1 = {};
        String[] titles1 = { "课程号", "课程名","学分"};
        String[][] datas2 = {};
        String[] titles2 = { "学号", "课程号","成绩","等级"};
        String[][] datas3 = {};
        String[] titles3 = { "账号", "密码","学号","电话号码"};
        DefaultTableModel myModel  = new DefaultTableModel(datas, titles);// myModel存放表格的数据
        DefaultTableModel myModel1 = new DefaultTableModel(datas1, titles1);
        DefaultTableModel myModel2 = new DefaultTableModel(datas2, titles2);
        DefaultTableModel myModel3 = new DefaultTableModel(datas3, titles3);
        JTable table  = new JTable(myModel);table.setPreferredScrollableViewportSize(new Dimension(700, 100));// 表格的显示尺寸// 表格对象table的数据来源是myModel对象
        JTable table1 = new JTable(myModel1);table1.setPreferredScrollableViewportSize(new Dimension(700, 100));
        JTable table2 = new JTable(myModel2);table2.setPreferredScrollableViewportSize(new Dimension(700, 100));
        JTable table3 = new JTable(myModel3);table3.setPreferredScrollableViewportSize(new Dimension(700, 100));
        // 产生一个带滚动条的面板
        JScrollPane scrollPane = new JScrollPane(table);scrollPane.setBounds(170, 110, 700, 100);
        JScrollPane scrollPane1 = new JScrollPane(table1);scrollPane1.setBounds(170, 220, 700, 100);
        JScrollPane scrollPane2 = new JScrollPane(table2);scrollPane2.setBounds(170, 330, 700, 100);
        JScrollPane scrollPane3 = new JScrollPane(table3);scrollPane3.setBounds(170, 0, 700, 100);
        // 将带滚动条的面板添加入B窗口中
        B.setLayout(null);//自由布局
        B.add(scrollPane);B.add(scrollPane1);B.add(scrollPane2);
        B.add(scrollPane3);B.add(bgimg1);

        component1.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e)
            {
                new Add();
            }
        });
        component2.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e)
            {
                new Update();
            }
        });
        component3.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e)
            {
                new Delete();
            }
        });
        component4.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                new Glogin();
                closeThis();
            }
        });
        component7.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                try {
                    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
                    //加载对应的jdbc驱动
                    String url="jdbc:sqlserver://localhost:1433;DatabaseName=高校学生管理库";
                    //配置连接字符串
                    String user="sa";//sa超级管理员
                    String password="你的密码";//密码
                    Connection conn=DriverManager.getConnection(url,user,password);
                    //创建数据库连接对象
                    Statement st=conn.createStatement();
                    //创建SQL语句执行对象
                    while(myModel.getRowCount()>0) {
                        myModel.removeRow(myModel.getRowCount()-1);
                    }
                    while(myModel1.getRowCount()>0) {
                        myModel1.removeRow(myModel1.getRowCount()-1);
                    }
                    while(myModel2.getRowCount()>0) {
                        myModel2.removeRow(myModel2.getRowCount()-1);
                    }
                    while(myModel3.getRowCount()>0) {
                        myModel3.removeRow(myModel3.getRowCount()-1);
                    }//清空表上的东西
                    String ID=component6.getText().trim();
                    String  s="(Select * from dbo.Student,dbo.PY where Student.Sno='"+ID+"' And Student.Sno=PY.Sno)";
                    ResultSet r=st.executeQuery(s);
                    while(r.next()) {
                        Vector<String> ve = new Vector<String>();
                        ve.addElement(r.getString(1));ve.addElement(r.getString(2));
                        ve.addElement(r.getString(3));ve.addElement(r.getString(4));
                        ve.addElement(r.getString(5));
                        myModel.addRow(ve);
                        Vector<String> ve1 = new Vector<String>();
                        ve1.addElement(r.getString(6));ve1.addElement(r.getString(7));
                        ve1.addElement(r.getString(8));ve1.addElement(r.getString(9));
                        myModel3.addRow(ve1);
                    }
                    String  s1="(Select * from dbo.SC,dbo.Course where Sno='"+ID+"' And SC.Cno=Course.Cno)";
                    ResultSet r1=st.executeQuery(s1);
                    while(r1.next()) {
                        Vector<String> ve1 = new Vector<String>();
                        ve1.addElement(r1.getString(1));ve1.addElement(r1.getString(2));
                        ve1.addElement(r1.getString(3));ve1.addElement(r1.getString(4));
                        myModel2.addRow(ve1);
                        Vector<String> ve2 = new Vector<String>();
                        ve2.addElement(r1.getString(5));ve2.addElement(r1.getString(6));
                        ve2.addElement(r1.getString(8));
                        myModel1.addRow(ve2);
                    }
                    conn.close();
                }catch (ClassNotFoundException ex) {
                    System.out.println("没有找到对应的数据库驱动类");
                }
                catch (SQLException ex) {
                    System.out.println("数据库连接或者是数据库操作失败");
                }
            }
        });
        component8.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                try {
                    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
                    //加载对应的jdbc驱动
                    String url="jdbc:sqlserver://localhost:1433;DatabaseName=高校学生管理库";
                    //配置连接字符串
                    String user="sa";//sa超级管理员
                    String password="你的密码";//密码
                    Connection conn=DriverManager.getConnection(url,user,password);
                    //创建数据库连接对象
                    Statement st=conn.createStatement();
                    //创建SQL语句执行对象
                    while(myModel.getRowCount()>0) {
                        myModel.removeRow(myModel.getRowCount()-1);
                    }
                    while(myModel1.getRowCount()>0) {
                        myModel1.removeRow(myModel1.getRowCount()-1);
                    }
                    while(myModel2.getRowCount()>0) {
                        myModel2.removeRow(myModel2.getRowCount()-1);
                    }
                    while(myModel3.getRowCount()>0) {
                        myModel3.removeRow(myModel3.getRowCount()-1);
                    }
                    String  strSQL="(Select * from  dbo.Student)";
                    ResultSet rs=st.executeQuery(strSQL);
                    while(rs.next()) {
                        Vector<String> v = new Vector<String>();
                        v.addElement(rs.getString(1));v.addElement(rs.getString(2));
                        v.addElement(rs.getString(3));v.addElement(rs.getString(4));
                        v.addElement(rs.getString(5));
                        myModel.addRow(v);
                    }
                    String  strSQL1="(Select * from  dbo.Course)";
                    ResultSet rs1=st.executeQuery(strSQL1);
                    while(rs1.next()) {
                        Vector<String> v1 = new Vector<String>();
                        v1.addElement(rs1.getString(1));v1.addElement(rs1.getString(2));
                        v1.addElement(rs1.getString(4));
                        myModel1.addRow(v1);
                    }
                    String  strSQL2="(Select * from  dbo.SC)";
                    ResultSet rs2=st.executeQuery(strSQL2);
                    while(rs2.next()) {
                        Vector<String> v2 = new Vector<String>();
                        v2.addElement(rs2.getString(1));v2.addElement(rs2.getString(2));
                        v2.addElement(rs2.getString(3));v2.addElement(rs2.getString(4));
                        myModel2.addRow(v2);
                    }
                    String  strSQL3="(Select * from  dbo.PY)";
                    ResultSet rs3=st.executeQuery(strSQL3);
                    while(rs3.next()) {
                        Vector<String> v3 = new Vector<String>();
                        v3.addElement(rs3.getString(1));v3.addElement(rs3.getString(2));
                        v3.addElement(rs3.getString(3));v3.addElement(rs3.getString(4));
                        myModel3.addRow(v3);
                    }
                    conn.close();
                }catch (ClassNotFoundException ex) {
                    System.out.println("没有找到对应的数据库驱动类");
                }
                catch (SQLException ex) {
                    System.out.println("数据库连接或者是数据库操作失败");
                }
            }
        });
    }
    public  void closeThis() {
        this.dispose();
    }
}

4ee5d45f500645ad99d7bdb890196312.png

 Addition, deletion and modification (Add, Update, Delete):

As for adding, deleting, and modifying the content of these three parts, it is actually relatively simple, so I won’t say much again, I can take a good look at you, it’s easy to understand! !

Add source code:

package 高校学生管理库;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.*;

public class Add extends JFrame{
    private static final long serialVersionUID = 1L;
    final int WIDTH = 550;
    final int HEIGHT = 315;
    public Add() {
        super("添加信息");
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        this.setSize(1020,700);
        this.setLocationRelativeTo(null);
        setVisible(true);

        //初始化字体
        font fonts = new font();
        //设置背景图片
        ImageIcon img = new ImageIcon("C:\\Users\\god36c\\OneDrive\\桌面\\图四.jpg");
        JLabel bgimg = new JLabel(img);
        bgimg.setBounds(0,0,img.getIconWidth(),img.getIconHeight());
        bgimg.setOpaque(false);//背景照片透明
        //给盘子1创建对象
        javax.swing.JPanel jpanel_1 = new javax.swing.JPanel();
        jpanel_1.setBounds(0,0,WIDTH,HEIGHT);
        jpanel_1.setLayout(null);//设置布局为空
        //给盘子2创建对象
        javax.swing.JPanel jpanel_2 = new javax.swing.JPanel();
        jpanel_2.setBounds(0,30,WIDTH,100);
        FlowLayout flowLayout = new FlowLayout(FlowLayout.CENTER);
        jpanel_2.setLayout(flowLayout);//设置布局为居中
        jpanel_2.setOpaque(false);//设置盘子为透明
        //给盘子3创建对象
        javax.swing.JPanel jpanel_3 = new javax.swing.JPanel();
        jpanel_3.setBounds(0,50,WIDTH,350);
        jpanel_3.setLayout(null);//设置布局为空
        jpanel_3.setOpaque(false);//设置盘子为透明

        JLabel title = new JLabel(" 添 加 界 面 ");
        title.setFont(font.title);
        title.setHorizontalAlignment(SwingConstants.CENTER);//居中

        JLabel j=new JLabel("学号:");JTextField c=new JTextField(15);//学号
        j.setBounds(40, 30,  50, 20); c.setBounds(120, 30, 100, 25);
        JLabel j1=new JLabel("姓名:");JTextField c1=new JTextField(15);//姓名
        j1.setBounds(40, 70, 50, 20);c1.setBounds(120, 70, 100, 25);
        JLabel j2=new JLabel("性别:");JTextField c2=new JTextField(15);//性别
        j2.setBounds(40, 110, 50, 30);c2.setBounds(120, 110, 100, 25);
        JLabel j3=new JLabel("年龄:");JTextField c3=new JTextField(15);//年龄
        j3.setBounds(40, 150, 50, 30);c3.setBounds(120, 150, 100, 25);
        JLabel j4=new JLabel("专业:");JTextField c4=new JTextField(15);//专业
        j4.setBounds(40, 190, 50, 30);c4.setBounds(120, 190, 100, 25);
        JLabel j5=new JLabel("课程:");JTextField c5=new JTextField(15);//课程
        j5.setBounds(40, 230, 50, 30);c5.setBounds(120, 230, 100, 25);
        JLabel j6=new JLabel("成绩:");JTextField c6=new JTextField(15);//成绩
        j6.setBounds(40, 270, 50, 30);c6.setBounds(120, 270, 100, 25);
        JLabel j7=new JLabel("等级:");JTextField c7=new JTextField(15);//等级
        j7.setBounds(40, 310, 50, 30); c7.setBounds(120, 310, 100, 25);
        JLabel j8=new JLabel("(温馨提醒:添加学生信息学号必填哦~,右边一列为课程信息)");j8.setBounds(10, 600, 500, 30);
        JLabel j11=new JLabel("课程号:");JTextField c11=new JTextField(15);//课程号
        j11.setBounds(300, 120, 70, 30); c11.setBounds(380, 120, 100, 25);
        JLabel j12=new JLabel("课程名:");JTextField c12=new JTextField(15);//课程名
        j12.setBounds(300, 170, 70, 30); c12.setBounds(380, 170, 100, 25);
        JLabel j13=new JLabel("学分:");JTextField c13=new JTextField(15);//学分
        j13.setBounds(300, 230, 70, 30); c13.setBounds(380, 230, 100, 25);

        JButton a=new JButton("确定");
        a.setContentAreaFilled(false);
        a.setBounds(100, 400, 100, 30);
        JButton b=new JButton("重置");
        b.setContentAreaFilled(false);
        b.setBounds(300, 400, 100, 30);

        jpanel_3.add(j);  jpanel_3.add(j1); jpanel_3.add(j2);
        jpanel_3.add(j3); jpanel_3.add(j4); jpanel_3.add(j5);
        jpanel_3.add(j6); jpanel_3.add(j7); jpanel_3.add(j11);
        jpanel_3.add(j12);jpanel_3.add(j13);jpanel_1.add(j8);
        jpanel_3.add(c);jpanel_3.add(c1); jpanel_3.add(c2);
        jpanel_3.add(c3);jpanel_3.add(c4); jpanel_3.add(c5);
        jpanel_3.add(c6);jpanel_3.add(c7);jpanel_3.add(c11);
        jpanel_3.add(c12);jpanel_3.add(c13);jpanel_1.add(a);
        jpanel_1.add(b);this.add(jpanel_3);jpanel_2.add(title);
        this.add(jpanel_2);jpanel_1.add(bgimg);this.add(jpanel_1);

        a.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                try {
                    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
                    //加载对应的jdbc驱动
                    String url="jdbc:sqlserver://localhost:1433;DatabaseName=高校学生管理库";
                    //配置连接字符串
                    String user="sa";//sa超级管理员
                    String password="你的密码";//密码
                    Connection conn=DriverManager.getConnection(url,user,password);
                    Statement st=conn.createStatement();

                    String a=c.getText().trim();    String a1=c1.getText().trim();
                    String a2=c3.getText().trim();  String a3=c5.getText().trim();
                    String a4=c6.getText().trim();  String a11=c11.getText().trim();
                    String a12=c12.getText().trim();String a13=c13.getText().trim();
                    String a5=c2.getText().trim();  String a6=c4.getText().trim();
                    String a7=c7.getText().trim();

                    String  s="(Select * from dbo.Student where Sno='"+a+"')";
                    ResultSet r=st.executeQuery(s);
                    if(r.next()) {
                        JOptionPane.showMessageDialog(null, "该同学已存在哦~","提示消息",JOptionPane.WARNING_MESSAGE);
                    }
                    else {
                        if(a.equals("")) {
                            String  s3="insert into dbo.Course(Cno,Cname,Ccredit) values('"+a11+"','"+a12+"','"+a13+"')";
                            int r3=st.executeUpdate(s3);
                            if(r3==1) {
                                JOptionPane.showMessageDialog(null, "添加成功","提示消息",JOptionPane.WARNING_MESSAGE);
                            }
                        }
                        else {
                            String  s1="insert into dbo.Student(Sno,Sname,Ssex,Sage,Sdept) values('"+a+"','"+a1+"','"+a5+"','"+a2+"','"+a6+"')";
                            int r1=st.executeUpdate(s1);
                            String  s2="insert into dbo.SC(Sno,Cno,Grade,LEVEL) values('"+a+"','"+a3+"','"+a4+"','"+a7+"')";
                            int r2=st.executeUpdate(s2);
                            if(r1==1&&r2==1) {
                                JOptionPane.showMessageDialog(null, "添加成功","提示消息",JOptionPane.WARNING_MESSAGE);
                            }
                        }
                    }
                    conn.close();
                }catch (ClassNotFoundException ex) {
                    System.out.println("没有找到对应的数据库驱动类");
                }
                catch (SQLException ex) {
                    System.out.println("数据库连接或者是数据库操作失败");
                }
            }
        });
        b.addActionListener(new ActionListener(){//重置清零
            public void actionPerformed(ActionEvent e) {
                c.setText("");c1.setText("");c2.setText("");
                c3.setText("");c4.setText("");c5.setText("");
                c6.setText("");c11.setText("");c12.setText("");
                c13.setText("");
            }
        });
    }
}

5630e8e67903422abe5e04c102e4426e.png

 Delete source code:

package 高校学生管理库;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.*;

public class Delete extends JFrame{
    private static final long serialVersionUID = 1L;
    final int WIDTH = 500;
    final int HEIGHT = 315;
    public Delete() {
        super("删除信息");
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        this.setSize(1020,700);
        this.setLocationRelativeTo(null);
        setVisible(true);

        //初始化字体
        font fonts = new font();
        //设置背景图片
        ImageIcon img = new ImageIcon("C:\\Users\\god36c\\OneDrive\\桌面\\图五.jpg");
        JLabel bgimg = new JLabel(img);
        bgimg.setBounds(0,0,img.getIconWidth(),img.getIconHeight());
        bgimg.setOpaque(false);//将面板设置为透明
        //给盘子1创建对象
        javax.swing.JPanel jpanel_1 = new javax.swing.JPanel();
        jpanel_1.setBounds(0,0,WIDTH,HEIGHT);
        jpanel_1.setLayout(null);//设置布局为空
        jpanel_1.setOpaque(false);
        //给盘子2创建对象
        javax.swing.JPanel jpanel_2 = new javax.swing.JPanel();
        jpanel_2.setBounds(0,30,WIDTH,100);
        FlowLayout flowLayout = new FlowLayout(FlowLayout.CENTER);
        jpanel_2.setLayout(flowLayout);//设置布局为居中
        jpanel_2.setOpaque(false);//设置盘子为透明
        //给盘子3创建对象
        javax.swing.JPanel jpanel_3 = new javax.swing.JPanel();
        jpanel_3.setBounds(0,50,WIDTH,350);
        jpanel_3.setLayout(null);//设置布局为空
        jpanel_3.setOpaque(false);//设置盘子为透明

        JLabel title = new JLabel(" 删 除 界 面 ");
        title.setFont(font.title);
        title.setHorizontalAlignment(SwingConstants.CENTER);//居中

        JCheckBox optionA=new JCheckBox(" 姓名");optionA.setBounds(80, 70, 150, 20);
        JCheckBox optionB=new JCheckBox(" 性别");optionB.setBounds(80, 120, 150, 20);
        JCheckBox optionC=new JCheckBox(" 年龄");optionC.setBounds(80, 170, 150, 20);
        JCheckBox optionD=new JCheckBox(" 专业");optionD.setBounds(80, 220, 150, 20);
        JCheckBox optionE=new JCheckBox(" 成绩");optionE.setBounds(80, 320, 150, 20);
        JCheckBox optionF=new JCheckBox(" 等级");optionF.setBounds(80, 370, 150, 20);
        optionA.setContentAreaFilled(false);optionB.setContentAreaFilled(false);
        optionC.setContentAreaFilled(false);optionD.setContentAreaFilled(false);
        optionE.setContentAreaFilled(false);optionF.setContentAreaFilled(false);
        JLabel j=new JLabel("学号:");j.setBounds(30, 20,  50, 20);
        JLabel  j1=new JLabel("(温馨提醒:谨慎删除哦~)");j1.setBounds(10, 600, 450, 15);
        JLabel j11=new JLabel("课程号:");j11.setBounds(300, 150,  70, 30);
        JLabel j12=new JLabel("课程名:");j12.setBounds(300, 250,  70, 30);
        JLabel cc=new JLabel("课程号:");cc.setBounds(20, 270, 70, 20);
        JTextField c=new JTextField(12);c.setBounds(90, 270, 150, 25);
        JTextField c1=new JTextField(12);c1.setBounds(90, 20, 150, 25);
        JTextField c11=new JTextField(10);c11.setBounds(380, 150, 100, 25);
        JTextField c12=new JTextField(10);c12.setBounds(380, 250, 100, 25);

        JButton a=new JButton("确定");a.setBounds(100, 400, 100, 30);
        a.setContentAreaFilled(false);
        JButton b=new JButton("重置");b.setBounds(300, 400, 100, 30);
        b.setContentAreaFilled(false);

        jpanel_1.add(a);jpanel_1.add(b);jpanel_3.add(j11);
        jpanel_3.add(c);jpanel_3.add(cc); jpanel_3.add(c12); jpanel_1.add(j1);
        jpanel_3.add(j);jpanel_3.add(c1);jpanel_3.add(c11); jpanel_3.add(j12);
        jpanel_3.add(optionA);jpanel_3.add(optionB);jpanel_3.add(optionC);
        jpanel_3.add(optionD);jpanel_3.add(optionE);jpanel_3.add(optionF);
        this.add(jpanel_3);jpanel_2.add(title);this.add(jpanel_2);
        jpanel_1.add(bgimg);this.add(jpanel_1);

        a.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                try {
                    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
                    //加载对应的jdbc驱动
                    String url="jdbc:sqlserver://localhost:1433;DatabaseName=高校学生管理库";
                    //配置连接字符串
                    String user="sa";//sa超级管理员
                    String password="你的密码";//密码
                    Connection conn=DriverManager.getConnection(url,user,password);
                    //创建数据库连接对象
                    Statement st=conn.createStatement();
                    //创建SQL语句执行对象
                    String a=c1.getText().trim();
                    String a1=c.getText().trim();
                    String  s="(Select * from dbo.Student where Sno='"+a+"')";
                    ResultSet r=st.executeQuery(s);
                    if(r.next()) {
                        if(optionA.isSelected()) {
                            String strSQL="update dbo.Student set Sname="+null+" where Sno='"+a+"'";
                            st.executeUpdate(strSQL);
                        }
                        if(optionB.isSelected()) {
                            String strSQL="update dbo.Student set Ssex="+null+" where Sno='"+a+"'";
                            st.executeUpdate(strSQL);
                        }
                        if(optionC.isSelected()) {
                            String strSQL="update dbo.Student set Sage="+null+" where Sno='"+a+"'";
                            st.executeUpdate(strSQL);
                        }
                        if(optionD.isSelected()) {
                            String strSQL="update dbo.Student set Sdept="+null+" where Sno='"+a+"'";
                            st.executeUpdate(strSQL);
                        }
                        if(optionE.isSelected()) {
                            String strSQL="update dbo.SC set Grade="+null+" where Sno='"+a+"' And Cno='"+a1+"'";
                            st.executeUpdate(strSQL);
                        }
                        if(optionF.isSelected()) {
                            String strSQL="update dbo.SC set LEVEL="+null+" where Sno='"+a+"' And Cno='"+a1+"'";
                            st.executeUpdate(strSQL);
                        }
                        JOptionPane.showMessageDialog(null,"删除成功哦~");
                    }
                    else {
                        String cc=c11.getText().trim();
                        String strSQL="delete from dbo.Course where Cno='"+cc+"' ";
                        int rr=st.executeUpdate(strSQL);
                        if(rr==1) {
                            JOptionPane.showMessageDialog(null,"删除成功哦~");
                        }
                        else {
                            JOptionPane.showMessageDialog(null,"课程不存在哦~");
                        }
                    }
                    conn.close();
                }catch (ClassNotFoundException ex) {
                    System.out.println("没有找到对应的数据库驱动类");
                }
                catch (SQLException ex) {
                    System.out.println("数据库连接或者是数据库操作失败");
                }
            }
        });
        b.addActionListener(new ActionListener(){//重置清零
            public void actionPerformed(ActionEvent e) {
                c1.setText("");c11.setText("");c12.setText("");
                optionA.setSelected(false);optionB.setSelected(false);optionC.setSelected(false);
                optionD.setSelected(false);optionE.setSelected(false);optionF.setSelected(false);
            }
        });
    }
}

da9edeb3ef404cf5a060c396eaec6797.png

Update source code:

package 高校学生管理库;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.*;

public class Update extends JFrame{
    private static final long serialVersionUID = 1L;
    final int WIDTH = 550;
    final int HEIGHT = 315;
    public Update() {
        super("修改信息");
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        this.setSize(1020,700);
        this.setLocationRelativeTo(null);
        setVisible(true);

        //初始化字体
        font fonts = new font();
        //设置背景图片
        ImageIcon img = new ImageIcon("C:\\Users\\god36c\\OneDrive\\桌面\\图六.jpg");
        JLabel bgimg = new JLabel(img);
        bgimg.setBounds(0,0,img.getIconWidth(),img.getIconHeight());
        bgimg.setOpaque(false);//将面板设置为透明
        //给盘子1创建对象
        javax.swing.JPanel jpanel_1 = new javax.swing.JPanel();
        jpanel_1.setBounds(0,0,WIDTH,HEIGHT);
        jpanel_1.setLayout(null);//设置布局为空
        jpanel_1.setOpaque(false);//设置盘子为透明
        //给盘子2创建对象
        javax.swing.JPanel jpanel_2 = new javax.swing.JPanel();
        jpanel_2.setBounds(0,30,WIDTH,100);
        FlowLayout flowLayout = new FlowLayout(FlowLayout.CENTER);
        jpanel_2.setLayout(flowLayout);//设置布局为居中
        jpanel_2.setOpaque(false);//设置盘子为透明
        //给盘子3创建对象
        javax.swing.JPanel jpanel_3 = new javax.swing.JPanel();
        jpanel_3.setBounds(0,50,WIDTH,350);
        jpanel_3.setLayout(null);//设置布局为空
        jpanel_3.setOpaque(false);//设置盘子为透明

        JLabel title = new JLabel(" 修 改 界 面 ");
        title.setFont(font.title);
        title.setHorizontalAlignment(SwingConstants.CENTER);//居中

        JLabel j=new JLabel("学号:");j.setBounds(20, 30,  50, 20);
        JTextField c=new JTextField(15);c.setBounds(80, 30, 120, 25);//学号
        JLabel j1=new JLabel("姓名:");j1.setBounds(20, 70, 50, 20);
        JTextField c1=new JTextField(15);c1.setBounds(80, 70, 100, 25);//姓名
        JLabel j2=new JLabel("性别:");j2.setBounds(20, 110, 50, 30);
        JTextField c2=new JTextField(15);c2.setBounds(80, 110, 100, 25);//性别
        JLabel j3=new JLabel("年龄:");j3.setBounds(20, 150, 50, 30);
        JTextField c3=new JTextField(15);c3.setBounds(80, 150, 100, 25);//年龄
        JLabel j4=new JLabel("专业:");j4.setBounds(20, 190, 50, 30);
        JTextField c4=new JTextField(15);c4.setBounds(80, 190, 100, 25);//专业
        JLabel j5=new JLabel("课程:");j5.setBounds(20, 230, 50, 30);
        JTextField c5=new JTextField(20);c5.setBounds(80, 230, 100, 25);//课程
        JLabel j6=new JLabel("成绩:");j6.setBounds(20, 270, 50, 30);
        JTextField c6=new JTextField(20);c6.setBounds(80, 270, 100, 25);//成绩
        JLabel j7=new JLabel("等级:");j7.setBounds(20, 310, 50, 30);
        JTextField c7=new JTextField(15);c7.setBounds(80, 310, 100, 25);//等级
        JLabel j00=new JLabel("旧课程号:");j00.setBounds(290, 100, 90, 30);
        JTextField c00=new JTextField(15);c00.setBounds(380, 100, 100, 25);//旧课程号
        JLabel j11=new JLabel("新课程号:");j11.setBounds(290, 150, 90, 30);
        JTextField c11=new JTextField(15);c11.setBounds(380, 150, 100, 25);//新课程号
        JLabel j12=new JLabel("新课程名:");j12.setBounds(290, 200, 90, 30);
        JTextField c12=new JTextField(15);c12.setBounds(380, 200, 100, 25);//新课程名
        JLabel j13=new JLabel("新学分:");j13.setBounds(300, 250, 90, 30);
        JTextField c13=new JTextField(15);c13.setBounds(380, 250, 100, 25);//新学分
        JLabel j8=new JLabel("(温馨提醒:更新学生信息学号必填哦~右边一列为课程信息)");j8.setBounds(10, 600, 500, 15);

        JButton aa=new JButton("确定");aa.setBounds(100, 400, 100, 30);
        aa.setContentAreaFilled(false);
        JButton bb=new JButton("重置");bb.setBounds(300, 400, 100, 30);
        bb.setContentAreaFilled(false);

        jpanel_3.add(j); jpanel_3.add(j1);jpanel_3.add(j2);
        jpanel_3.add(j3);jpanel_3.add(j4);jpanel_3.add(j5);
        jpanel_3.add(j6);jpanel_3.add(j7);jpanel_3.add(j11);
        jpanel_3.add(j12);jpanel_3.add(j13);jpanel_3.add(c11);
        jpanel_3.add(c12);jpanel_3.add(c13);jpanel_3.add(c);
        jpanel_3.add(c1); jpanel_3.add(c2); jpanel_3.add(c3);
        jpanel_3.add(c4); jpanel_3.add(c5); jpanel_3.add(c6);
        jpanel_3.add(c7); jpanel_1.add(aa); jpanel_1.add(bb);
        jpanel_1.add(j8);jpanel_3.add(c00);jpanel_3.add(j00);
        this.add(jpanel_3);jpanel_2.add(title);this.add(jpanel_2);
        jpanel_1.add(bgimg);this.add(jpanel_1);

        aa.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                try {
                    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
                    //加载对应的jdbc驱动
                    String url="jdbc:sqlserver://localhost:1433;DatabaseName=高校学生管理库";
                    //配置连接字符串
                    String user="sa";//sa超级管理员
                    String password="你的密码";//密码
                    Connection conn=DriverManager.getConnection(url,user,password);
                    Statement st=conn.createStatement();
                    //修改过布局所以顺序有点乱=。=
                    String a=c.getText().trim();String a1=c1.getText().trim();
                    String a2=c3.getText().trim();String a3=c5.getText().trim();
                    String a4=c6.getText().trim();String a11=c11.getText().trim();
                    String a12=c12.getText().trim();String a13=c13.getText().trim();
                    String a5=c2.getText().trim();String a6=c4.getText().trim();String a7=c7.getText().trim();
                    String a00=c00.getText().trim();

                    String  s="(Select * from dbo.Student where Sno='"+a+"')";
                    ResultSet r=st.executeQuery(s);
                    if(r.next()) {
                        if (!a1.isEmpty()) {
                            String  strSQL="update dbo.Student set Sname='"+a1+"' where Sno='"+a+"'";
                            st.executeUpdate(strSQL);
                        }
                        if (!a2.isEmpty()) {
                            String  strSQL="update dbo.Student set Sage='"+a2+"' where Sno='"+a+"'";
                            st.executeUpdate(strSQL);
                        }
                        if (!a5.isEmpty()) {
                            String  strSQL="update dbo.Student set Ssex='"+a5+"' where Sno='"+a+"'";
                            st.executeUpdate(strSQL);
                        }
                        if (!a6.isEmpty()) {
                            String  strSQL="update dbo.Student set Sdept='"+a6+"' where Sno='"+a+"'";
                            st.executeUpdate(strSQL);
                        }
                        if (!a3.isEmpty()) {
                            String  strSQL="update dbo.SC set Grade='"+a4+"' where Sno='"+a+"' And Cno='"+a3+"'" ;
                            String  strSQL1="update dbo.SC set LEVEL='"+a7+"' where Sno='"+a+"' And Cno='"+a3+"'" ;
                            st.executeUpdate(strSQL);
                            st.executeUpdate(strSQL1);
                        }
                        JOptionPane.showMessageDialog(null,"修改成功哦~");
                    }
                    else {
                        String  strSQL="update dbo.Course set Cno='"+a11+"',Cname='"+a12+"',Ccredit='"+a13+"'  where Cno='"+a00+"'";
                        int rs1=st.executeUpdate(strSQL);
                        if(rs1==1) {
                            JOptionPane.showMessageDialog(null,"修改成功哦~");
                        }
                    }
                    conn.close();
                }catch (ClassNotFoundException ex) {
                    System.out.println("没有找到对应的数据库驱动类");
                }
                catch (SQLException ex) {
                    System.out.println("数据库连接或者是数据库操作失败");
                }
            }
        });
        //重置清零
        bb.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                c.setText("");c1.setText("");c2.setText("");
                c3.setText("");c4.setText("");c5.setText("");
                c6.setText("");c11.setText("");c12.setText("");
                c13.setText("");
            }
        });
    }
}

246ca2e69ba24289bacebb31c90a174b.png

In addition, for the addition, deletion and modification of the interface, I set the font class separately.

font class:

package 高校学生管理库;

import java.awt.Font;

public class font {
    public static Font title;
    public static Font account;
    public font() {
        title= new Font("宋体",Font.BOLD,30);
        account = new Font("宋体",Font.BOLD,16);
    }
}

SQL statements:

There is also the SQL language used: In addition, the administrator needs to set it by himself, you can also set the student fine

--照片存放表
create table image
 (  Sno char(10), 
    ima  varchar(50),
 );

 --学生表
create table Student
(  Sno char(9) primary key, /*列级完整性约束条件,Sno是主码 */
   Sname char(20) unique, /*Sname取唯一值 */
   Ssex char(2),
   Sage smallint,
   Sdept char(20)
 );

 --Course表
create table Course
(	Cno char(4) primary key,/* 列级完整性约束条件,Cno是主码*/
	Cname char(40) NOT NULL, /* 列级完整性约束条件,Cname不能取空值 */
	Cpno char(4),/* Cpno的含义是先修课 */
	Ccredit int,
	foreign key(Cpno) references Course(Cno) /*表级完整性约束条件,Cpno是外码,被参照表是Course,被参照列是Cno */
);

--信息表
create table SC
(	Sno char(9),
	Cno char(4),
	Grade int,
	LEVEL char(4),
	primary key(Sno,Cno),/*主码由两个属性构成,必须作为表级完整性进行定义 */
	foreign key(Sno) references Student(Sno), /*表级完整性约束条件,Sno是外码,被参照表是Student */
	foreign key(Cno) references Course(Cno)/*表级完整性约束条件,Cno是外码,被参照表是Course */
);

--管理员表
create table GLY(
	ID1 varchar(30),
	PAWD1 varchar(50)--密码
);

--学生注册表
create table PY(
	ID varchar(30),
	PAWD varchar(50),
	Sno char(9),
	phone  char(20)
);

--学生信息

--管理员


--插入Course数据
insert into Course values('1','数据库',null,4)
insert into Course values('2','高等数学A',null,2)
insert into Course values('3','信息系统',null,4)
insert into Course values('4','操作系统',null,3)
insert into Course values('5','数据结构','5',4)
insert into Course values('6','计算机网络','5',4)
insert into Course values('7','离散数学','5',2)
insert into Course values('8','大学英语','5',4)
insert into Course values('9','计算机组成原理','5',3)
insert into Course values('10','算法设计与分析','5',4)
insert into Course values('11','大学物理A',null,4)
insert into Course values('12','高等数学B',null,2)
insert into Course values('13','大学物理B',null,4)
insert into Course values('14','计算机英语',null,3)
insert into Course values('15','算法导论','5',4)
insert into Course values('16','数据库原理与分析','5',4)
insert into Course values('17','数据挖掘','5',2)
insert into Course values('18','移动应用开发','5',4)
insert into Course values('19','Javaweb开发应用','5',3)

--课程等级

Photos needed:

Figure 1:

13d43d4d7cc34689be803aba18ece4fd.jpeg

Figure II:

 62520652936942d5b977e2d587dd1dae.jpeg

Figure 3:

b6062c5da1b44951b513cd9e0ec66803.jpeg

Figure 4:

df447c02029b4ff5ae83090e7a5c60a9.jpeg

 Figure 5:

6c0a4cb215ed46d38b0e530f0ae32146.jpeg

Figure 6:

7f8cfbe1ae3b405ca5691fe799f77f15.jpeg

Figure 7:

46ede8ccb0e548118016a57b16c5c15f.jpeg

 Figure 8:

22c4416170164e6287e665d2c1bb7841.jpeg

Figure 9:

9288679a7d704d0cac253940d876d0b2.jpeg

 Figure ten:

daf566bcc81d4a4b8b365e46d2698649.jpeg

Figure 11:

0bf09297d1784b6aac965cf30182a62c.jpeg

Figure 12:

5006e6bc22484d998baf6980911e6567.jpeg

 

Guess you like

Origin blog.csdn.net/weixin_62174595/article/details/128448298