Student information management system (JAVA+MYSQL)

Realize the student information management system based on Java swing+MySQL: functions: 1. Input the basic information of students; 2. The function of querying students' basic information; 3. The function of modifying students' basic information; 4. The function of deleting students' basic information; 5. The function of displaying all student information; It's enough to deal with the general curriculum. Let's share it with you.

If necessary: https://pan.baidu.com/s/1JqLFKPlmhV2INeETy9lHAQ

Extraction code: nima

It includes all code source files +mysql8.0.25 driver jar package + background image of login page 345.jpg

1. Development environment: jdk11+win10+mysql 8+IDEA

Remember to connect the database with IDEA or eclipse, and remember to add the database driver jar package

These two are adding jar packages and idea to connect mysql. You can make a reference

IDEA importing mysql database driver_ Follow the blog of sun -CSDN blog [here is picture 004]https://blog.csdn.net/qq_54705917/article/details/123484397?spm=1001.2014.3001.5502IDEA connects to mysql database_ Follow the blog of sun -CSDN blog [here is picture 005]https://blog.csdn.net/qq_54705917/article/details/123484737?spm=1001.2014.3001.5502

2. Database design

code:

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

3. Window and function design

(1) . main function

main.java

import javax.swing.*;

public class main {
    public static void main(String[] args) {
        JFrame jf = new StuLogin();
    }
}

(2) . login interface (the default account and password are: admin)

StuLogin.java

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

public class StuLogin extends JFrame {
    private StuLogin self;
    private ImageIcon imageIcon;
    private Image image;
    private String userid;// Login user name and password
    private String password;
    private JLabel unLabel = new JLabel("account number:");// Landing panel control
    private JTextField unField = new JTextField();
    private JLabel pwLabel = new JLabel("password:");
    private JPasswordField pwField = new JPasswordField();
    private JButton dl = new JButton("Sign in");
    private JButton d2 = new JButton("Reset");
    public StuLogin() {

        this.self = this;
        this.setSize(350, 300);// Set login panel
        Set window background
        //Set contentPane to transparent first
        ((JPanel)getContentPane()).setOpaque(false);
        //Set the picture again
        imageIcon = new ImageIcon("345.jpg");//Icon component
        image = imageIcon.getImage();
        JLabel imgLabel = new JLabel(imageIcon);
        getLayeredPane().add(imgLabel, new Integer(Integer.MIN_VALUE));
        imgLabel.setBounds(0,0,400,300); //Location of background picture

        this.setIconImage(image);//Set window image
        this.setLocation(600,300);
        this.setVisible(true);
        this.setResizable(false);
        this.setLayout(null);
//      this.getContentPane().setBackground(Color.BLACK); Set the window background color;

        //Set window name
        this.setTitle("Student information management system");
        unLabel.setSize(50, 30);
        unLabel.setLocation(60, 40);
        unLabel.setForeground(Color.red);
        unLabel.setFont(new Font("Italics",Font.BOLD,15));
        unField.setSize(150, 35);
        unField.setLocation(110, 35);
        pwLabel.setSize(50, 30);
        pwLabel.setLocation(60, 100);
        pwLabel.setForeground(Color.red);
        pwLabel.setFont(new Font("Italics",Font.BOLD,15));
        pwField.setSize(150, 35);
        pwField.setLocation(110, 100);
        dl.setSize(80, 35);
        dl.setLocation(65, 175);
        dl.setBackground(Color.red);
        d2.setSize(80, 35);
        d2.setLocation(185, 175);
        d2.setBackground(Color.red);
        dl.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                userid = unField.getText();
                password = pwField.getText();
                if(userid.equals("admin")&&password.equals("admin")) {
                    self.setVisible(false);
//                    JOptionPane.showMessageDialog(null, "login success", "login status", joptionpane.place_message);
                    new StuManager();
                } else {
                    JOptionPane.showMessageDialog(null, "Account or password error!", "Login status",JOptionPane.PLAIN_MESSAGE);
                }
            }
        });
        d2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                unField.setText("");
                pwField.setText("");
            }
        });
        this.add(unLabel);
        this.add(unField);
        this.add(pwLabel);
        this.add(pwField);
        this.add(dl);
        this.add(d2);
    }
}

I will put the background image of the login page in the link shared above

(3) Administrator interface (deletion function is in it)

StuManager.java

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

