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

说明

1.本文适用对象:想要做项目练一下手,检验一下自己所学的小白程序员。

2.本文开发环境:jdk10+MySQL8.0+idea 2018

3.本文完成内容:MySQL的curd以及简单界面的制作(Swing)。

4.如果有必要,可以私信笔者给出源代码以及数据库文件。

正文

在观看本文章前,建议具备知识有:基本的MySQL的curd常识,基本Java知识点的了解,以及基本Swing界面的制作(可以边看边对照API).下面开始正文讲解:

在设计本系统时,需要做出基本的登陆界面,那么需要基本了解到容器的概念-顶层容器和中间容器,建议观看blog:https://blog.csdn.net/liujun13579/article/category/1172073 里面有超级详细的讲解。

看完之后,回归到顶层容器的设计,我们需要在顶层容器中定义的内容:

 

  1. 初始界面的名称;
  2. 初始界面的大小;
  3. 初始界面刚显示出来位于屏幕的何方;
  4. 初始的退出方式等。

各个功能键是定义在中间容器JPanel中,然后依附于顶层容器。

在程序运行过程中需要对按键进行实时的检测,判断哪个按键被按,则执行具体的方法。

快捷入口(本系统在编写过程中遇到的问题,如有相同,可以直接查看内容,否则跳过就好)

问题1:JDBC加载失败(重点)

https://blog.csdn.net/weixin_38627405/article/details/73824733

https://www.cnblogs.com/Xieyang-blog/p/8489400.html

问题2:

MYSQL:WARN: Establishing SSL connection without server's identity verification is not recommended.

解决办法:https://blog.csdn.net/u010429286/article/details/77750177

问题3:

mysql运行报The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone的解决方法

解决方法:https://www.cnblogs.com/ljy-20180122/p/9157912.html

问题4:MySQL如何设置自动获取当前时间

解决办法:https://www.cnblogs.com/lhj588/p/4245719.html

下面直接给出本设计中的代码(有较为详细的注释):

首先是基础界面示例代码:

package FirstVision;

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

/**
 * @className:Window
 * @Description:学生信息管理系统的登陆界面
 * @author:TAO
 * @Date:2018/6/26 8:40
 */
public class Window {
    public static void main(String[] args) {
        JFrame jFrame=new JFrame("学生信息管理系统");
        Dimension dimension=new Dimension(400,300);
        Point point=new Point(350,150);

        jFrame.setSize(dimension);
        jFrame.setLocation(point);//设置组件的位置
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //jFrame.setVisible(true);

        JButton button1=new JButton("添加");
        JButton button2=new JButton("修改");
        JButton button3=new JButton("查询");
        JButton button4=new JButton("删除");
        JButton button5=new JButton("浏览");//定义五个功能键

        FlowLayout flowLayout=new FlowLayout(FlowLayout.LEFT,10,10);//设置对其方式以及确定的左右上下间隙
        JPanel panel=new JPanel(flowLayout);
        panel.add(button1);
        panel.add(button2);
        panel.add(button3);
        panel.add(button4);
        panel.add(button5);

        jFrame.setContentPane(panel);

        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Add add=new Add();
            }
        });
        button2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Change change=new Change();
            }
        });
        button3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Ask ask=new Ask();
            }
        });
        button4.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Delete delete=new Delete();
            }
        });
        button5.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Look look=new Look();
            }
        });
        jFrame.setVisible(true);//JAVA写界面程序的时候,要把setVisible(true)放到最后面,不然在这句之后添加的组件有时候不显示,需要重新刷新。
    }
}

再是MySQL的curd和查看整个表内容的方法

首先是添加:

package FirstVision;

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

/**
 * @className:Add
 * @Description:数据库添加
 * @author:TAO
 * @Date:2018/6/26 8:54
 */
public class Add extends JFrame {
    private static final long serialVersionUID=-1928970409928880648L;

    JLabel jlnumber=new JLabel("学号:");
    JLabel jlname=new JLabel("姓名:");
    JLabel jlsex=new JLabel("性别:");
    JLabel jlbirthday=new JLabel("生日:");
    JLabel jldepartment=new JLabel("学院:");