public class StuManager extends JFrame implements ActionListener {
    //Define some controls
    private Object[] types = {"-Please select the query method-", "Query by student number", "Name inquiry", "Gender inquiry","Query by age", "Query by place of origin","Query by class"};
    private JComboBox searchType = new JComboBox(types); //Create a combo box to select and query different student information·
    JPanel jp1,jp2;
    JLabel jl1;
    JButton jb1,jb2,jb3,jb4;
    JTable jt;
    JScrollPane jsp;
    JTextField jtf1,jtf2;
    String strRS;
    StuModel sm;
    //Define variables that connect to the database
    PreparedStatement ps;
    Connection ct = null;
    ResultSet rs = null;
    //Constructor
    public StuManager(){
        jp1 = new JPanel();
        jp1.setBackground(Color.gray);
        jtf1 = new JTextField(15);
        jtf2 = new JTextField();
        jtf2.setEditable(false);
        jb1 = new JButton("query");
        jb1.addActionListener(this);
        jl1 = new JLabel("Total number:");
        jp1.add(searchType);
        jp1.add(jtf1);
        jp1.add(jb1);
        jp1.add(jl1);
        jp1.add(jtf2);
        jb2 = new JButton("add to");
        jb2.setSize(100,500);
        jb2.addActionListener(this);
        jb3 = new JButton("modify");
        jb3.addActionListener(this);
        jb4 = new JButton("delete");
        jb4.addActionListener(this);

        jp2 = new JPanel();
        jp2.add(jb2);
        jp2.add(jb3);
        jp2.add(jb4);
        jp2.setBackground(Color.gray);
        //Create model objects
        sm = new StuModel();
        //Initialize the total number of people
        strRS=String.valueOf(sm.getRowCount());
        jtf2.setText(strRS);
        //Initialize table and scroll panel
        jt = new JTable(sm);
        jsp = new JScrollPane(jt);


        //Put jsp into jframe
        this.add(jsp);
        this.add(jp1,BorderLayout.PAGE_START);
        this.add(jp2,BorderLayout.PAGE_END);
        this.setTitle("Student information management system");
//        this.pack();
        this.setSize(600, 400);
        this.setLocation(500, 200);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setVisible(true);

    }
    public void actionPerformed(ActionEvent arg0) {
        //Determine which button is clicked
        if(arg0.getSource() == jb1){
            System.out.println("Users want to be queried...");
            int index = searchType.getSelectedIndex();
            String sql = new String();
            if(index == 0){
                sql = "select * from stu ";
            }
            else if(index == 1){
                //Because the data of the table is encapsulated in StuModel, you can easily complete the query
                String Id =this.jtf1.getText().trim();
                //Write an sql statement
                sql = "select * from stu where stuId = '"+Id+"' ";

            }
            else if(index == 2){
                String name =this.jtf1.getText().trim();
                sql = "select * from stu where stuName = '"+name+"' ";

            }
            else if(index == 3){
                String sex =this.jtf1.getText().trim();
                sql = "select * from stu where stuSex = '"+sex+"' ";

            }
            else if(index == 4){
                String age =this.jtf1.getText().trim();
                sql = "select * from stu where stuAge = '"+age+"' ";
            }
            else if(index ==5){
                String jg =this.jtf1.getText().trim();
                sql = "select * from stu where stuJG= '"+jg+"' ";

            }
            else if(index ==6){
                String bj =this.jtf1.getText().trim();
                sql = "select * from stu where stuBJ= '"+bj+"' ";

            }
            //Build a data model class and update it
            sm = new StuModel(sql);

            strRS=String.valueOf(sm.getRowCount());
            jtf2.setText(strRS);
            //Update jtable
            jt.setModel(sm);

        }

        //1, Pop up the add interface
        else if(arg0.getSource() == jb2){
            System.out.println("add to...");
            StuAddDiag sa = new StuAddDiag(this,"Add student",true);
            //Get a new data model again,
            sm =  new StuModel();
            strRS=String.valueOf(sm.getRowCount());
            jtf2.setText(strRS);
            jt.setModel(sm);
        }else if(arg0.getSource() == jb4){
            //2, Delete record
            //1. Get student ID
            int rowNum = this.jt.getSelectedRow();//getSelectedRow will return the row in the user point
            //If the user does not select a line, it returns -1
            if(rowNum == -1){
                //Tips
                JOptionPane.showMessageDialog(this, "Please select a row");
                return ;
            }
            //Get academic ID
            String stuId = (String)sm.getValueAt(rowNum, 0);


            //Connect to the database and complete the deletion task
            try{
                //1. Load drive
                Class.forName("com.mysql.cj.jdbc.Driver");
                //2. Connect to the database
                String url = "jdbc:mysql://localhost:3306/student";
                String user = "root";
                String passwd = "020334";

                ct = DriverManager.getConnection(url, user, passwd);
//                System.out.println("connection succeeded");
                ps = ct.prepareStatement("delete from stu where stuId = ?");
                ps.setString(1,stuId);
                ps.executeUpdate();
                JOptionPane.showMessageDialog(null, "Successfully deleted", "Deletion",JOptionPane.PLAIN_MESSAGE);
            }catch(Exception e){
                e.printStackTrace();
            }finally{
                try{
                    if(rs!= null){
                        rs.close();
                        rs = null;

                    }
                    if(ps!= null){
                        ps.close();
                        ps = null;
                    }
                    if(ct != null){
                        ct.close();
                        ct = null;
                    }
                } catch(Exception e){
                    e.printStackTrace();
                }
            }
            sm = new StuModel();
            strRS=String.valueOf(sm.getRowCount());
            jtf2.setText(strRS);
            //Update jtable
            jt.setModel(sm);
        }else if(arg0.getSource() == jb3){
//            System.out.println("11111");
            //3, User wants to modify
            int rowNum = this.jt.getSelectedRow();
            if(rowNum == -1){
                //Tips
                JOptionPane.showMessageDialog(this, "Please select a row");
                return ;
            }
            //display a dialog box
//            System.out.println( "12435");
            StuUpDiag su = new StuUpDiag(this, "Modify student information", true, sm, rowNum);
            sm = new StuModel();
            jt.setModel(sm);
        }
    }
}