    JTextField jtnumber=new JTextField("",20);
    JTextField jtname=new JTextField("",20);
    JTextField jtsex=new JTextField("",20);
    JTextField jtbirthday=new JTextField("",20);
    JTextField jtdepartment=new JTextField("",20);

    JButton buttonadd=new JButton("添加");
    JButton buttonreturn=new JButton("返回");

    public Add(){
        JPanel jpnumber=new JPanel();
        JPanel jpname=new JPanel();
        JPanel jpsex=new JPanel();
        JPanel jpbirthday=new JPanel();
        JPanel jpdepartment=new JPanel();
        JPanel jpforbutton=new JPanel(new GridLayout(1,1));

        jpnumber.add(jlnumber);
        jpnumber.add(jtnumber);

        jpname.add(jlname);
        jpname.add(jtname);

        jpsex.add(jlsex);
        jpsex.add(jtsex);

        jpbirthday.add(jlbirthday);
        jpbirthday.add(jtbirthday);

        jpdepartment.add(jldepartment);
        jpdepartment.add(jtdepartment);

        jpforbutton.add(buttonadd);
        jpforbutton.add(buttonreturn);

        buttonadd.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Connection conn=null;
//                Statement stat=null;
                PreparedStatement ps=null;

                String sql="Insert into student(id,name,sex,birthday,department)"+"values(?,?,?,?,?)";

                try{
                    Class.forName("com.mysql.cj.jdbc.Driver");//尝试Class.forName("Driver")失败
                    System.out.println("JDBC 加载成功!");
                }catch (Exception a){
                    System.out.println("JDBC 加载失败!");
                    a.printStackTrace();
                }
                try {
                    conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/javaStu?useSSL=false ","root","123456");
                    ps=conn.prepareStatement(sql);

                    ps.setString(1,jtnumber.getText());
                    ps.setString(2,jtname.getText());
                    ps.setString(3,jtsex.getText());
                    ps.setString(4,jtbirthday.getText());
                    ps.setString(5,jtdepartment.getText());

                    ps.executeUpdate();
                }catch (SQLException b){
                    b.printStackTrace();
                }finally {
                    try{
                        conn.close();
                        //System.out.println("Mysql 关闭成功");
                        System.out.println("add success.");
                    }catch (SQLException c){
                        //System.out.println("Mysql 关闭失败");
                        System.out.println("add fail.");
                        c.printStackTrace();
                    }
                }
            }
        });
        /**
         * 此处需要改进,返回按钮是结束当前界面,返回到前一个界面。
         * */
        buttonreturn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
//                Window window=new Window();
                dispose();
            }
        });
        this.setTitle("添加学生信息");
        this.setLayout(new GridLayout(9,1));
        this.add(jpnumber);
        this.add(jpname);
        this.add(jpsex);
        this.add(jpbirthday);
        this.add(jpdepartment);
        this.add(jpforbutton);

        this.setLocation(400,300);
        this.setSize(350,300);
        this.setVisible(true);
    }
}

其次是查询(根据序号查询):

package FirstVision;

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

/**
 * @className:Ask
 * @Description:查询数据库中的元素
 * @author:TAO
 * @Date:2018/6/26 8:54
 */
public class Ask extends JFrame{
    private static final long serialVersionUID=-1928970409928880648L;

    JLabel jlnumber=new JLabel("学号:");
    JLabel jlname=new JLabel("姓名:");
    JLabel jlsex=new JLabel("性别:");
    JLabel jlbirthday=new JLabel("生日:");
    JLabel jldepartment=new JLabel("学院:");

    JTextField jtnumber=new JTextField("",20);//根据学号查询信息
    JLabel jname=new JLabel();
    JLabel jsex=new JLabel();
    JLabel jbirthday=new JLabel();
    JLabel jdepartment=new JLabel();

    JButton buttonask=new JButton("查询");
    JButton buttonreturn=new JButton("返回");