(4) . model interface

StuModel.java

/*
  Used to refresh and render the database
 * This is a model of my stu table
 * You can encapsulate all the operations on the student table into this class
 */
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Vector;
import javax.swing.table.*;

public class StuModel extends AbstractTableModel{

    //rowData stores row data and columnNames stores column names
    Vector rowData,columnNames;//Like ArrayList, the bottom layer of Vector is also an array of Object type Object [].; Construct an empty Vector so that the size of its internal data array is 10 and its standard capacity increment is zero

    //Define variables that connect to the database
    Statement stat = null;
    Connection ct = null;
    ResultSet rs = null;

    //initialization
    public void init(String sql){
        if(sql.equals("")){
            sql = "select * from stu";
        }
        //middle
        //Set column name
        columnNames = new Vector();//Here is a one-dimensional vector representation column;
        columnNames.add("Student number");
        columnNames.add("name");
        columnNames.add("Gender");
        columnNames.add("Age");
        columnNames.add("Native place");
        columnNames.add("contact information");
        columnNames.add("class");

        //rowData stores multiple rows
        rowData = new Vector();
        try{
            //1. Load drive
            Class.forName("com.mysql.cj.jdbc.Driver");
            System.out.println("Load successful");
            //2. Connect to the database
            //Define several constants
            String url = "jdbc:mysql://localhost:3306/student";
            String user = "root";
            String passwd = "020334";//Here you need to fill in your own database password

            ct = DriverManager.getConnection(url,user,passwd);
            stat = ct.createStatement();//Create stat object
            rs = stat.executeQuery(sql);//Query results

            while(rs.next()){
                Vector hang = new Vector();
                hang.add(rs.getString(1));
                hang.add(rs.getString(2));
                hang.add(rs.getString(3));
                hang.add(rs.getString(4));
                hang.add(rs.getString(5));
                hang.add(rs.getString(6));
                hang.add(rs.getString(7));
                //Add to rowData
                rowData.add(hang);//Here is a two-dimensional vector, representing rows;
            }

        }catch(Exception e){
            e.printStackTrace();
        }finally{
            try{
                if(rs!=null){
                    rs.close();
                    rs = null;
                }
                if(stat != null){
                    stat.close();
                    stat = null;
                }
                if(ct != null){
                    ct.close();
                    ct = null;
                }
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }


    //The second constructor obtains the data model through the passed sql statement
    public StuModel(String sql){
        this.init(sql);
    }

    //Constructor to initialize my data model (table)
    public StuModel(){
        this.init("");
    }

    //Get the total number of lines
    public int getRowCount() {
        // TODO Auto-generated method stub
        return this.rowData.size();
    }

    //Get the total number of columns
    public  int getColumnCount() {
        // TODO Auto-generated method stub
        return this.columnNames.size();
    }

    //Get the data of a row and a column
    public Object getValueAt(int row, int column) {
        // TODO Auto-generated method stub
        return ((Vector)(this.rowData.get(row))).get(column);
    }

    //Get attribute name
    public String getColumnName(int column) {
        // TODO Auto-generated method stub
        return (String)this.columnNames.get(column);
    }
}

(5) . add student interface

StuAddDiag.java

import javax.swing.JDialog;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.*;
//Used to realize the function of adding readers
public class StuAddDiag extends JDialog implements ActionListener {
    //Define the swing components I need
    JLabel jl1,jl2,jl3,jl4,jl5,jl6,jl7;
    JTextField jf1,jf2,jf3,jf4,jf5,jf6,jf7;
    JPanel jp1,jp2,jp3;
    JButton jb1,jb2;
    //owner writes the parent window instead. title is the name of the window, and modal specifies whether it is a modal window () or a non modal window
    public StuAddDiag(Frame owner, String title, boolean modal){
        //Call parent method
        super(owner,title,modal);

        jl1 = new JLabel("Student number");
        jl2 = new JLabel("name");
        jl3 = new JLabel("Gender");
        jl4 = new JLabel("Age");
        jl5 = new JLabel("Native place");
        jl6 = new JLabel("contact information");
        jl7 = new JLabel("class");

        jf1 = new JTextField(30);
        jf2 = new JTextField(30);
        jf3 = new JTextField(30);
        jf4 = new JTextField(30);
        jf5 = new JTextField(30);
        jf6 = new JTextField(30);
        jf7 = new JTextField(30);

        jb1 = new JButton("add to");
        jb1.addActionListener(this::actionPerformed);
        jb2 = new JButton("cancel");
        jb2.addActionListener(this::actionPerformed);

        jp1 = new JPanel();
        jp2 = new JPanel();
        jp3 = new JPanel();

        //Set layout
        jp1.setLayout(new GridLayout(7,1));
        jp2.setLayout(new GridLayout(7,1));

        jp3.add(jb1);
        jp3.add(jb2);

        jp1.add(jl1);
        jp1.add(jl2);
        jp1.add(jl3);
        jp1.add(jl4);
        jp1.add(jl5);
        jp1.add(jl6);
        jp1.add(jl7);

        jp2.add(jf1);
        jp2.add(jf2);
        jp2.add(jf3);
        jp2.add(jf4);
        jp2.add(jf5);
        jp2.add(jf6);
        jp2.add(jf7);

        this.add(jp1, BorderLayout.WEST);
        this.add(jp2, BorderLayout.CENTER);
        this.add(jp3, BorderLayout.SOUTH);
        this.setLocation(600, 350);
        this.setSize(300,200);
        this.setVisible(true);
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        if(e.getSource() == jb1){
            Connection ct = null;
            PreparedStatement pstmt = null;
            ResultSet rs = null;

            try{
                //1. Load drive
                Class.forName("com.mysql.cj.jdbc.Driver");
                System.out.println("Load successful");
                //2. Connect to the database
                //Define several constants
                String url = "jdbc:mysql://localhost:3306/student";
                String user = "root";
                String passwd = "020334";
                ct = DriverManager.getConnection(url,user,passwd);

                //And compile statement objects

                String strsql = "insert into stu values(?,?,?,?,?,?,?)";
                pstmt = ct.prepareStatement(strsql);

                //Assign values to objects
                pstmt.setString(1,jf1.getText());
                pstmt.setString(2,jf2.getText());
                pstmt.setString(3,jf3.getText());
                pstmt.setString(4,jf4.getText());
                pstmt.setString(5,jf5.getText());
                pstmt.setString(6,jf6.getText());
                pstmt.setString(7,jf7.getText());

                pstmt.executeUpdate();
                JOptionPane.showMessageDialog(null, "Successfully added", "Add situation",-1);
                this.dispose();//Close student dialog

            }catch(Exception arg1){
                arg1.printStackTrace();
            }finally{
                try{
                    if(rs!=null){
                        rs.close();
                        rs = null;
                    }
                    if(pstmt != null){
                        pstmt.close();
                        pstmt = null;
                    }
                    if(ct != null){
                        ct.close();
                        ct = null;
                    }
                }catch(Exception arg2){
                    arg2.printStackTrace();
                }
            }

        }else{
            this.dispose();
        }

    }
}

(6) . modify student interface

StuUpDiag.java

import javax.swing.JDialog;
import javax.swing.*;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.*;
/*
// * Is to modify student information

 */
public class StuUpDiag extends JDialog implements ActionListener {
    //Define the swing components I need
    JLabel jl1,jl2,jl3,jl4,jl5,jl6,jl7;
    JTextField jf1,jf2,jf3,jf4,jf5,jf6,jf7;
    JPanel jp1,jp2,jp3;
    JButton jb1,jb2;
    //owner writes the parent window instead. title is the name of the window, and modal specifies whether it is a modal window () or a non modal window
    public StuUpDiag(Frame owner, String title, boolean modal, StuModel sm, int rowNum){
        //Call parent method
        super(owner,title,modal);

        jl1 = new JLabel("Student number");
        jl2 = new JLabel("name");
        jl3 = new JLabel("Gender");
        jl4 = new JLabel("Age");
        jl5 = new JLabel("Native place");
        jl6 = new JLabel("contact information");
        jl7 = new JLabel("class");



        jf1 = new JTextField(30);
        jf1.setText((sm.getValueAt(rowNum, 0)).toString());
        jf2 = new JTextField(30);
        jf2.setText((String)sm.getValueAt(rowNum, 1));
        jf3 = new JTextField(30);
        jf3.setText(sm.getValueAt(rowNum, 2).toString());
        jf4 = new JTextField(30);
        jf4.setText((sm.getValueAt(rowNum, 3)).toString());
        jf5 = new JTextField(30);
        jf5.setText((String)sm.getValueAt(rowNum, 4));
        jf6 = new JTextField(30);
        jf6.setText((String)sm.getValueAt(rowNum, 5));
        jf7 = new JTextField(30);
        jf7.setText((String)sm.getValueAt(rowNum, 6));

        jb1 = new JButton("modify");
        jb1.addActionListener(this::actionPerformed);
        jb2 = new JButton("cancel");
        jb2.addActionListener(this::actionPerformed);

        jp1 = new JPanel();
        jp2 = new JPanel();
        jp3 = new JPanel();

        //Set layout
        jp1.setLayout(new GridLayout(7,1));
        jp2.setLayout(new GridLayout(7,1));

        jp3.add(jb1);
        jp3.add(jb2);

        jp1.add(jl1);
        jp1.add(jl2);
        jp1.add(jl3);
        jp1.add(jl4);
        jp1.add(jl5);
        jp1.add(jl6);
        jp1.add(jl7);

        jp2.add(jf1);
        jp2.add(jf2);
        jp2.add(jf3);
        jp2.add(jf4);
        jp2.add(jf5);
        jp2.add(jf6);
        jp2.add(jf7);

        this.add(jp1, BorderLayout.WEST);
        this.add(jp2, BorderLayout.CENTER);
        this.add(jp3, BorderLayout.SOUTH);
        this.setLocation(600, 350);
        this.setSize(300,200);
        this.setVisible(true);
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        if(e.getSource() == jb1){
            Connection ct = null;
            PreparedStatement pstmt = null;
            ResultSet rs = null;

            try{
                //1. Load drive
                Class.forName("com.mysql.cj.jdbc.Driver");
                System.out.println("Load successful");
                //2. Connect to the database
                //Define several constants
                String url = "jdbc:mysql://localhost:3306/student";
                String user = "root";
                String passwd = "020334";
                ct = DriverManager.getConnection(url,user,passwd);
                //And compile statement objects
                String strsql = "update stu set stuName = '"+jf2.getText()+"',stuSex = '"+jf3.getText()+"',stuAge = '"+jf4.getText()+"',stuJG='"+jf5.getText()+"',stuLX='"+jf6.getText()+"',stuBJ='"+jf7.getText()+"' where stuId = '"+jf1.getText()+"'";
                pstmt = ct.prepareStatement(strsql);

                pstmt.executeUpdate();
                JOptionPane.showMessageDialog(null, "Successfully modified", "Modification",JOptionPane.PLAIN_MESSAGE);
                this.dispose();//Close student dialog

            }catch(Exception arg1){
                arg1.printStackTrace();
            }finally{
                try{
                    if(rs!=null){
                        rs.close();
                        rs = null;
                    }
                    if(pstmt != null){
                        pstmt.close();
                        pstmt = null;
                    }
                    if(ct != null){
                        ct.close();
                        ct = null;
                    }
                }catch(Exception arg2){
                    arg2.printStackTrace();
                }
            }

        }else{
            this.dispose();//Close student dialog
        }

    }


}

First of all, I would like to introduce myself. I graduated from Jiaotong University in 13 years. I once worked in a small company, went to large factories such as Huawei OPPO, and joined Alibaba in 18 years, until now. I know that most junior and intermediate Java engineers who want to improve their skills often need to explore and grow by themselves or sign up for classes, but there is a lot of pressure on training institutions to pay nearly 10000 yuan in tuition fees. The self-study efficiency of their own fragmentation is very low and long, and it is easy to encounter the ceiling technology to stop. Therefore, I collected a "full set of learning materials for java development" and gave it to you. The original intention is also very simple. I hope to help friends who want to learn by themselves and don't know where to start, and reduce everyone's burden at the same time. Add the business card below to get a full set of learning materials

Tags: Back-end Front-end Android Interview

Posted by sargus on Mon, 08 Aug 2022 02:17:01 +0930