    public Ask(){
        FlowLayout flowLayout=new FlowLayout(FlowLayout.LEFT,10,10);//设置对其方式以及间隙
        JPanel jpnumber=new JPanel(flowLayout);
        JPanel jpname=new JPanel(flowLayout);
        JPanel jpsex=new JPanel(flowLayout);
        JPanel jpbirthday=new JPanel(flowLayout);
        JPanel jpdepartment=new JPanel(flowLayout);
        JPanel jpforbutton=new JPanel(new GridLayout(1,1));//创建中间容器

        jpnumber.add(jlnumber);
        jpnumber.add(jtnumber);

        jpname.add(jlname);
        jpname.add(jname);

        jpsex.add(jlsex);
        jpsex.add(jsex);

        jpbirthday.add(jlbirthday);
        jpbirthday.add(jbirthday);

        jpdepartment.add(jldepartment);
        jpdepartment.add(jdepartment);

        jpforbutton.add(buttonask);
        jpforbutton.add(buttonreturn);

        buttonask.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Connection conn=null;
                ResultSet res=null;
                Statement stat=null;

                String sql="select * from student;";
                try{
                    Class.forName("com.mysql.cj.jdbc.Driver");
                    System.out.println("JDBC 加载成功");
                }catch (Exception d){
                    System.out.println("JDBC 加载失败");
                    d.printStackTrace();
                }
                try{
                    conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/javaStu?useSSL=false ","root","123456");
                    stat=conn.createStatement();
                    res=stat.executeQuery(sql);
                    while (res.next()){
                        if(res.getString(1).equals(jtnumber.getText())){
                            jname.setText(res.getString(2));
                            jsex.setText(res.getString(3));
                            jbirthday.setText(res.getString(4));
                            jdepartment.setText(res.getString(5));
                            break;
                        }
                    }
                }catch (SQLException e1){
                    e1.printStackTrace();
                }finally {
                    try{
                        conn.close();
                        System.out.println("ask success.");
                    }catch (SQLException e2){
                        System.out.println("ask fail.");
                        e2.printStackTrace();
                    }
                }
            }
        });
        buttonreturn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
//                Window window=new Window();
                dispose();//销毁窗体,即返回上一界面
            }
        });
        this.setTitle("查询学生信息");
        this.setLayout(new GridLayout(9,1));
        this.add(jpnumber);
        this.add(jpname);
        this.add(jpsex);
        this.add(jpbirthday);
        this.add(jpdepartment);
        this.add(jpforbutton);

        this.setLocation(400,300);
        this.setSize(350,300);
        this.setVisible(true);
    }
}

 

再是改变(change):

 

package FirstVision;

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

/**
 * @className:Change
 * @Description:改变数据内容
 * @author:TAO
 * @Date:2018/6/26 8:54
 */
public class Change extends JFrame {
    private static final long serialVersionUID=-1928970409928880648L;

    JLabel jlnumber=new JLabel("学号:");
    JLabel jlname=new JLabel("姓名:");
    JLabel jlsex=new JLabel("性别:");
    JLabel jlbirthday=new JLabel("生日:");
    JLabel jldepartment=new JLabel("学院:");

    JTextField jtnumber=new JTextField("",20);
    JTextField jtname=new JTextField("",20);
    JTextField jtsex=new JTextField("",20);
    JTextField jtbirthday=new JTextField("",20);
    JTextField jtdepartment=new JTextField("",20);

    JButton buttonchange=new JButton("修改");
    JButton buttonreturn=new JButton("返回");

    public Change(){
        JPanel jpnumber=new JPanel();
        JPanel jpname=new JPanel();
        JPanel jpsex=new JPanel();
        JPanel jpbirthday=new JPanel();
        JPanel jpdepartment=new JPanel();
        JPanel jpforbutton=new JPanel(new GridLayout(1,1));

        jpnumber.add(jlnumber);
        jpnumber.add(jtnumber);

        jpname.add(jlname);
        jpname.add(jtname);

        jpsex.add(jlsex);
        jpsex.add(jtsex);

        jpbirthday.add(jlbirthday);
        jpbirthday.add(jtbirthday);

        jpdepartment.add(jldepartment);
        jpdepartment.add(jtdepartment);

        jpforbutton.add(buttonchange);
        jpforbutton.add(buttonreturn);

        buttonchange.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                var number=jtnumber.getText();
                var name=jtname.getText();
                var sex=jtsex.getText();
                var birthday=jtbirthday.getText();
                var department=jtdepartment.getText();

                Connection conn=null;
                ResultSet res=null;
                Statement stat=null;

                var sql="select * from student";

                try{
                    conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/javaStu?useSSL=false ","root","123456");
                    stat=conn.createStatement();
                    res=stat.executeQuery(sql);
                    while(res.next()){
                        if(res.getString(1).equals(jtnumber.getText())){
                            try{
                                Class.forName("com.mysql.cj.jdbc.Driver");
                                System.out.println("JDBC 加载成功");
                            }catch (Exception e6){
                                System.out.println("JDBC 加载失败");
                                e6.printStackTrace();
                    }
                            String sql2="update student set name='"+name+"'where id='"+jtnumber.getText()+"'";
                            String sql3="update student set sex='"+sex+"'where id='"+jtnumber.getText()+"'";
                            String sql4="update student set birthday='"+birthday+"'where id='"+jtnumber.getText()+"'";
                            String sql5="update student set department='"+department+"'where id='"+jtnumber.getText()+"'";
                            try{
                                conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/javaStu?useSSL=false ","root","123456");
                                stat=conn.createStatement();
                                stat.execute(sql2);
                                stat.execute(sql3);
                                stat.execute(sql4);
                                stat.execute(sql5);//分清execute和executeQuery的区别
                            }catch (SQLException e7){
                                e7.printStackTrace();
                            }
                            try{
                                stat.close();
                                conn.close();
                            }catch (Exception e8){
                                e8.printStackTrace();
                            }
                            break;
                        }
                    }
                }catch (Exception e9){
                    e9.printStackTrace();
                }finally {
                    try{
                        stat.close();
                        conn.close();
                        System.out.println("change success.");
                    }catch (Exception e8){
                        System.out.println("change fail.");
                        e8.printStackTrace();
                    }
                }
            }
        });
        buttonreturn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
//                Window window=new Window();
            }
        });
        this.setTitle("修改学生信息");
        this.setLayout(new GridLayout(9,1));
        this.add(jpnumber);
        this.add(jpname);
        this.add(jpsex);
        this.add(jpbirthday);
        this.add(jpdepartment);
        this.add(jpforbutton);

        this.setLocation(400,300);
        this.setSize(350,300);
        this.setVisible(true);
    }
}

删除操作:

package FirstVision;

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

/**
 * @className:Delete
 * @Description:TODO
 * @author:TAO
 * @Date:2018/6/26 8:54
 */
public class Delete extends JFrame {
    private static final long serialVersionUID=-1928970409928880648L;

    JLabel jlnumber=new JLabel("学号:");
    JTextField jtnumber=new JTextField("",20);

    JButton buttondelete=new JButton("删除");
    JButton buttonreturn=new JButton("返回");

    public Delete(){
        JPanel jpnumber=new JPanel();
        JPanel jpforbutton=new JPanel(new GridLayout(1,1));

        jpnumber.add(jlnumber);
        jpnumber.add(jtnumber);

        jpforbutton.add(buttondelete);
        jpforbutton.add(buttonreturn);

        buttondelete.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String number=jtnumber.getText();
                Connection conn=null;
                ResultSet res=null;
                Statement stat=null;
                String sql="delete from student where id='"+number+"'";

                try{
                    Class.forName("com.mysql.cj.jdbc.Driver");
                    System.out.println("JDBC 加载成功");
                }catch (Exception e1){
                    System.out.println("JDBC 加载失败");
                    e1.printStackTrace();
                }
                try{
                    conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/javaStu?useSSL=false ","root","123456");
                    stat=conn.createStatement();
                    stat.executeUpdate(sql);
                }catch (Exception e2){
                    e2.printStackTrace();
                }finally {
                    try{
                        conn.close();
                        stat.close();
                        System.out.println("delete success.");
                    }catch (SQLException e3){
                        System.out.println("delete fail.");
                        e3.printStackTrace();
                    }
                }
            }
        });
        buttonreturn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
//                Window window=new Window();
                dispose();
            }
        });
        this.setTitle("删除学生信息:");

        this.setLayout(new GridLayout(9,1));
        this.add(jpnumber);
        this.add(jpforbutton);
        this.setLocation(400,300);
        this.setSize(350,300);
        this.setVisible(true);
    }
}

最后是查看表的操作:

package FirstVision;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Vector;

/**
 * @className:Look
 * @Description:浏览
 * @author:TAO
 * @Date:2018/6/26 8:55
 */
public class Look extends JFrame {
    private static final long serialVersionUID=-1928970409928880648L;

    Connection conn=null;
    PreparedStatement ps=null;
    ResultSet res=null;

    JTable jTable=null;
    JScrollPane jScrollPane=new JScrollPane();

    Vector columnNames=null;
    Vector rowData=null;

    JButton buttonreturn=new JButton("返回");

    public Look(){
        JPanel jpforbutton=new JPanel(new GridLayout(1,1));
        jpforbutton.add(buttonreturn);
        columnNames=new Vector();
        columnNames.add("学号");
        columnNames.add("姓名");
        columnNames.add("性别");
        columnNames.add("生日");
        columnNames.add("学院");
        rowData=new Vector();

        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            System.out.println("JDBC 加载成功");
        }catch (Exception e3){
            System.out.println("JDBC 加载失败");
            e3.printStackTrace();
        }
        try{
            conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/javaStu?useSSL=false ","root","123456");
            ps=conn.prepareStatement("select * from student;");
            res=ps.executeQuery();
            while(res.next()){
                Vector hang=new Vector();
                hang.add(res.getString(1));
                hang.add(res.getString(2));
                hang.add(res.getString(3));
                hang.add(res.getString(4));
                hang.add(res.getString(5));
                rowData.add(hang);
            }
            System.out.println("load ok!");
        }catch (Exception e4){
            System.out.println("die_1");
            e4.printStackTrace();
        }finally {
            try{
                res.close();
                conn.close();
                ps.close();
                System.out.println("look success.");
            }catch (Exception e5){
                System.out.println("look fail.");
                e5.printStackTrace();
            }
        }
        buttonreturn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
//                Window window=new Window();
                dispose();
            }
        });
        jTable=new JTable(rowData,columnNames);
        jScrollPane=new JScrollPane(jTable);
        this.add(jScrollPane);
        this.setTitle("浏览学生信息:");

        this.setLayout(new GridLayout(2,5));
        this.add(jpforbutton);
        this.setLocation(400,300);
        this.setSize(350,300);
        this.setVisible(true);
        //this.setResizable(false);
    }
}

数据库代码;

/*
Navicat MySQL Data Transfer

Source Server         : localhost_3306
Source Server Version : 80011
Source Host           : localhost:3306
Source Database       : javastu

Target Server Type    : MYSQL
Target Server Version : 80011
File Encoding         : 65001

Date: 2018-08-03 17:16:50
*/

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for `student`
-- ----------------------------
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
  `id` int(11) NOT NULL,
  `name` varchar(10) NOT NULL,
  `sex` varchar(5) NOT NULL,
  `birthday` datetime DEFAULT NULL,
  `department` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

-- ----------------------------
-- Records of student
-- ----------------------------
INSERT INTO `student` VALUES ('1', '江三金', '女', '1996-02-03 00:00:00', '机械');
INSERT INTO `student` VALUES ('2', '雷小刚', '男', '1995-08-12 00:00:00', '电气');
INSERT INTO `student` VALUES ('3', '李二', '男', '1996-03-14 00:00:00', '机械');
INSERT INTO `student` VALUES ('4', '李子韬', '女', '1998-02-16 00:00:00', '经管');
INSERT INTO `student` VALUES ('5', '周高', '男', '1997-11-12 00:00:00', '计算机');
INSERT INTO `student` VALUES ('6', '吴三桂', '男', '1994-12-13 00:00:00', '机械');
INSERT INTO `student` VALUES ('7', '张益达', '男', '1980-06-12 00:00:00', '马克思');
INSERT INTO `student` VALUES ('8', '黄蓉', '女', '1988-08-08 00:00:00', '艺设');
INSERT INTO `student` VALUES ('9', '洪七公', '男', '1958-06-13 00:00:00', '丐帮');
INSERT INTO `student` VALUES ('10', '郭靖', '男', '1986-05-13 00:00:00', '土建');

 

你可能感兴趣的:(program)