JavaFX+Mysql 实现 学生信息管理系统

前言

基于JavaFx和Mysql实现一个学生信息管理系统。
同时实现 对学生信息的 增删改查 功能,以及实现用户注册账号和登录账号的功能。
完整代码在文章末尾。

页面展示 (部分)

1.登录页面在这里插入图片描述

2.管理员登录页面

在这里插入图片描述

3.注册页面

在这里插入图片描述

4.主页面

在这里插入图片描述
在这里插入图片描述

5.数据库建表

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

一、连接数据库

需要插入一个 jar包,然后通过JDBC建立连接。
(jar包放在文章末尾)
在这里插入图片描述

// 先建立连接
String URL="jdbc:mysql://127.0.0.1:3306/数据库名称?useUnicode=true&characterEncoding=UTF-8&userSSL=false&serverTimezone=GMT%2B8";//jdbc:mysql//服务器地址/数据库名
String USER=" ";//用户名
String PASSWORD=" ";//密码
Connection con=DriverManager.getConnection(URL, USER, PASSWORD); 
// 接着执行 sql语句
PreparedStatement pst =con.prepareStatement(sql) ;
// 查看 sql语句的执行结果
ResultSet rs = pstexecuteQuery();

二、实现主要功能

1.增加功能

实现对学生信息的添加。

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Modality;
import javafx.stage.Stage;
import java.awt.*;
import java.awt.print.Book;
import java.sql.*;
public class add_information {
    private static final String URL="jdbc:mysql://127.0.0.1:3306/数据库名?useUnicode=true&characterEncoding=UTF-8&userSSL=false&serverTimezone=GMT%2B8";//jdbc:mysql//服务器地址/数据库名
    private static final String USER="  ";//用户名
    private static final String PASSWORD="  ";//密码
    public add_information(TableView table, Stage stage) {
        Stage stage1 = new Stage();
        stage1.setTitle("增加信息");
        stage1.setWidth(400);
        stage1.setHeight(500);
        stage1.setX(740);
        stage1.setY(180);
        stage1.setResizable(false);
        stage1.initOwner(stage);
        stage1.initModality(Modality.WINDOW_MODAL);
        stage1.show();  //  让窗口显示出来
        AnchorPane an = new AnchorPane();
        Scene scene = new Scene(an);
        stage1.setScene(scene); // 设置场景
        Text t1 = new Text( 30,70,"学号");
        t1.setFont(new Font(30));
        Text t2 = new Text( 30,120,"班级");
        t2.setFont(new Font(30));
        Text t3 = new Text( 30,170,"姓名");
        t3.setFont(new Font(30));
        Text t4 = new Text( 30,220,"性别");
        t4.setFont(new Font(30));
        Text t5 = new Text( 10,270,"出生日期");
        t5.setFont(new Font(20));
        Text t6 = new Text( 30,320,"专业");
        t6.setFont(new Font(30));
        TextField a1 = new TextField();
        AnchorPane.setLeftAnchor(a1,110.0);
        AnchorPane.setTopAnchor(a1,48.0);
        TextField a2 = new TextField();
        AnchorPane.setLeftAnchor(a2,110.0);
        AnchorPane.setTopAnchor(a2,98.0);
        TextField a3 = new TextField();
        AnchorPane.setLeftAnchor(a3,110.0);
        AnchorPane.setTopAnchor(a3,148.0);
        TextField a4 = new TextField();
        AnchorPane.setLeftAnchor(a4,110.0);
        AnchorPane.setTopAnchor(a4,198.0);
        TextField a5 = new TextField();
        AnchorPane.setLeftAnchor(a5,110.0);
        AnchorPane.setTopAnchor(a5,248.0);
        TextField a6 = new TextField();
        AnchorPane.setLeftAnchor(a6,110.0);
        AnchorPane.setTopAnchor(a6,298.0);
        Button b1 = new Button("确认");
        AnchorPane.setTopAnchor(b1,358.0);
        AnchorPane.setLeftAnchor(b1,150.0);
        TextField a7 = new TextField();
        a7.setText("0");
        b1.setStyle(
                "-fx-font-size: 32px"
        );
        an.getChildren().addAll(t1,t2,t3,t4,t5,t6,a1,a2,a3,a4,a5,a6,b1);
        // 这个是 点击 增加信息按钮
        b1.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                Connection con=null;

                    if (a1.getText().isEmpty()||a2.getText().isEmpty()||a3.getText().isEmpty()||a4.getText().isEmpty()||a5.getText().isEmpty()||a6.getText().isEmpty()){
                        zhuceshibai shibai = new zhuceshibai(stage1,"请输入完整的信息");
                    }else {

                        try {
                            con = DriverManager.getConnection(URL, USER, PASSWORD);
                        } catch (SQLException throwables) {
                            throwables.printStackTrace();
                        }
                        String sqll= "select ID from information where ID=?";
                        PreparedStatement pst11 = null;
                        try {
                            pst11 = con.prepareStatement(sqll);
                            pst11.setString(1,a1.getText());
                        } catch (SQLException throwables) {
                            throwables.printStackTrace();
                        }
                        ResultSet rss = null;
                        try {
                            rss = pst11.executeQuery();
                        } catch (SQLException throwables) {
                            throwables.printStackTrace();
                        }
                        try {
                            if (rss.next()) {
                                zhuceshibai shibai6 = new zhuceshibai(stage1,"该学号已经添加过信息");
                            }else {

                                String s="0";    // 添加一个学生信息的同时,默认该学生的所有成绩为 0
                               Connection connection = DriverManager.getConnection(URL,USER,PASSWORD);
                             PreparedStatement preparedStatement = connection.prepareStatement("insert into score (ID,China,Math,English) values(?,?,?,?)");
                             preparedStatement.setString(1,a1.getText());
                             preparedStatement.setString(2,"0");
                             preparedStatement.setString(3,"0");
                             preparedStatement.setString(4,"0");
                                preparedStatement.executeUpdate();
                                stage1.close();
                                stage.close();
                                PreparedStatement pst = null;
                                try {
                                    //1.加载驱动程序
                                    //此语句固定,使用MySQL数据库无需更改,在JSP中可不加异常处理
                                    try {
                                        Class.forName("com.mysql.cj.jdbc.Driver");
                                    } catch (ClassNotFoundException e) {
                                        e.printStackTrace();
                                    }
                                    con = DriverManager.getConnection(URL, USER, PASSWORD);
                                    Statement sql = con.createStatement();
                                    try {
                                        con = DriverManager.getConnection(URL, USER, PASSWORD);
                                        pst = con.prepareStatement("insert into information (ID,grade,name,sex,birth,major) values(?,?,?,?,?,?) ");
                                        pst.setString(1, a1.getText());
                                        pst.setString(2, a2.getText());
                                        pst.setString(3, a3.getText());
                                        pst.setString(4, a4.getText());
                                        pst.setString(5, a5.getText());
                                        pst.setString(6, a6.getText());
                                        pst.execute();
                                        Mywindow m1 = new Mywindow();  // 刷新页面
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                    }
                                } catch (SQLException E) {
                                    System.out.println("SQL异常!!!!");
                                }
                            }
                        } catch (SQLException throwables) {
                            throwables.printStackTrace();
                        }
                    }
            }
        });
    }
}

在这里插入图片描述

2.删除功能

通过输入学号,进该学生进行搜索,然后进行删除

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Modality;
import javafx.stage.Stage;
import sample.Mywindow;
import sample.zhuceshibai;
import java.sql.*;
public class delet_information {
    private final Stage stage2 = new Stage();
    private static final String URL = "jdbc:mysql://127.0.0.1:3306/数据库名?useUnicode=true&characterEncoding=UTF-8&userSSL=false&serverTimezone=GMT%2B8";//jdbc:mysql//服务器地址/数据库名
    private static final String USER = "";//用户名
    private static final String PASSWORD = "";//密码
    Connection con=null;
    PreparedStatement pst=null;
    ResultSet rs=null;
    public delet_information(TableView table, Stage stage) {
        stage2.initOwner(stage);
        stage2.initModality(Modality.WINDOW_MODAL);
        stage2.show();  //  让窗口显示出来
        stage2.setTitle("删除信息");
        stage2.setWidth(400);
        stage2.setHeight(300);
        stage2.setX(740);
        stage2.setY(180);
        stage2.setResizable(false);
        AnchorPane an = new AnchorPane();
        Scene scene = new Scene(an); // bor 是 方位布局
        stage2.setScene(scene); // 设置场景
        Text t1 = new Text(30, 70, "学号");
        t1.setFont(new Font(30));
        TextField a1 = new TextField();
        AnchorPane.setLeftAnchor(a1, 110.0);
        AnchorPane.setTopAnchor(a1, 48.0);
        Button b1 = new Button("确认");
        b1.setStyle(
                "-fx-font-size: 32px"
        );
        AnchorPane.setTopAnchor(b1, 150.0);
        AnchorPane.setLeftAnchor(b1, 130.0);
        an.getChildren().addAll(t1,a1,b1);
        b1.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                String s="select ID from information where ID=?";
                //  先判断输入框不为空的前提下,该学号是否存在
                if (!a1.getText().isEmpty()){
                    Connection connection= null;
                    try {
                        connection = DriverManager.getConnection(URL,USER,PASSWORD);
                    } catch (SQLException throwables) {
                        throwables.printStackTrace();
                    }
                    PreparedStatement preparedStatement = null;
                    try {
                        preparedStatement = connection.prepareStatement(s);
                        preparedStatement.setString(1,a1.getText());
                    } catch (SQLException throwables) {
                        throwables.printStackTrace();
                    }
                    ResultSet resultSet = null;
                    try {
                        resultSet = preparedStatement.executeQuery();
                    } catch (SQLException throwables) {
                        throwables.printStackTrace();
                    }
                    try {
                        if (!resultSet.next()){
                            zhuceshibai zhuceshibai = new zhuceshibai(stage2,"无该生信息,不用删了");
                        } else {
                            stage2.close();
                            stage.close();
                            try {
                                con = DriverManager.getConnection(URL, USER, PASSWORD);
//                                pst = con.prepareStatement("delete from information where id=? ");
                                // 同时 删除学生的信息和成绩
                                pst = con.prepareStatement("delete score,information from score,information where score.ID=? && information.ID=? ");
                                pst.setString(1, a1.getText());
                                pst.setString(2, a1.getText());
                                pst.execute();
                                Mywindow m1 = new Mywindow();  // 刷新页面
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    } catch (SQLException throwables) {
                        throwables.printStackTrace();
                    }
                }
                // 判断 输入框是否有内容
                else if (a1.getText().isEmpty()) {
                    zhuceshibai shibai = new zhuceshibai(stage2,"请输入完整的信息");
                }
            }
        });
    }
}

在这里插入图片描述

3.修改功能

先通过输入学号然后对该生进行搜索,如果搜索到了,弹出该学生默认信息,然后进行修改。

扫描二维码关注公众号,回复: 14855860 查看本文章
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Modality;
import javafx.stage.Stage;
import sample.chazhaoshibai;
import sample.mod2_information;
import sample.zhuceshibai;
import java.sql.*;
public class mod_information {
    private final Stage stage1 = new Stage();
    private static final String URL="jdbc:mysql://127.0.0.1:3306/数据库名?useUnicode=true&characterEncoding=UTF-8&userSSL=false&serverTimezone=GMT%2B8";//jdbc:mysql//服务器地址/数据库名
    private static final String USER="";//用户名
    private static final String PASSWORD="";//密码
    public mod_information(TableView table, Stage stage) {
        stage1.initOwner(stage);
        stage1.initModality(Modality.WINDOW_MODAL);
        stage1.show();  //  让窗口显示出来
        stage1.setTitle("修改信息");
        stage1.setWidth(400);
        stage1.setHeight(300);
        stage1.setX(740);
        stage1.setY(180);
        stage1.setResizable(false);
        AnchorPane an = new AnchorPane();
        Scene scene = new Scene(an); // bor 是 方位布局
        stage1.setScene(scene); // 设置场景
        Text t1 = new Text( 30,83,"学号");
        t1.setFont(new Font(30));
        TextField a1 = new TextField();
        AnchorPane.setLeftAnchor(a1,110.0);
        AnchorPane.setTopAnchor(a1,58.0);
        Button b1 = new Button("确认");
        AnchorPane.setTopAnchor(b1,150.0);
        AnchorPane.setLeftAnchor(b1,130.0);
        b1.setStyle(
                "-fx-font-size: 32px"
        );
        an.getChildren().addAll(t1,a1,b1);
        // 这个是 点击 增加信息按钮
        b1.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                String sql = "select ID from information where ID=?";
                Connection connection = null;
                //   输入框内容不为空的前提下
                if (!a1.getText().isEmpty()) {
                    try {
                        connection = DriverManager.getConnection(URL, USER, PASSWORD);
                    } catch (SQLException throwables) {
                        throwables.printStackTrace();
                    }
                    PreparedStatement preparedStatement = null;
                    try {
                        preparedStatement = connection.prepareStatement(sql);
                        preparedStatement.setString(1,a1.getText());
                    } catch (SQLException throwables) {
                        throwables.printStackTrace();
                    }
                    ResultSet resultSet = null;
                    try {
                        resultSet = preparedStatement.executeQuery();
                    } catch (SQLException throwables) {
                        throwables.printStackTrace();
                    }
                    try {
                        if (!resultSet.next()) {
                            zhuceshibai zhuceshibai6 = new zhuceshibai(stage1, "没Ta信息,不用改");
                        }else {
                            stage1.close();
                            mod2_information mod2 = new mod2_information(table, stage, a1.getText());
                        }
                    } catch (SQLException throwables) {
                        throwables.printStackTrace();
                    }
                }
                //  输入框内容为空
                if (a1.getText().isEmpty()){
                    chazhaoshibai shibai = new chazhaoshibai(stage1);
                }
            }
        });
    }
}


import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Modality;
import javafx.stage.Stage;
import java.sql.*;
public class mod2_information {
    private final Stage stage1 = new Stage();
    private static final String URL="jdbc:mysql://127.0.0.1:3306/unite1?useUnicode=true&characterEncoding=UTF-8&userSSL=false&serverTimezone=GMT%2B8";//jdbc:mysql//服务器地址/数据库名
    private static final String USER="root";//用户名
    private static final String PASSWORD="zhubao.2003";//密码
    Connection con = null;
    PreparedStatement pst = null;
    ResultSet rs =null ;
    public mod2_information(TableView table, Stage stage,String s) {
        stage1.initOwner(stage);
        stage1.initModality(Modality.WINDOW_MODAL);
        String s1="";
        String s2="";
        String s3="";
        String s4="";
        String s5="";
        String s6="";
        String sss="select * from information where ID=?";
        try {
            con=DriverManager.getConnection(URL,USER,PASSWORD);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        try {
            pst=con.prepareStatement(sss);
            pst.setString(1,s);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        try {
            rs=pst.executeQuery();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        //     从数据库中获取该学号的所有内容,并且展示出来设置成默认值
        while (true){
            try {
                if (!rs.next()) break;
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            try {
                s1=rs.getString(1);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            try {
                s2=rs.getString(2);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            try {
                s3=rs.getString(3);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            try {
                s4=rs.getString(4);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            try {
                s5=rs.getString(5);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            try {
                s6=rs.getString(6);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        stage1.show();  //  让窗口显示出来
        stage1.setTitle("修改信息");
        stage1.setWidth(400);
        stage1.setHeight(480);
        stage1.setX(740);
        stage1.setY(180);
        stage1.setResizable(false);
//        stage1.initOwner(stage);
//        stage1.initModality(Modality.WINDOW_MODAL);
        AnchorPane an = new AnchorPane();
        Scene scene = new Scene(an); // bor 是 方位布局
        stage1.setScene(scene); // 设置场景
        Text t1 = new Text( 30,70,"学号");
        t1.setFont(new Font(30));
        Text t2 = new Text( 30,120,"班级");
        t2.setFont(new Font(30));
        Text t3 = new Text( 30,170,"姓名");
        t3.setFont(new Font(30));
        Text t4 = new Text( 30,220,"性别");
        t4.setFont(new Font(30));
        Text t5 = new Text( 10,270,"出生日期");
        t5.setFont(new Font(20));
        Text t6 = new Text( 30,320,"专业");
        t6.setFont(new Font(30));
        TextField a1 = new TextField();
        AnchorPane.setLeftAnchor(a1,110.0);
        AnchorPane.setTopAnchor(a1,48.0);
        TextField a2 = new TextField();
        AnchorPane.setLeftAnchor(a2,110.0);
        AnchorPane.setTopAnchor(a2,98.0);
        TextField a3 = new TextField();
        AnchorPane.setLeftAnchor(a3,110.0);
        AnchorPane.setTopAnchor(a3,148.0);
        TextField a4 = new TextField();
        AnchorPane.setLeftAnchor(a4,110.0);
        AnchorPane.setTopAnchor(a4,198.0);
        TextField a5 = new TextField();
        AnchorPane.setLeftAnchor(a5,110.0);
        AnchorPane.setTopAnchor(a5,248.0);
        TextField a6 = new TextField();
        AnchorPane.setLeftAnchor(a6,110.0);
        AnchorPane.setTopAnchor(a6,298.0);
        // 设置默认值
        a1.setText(s1);
        a2.setText(s2);
        a3.setText(s3);
        a4.setText(s4);
        a5.setText(s5);
        a6.setText(s6);
        Button b1 = new Button("确认");
        AnchorPane.setTopAnchor(b1,358.0);
        AnchorPane.setLeftAnchor(b1,130.0);
        b1.setStyle(
                "-fx-font-size: 32px"
        );
        an.getChildren().addAll(t1,t2,t3,t4,t5,t6,a1,a2,a3,a4,a5,a6,b1);
        // 这个是 点击 增加信息按钮
        b1.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                // 添加学生信息
                stage.close();
                PreparedStatement pst = null;
                try {
                    //1.加载驱动程序
                    //此语句固定,使用MySQL数据库无需更改,在JSP中可不加异常处理
                    try {
                        Class.forName("com.mysql.cj.jdbc.Driver");
                    } catch (ClassNotFoundException e) {
                        e.printStackTrace();
                    }
                    //2.获取数据库的连接
                    Connection con = DriverManager.getConnection(URL, USER, PASSWORD);
                    //3.通过数据库的连接操作数据库,实现修改数据
                    Statement sql = con.createStatement();
                    try {
                        con = DriverManager.getConnection(URL, USER, PASSWORD);
                        pst = con.prepareStatement("update information set ID=? ,grade=?,name=?,sex=?,birth=?,major=? where ID=?");
                        pst.setString(1, a1.getText());
                        pst.setString(2, a2.getText());
                        pst.setString(3, a3.getText());
                        pst.setString(4, a4.getText());
                        pst.setString(5, a5.getText());
                        pst.setString(6, a6.getText());
                        pst.setString(7,s);
                        pst.execute();
                        Mywindow m1 = new Mywindow();  // 刷新页面
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } catch (SQLException E) {
                    System.out.println("SQL异常!!!!");
                }
                stage1.close();
            }
        });
    }
}

在这里插入图片描述

4.查询功能

查询功能设计的为 模糊查询,即 输入 提示框内的任意一条信息就可以对其进行搜索。

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Modality;
import javafx.stage.Stage;

import java.sql.*;

public class find_information {
    private final Stage stage2 = new Stage();
    String url = "jdbc:mysql://127.0.0.1:3306/数据库名?useUnicode=true&characterEncoding=UTF-8&userSSL=false&serverTimezone=GMT%2B8";
    String user = " ";// 用户名
    String pwdd = " ";  // 密码
    String jdbc = "com.mysql.cj.jdbc.Driver";
    ResultSet rst = null;
    Connection cont = null;
    Statement ppst = null;
    public find_information(TableView table, Stage stage) {
        stage2.initOwner(stage);
        stage2.initModality(Modality.WINDOW_MODAL);
        stage2.show();  //  让窗口显示出来
        stage2.setTitle("查询信息");
        stage2.setWidth(400);
        stage2.setHeight(500);
        stage2.setX(740);
        stage2.setY(180);
        stage2.setResizable(false);
        AnchorPane an = new AnchorPane();
        Scene scene = new Scene(an); // bor 是 方位布局
        stage2.setScene(scene); // 设置场景
        Text t1 = new Text( 30,70,"学号");
        t1.setFont(new Font(30));
        Text t2 = new Text( 30,120,"班级");
        t2.setFont(new Font(30));
        Text t3 = new Text( 30,170,"姓名");
        t3.setFont(new Font(30));
        Text t4 = new Text( 30,220,"性别");
        t4.setFont(new Font(30));
        Text t5 = new Text( 10,270,"出生日期");
        t5.setFont(new Font(20));
        Text t6 = new Text( 30,320,"专业");
        t6.setFont(new Font(30));

        TextField a1 = new TextField();
        AnchorPane.setLeftAnchor(a1,110.0);
        AnchorPane.setTopAnchor(a1,48.0);
        TextField a2 = new TextField();
        AnchorPane.setLeftAnchor(a2,110.0);
        AnchorPane.setTopAnchor(a2,98.0);
        TextField a3 = new TextField();
        AnchorPane.setLeftAnchor(a3,110.0);
        AnchorPane.setTopAnchor(a3,148.0);
        TextField a4 = new TextField();
        AnchorPane.setLeftAnchor(a4,110.0);
        AnchorPane.setTopAnchor(a4,198.0);
        TextField a5 = new TextField();
        AnchorPane.setLeftAnchor(a5,110.0);
        AnchorPane.setTopAnchor(a5,248.0);
        TextField a6 = new TextField();
        AnchorPane.setLeftAnchor(a6,110.0);
        AnchorPane.setTopAnchor(a6,298.0);
        Button b1 = new Button("查找");
        b1.setStyle(
                "-fx-font-size: 32px"
        );
        AnchorPane.setTopAnchor(b1,358.0);
        AnchorPane.setLeftAnchor(b1,150.0);
        an.getChildren().addAll(t1,t2,t3,t4,t5,t6,a1,a2,a3,a4,a5,a6,b1);
        b1.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                stage2.close();
                int ceshi=0;
                String sql3 = "select i.ID,i.grade,i.name,i.sex,i.birth,i.major from information i where ";

                if (!a1.getText().isEmpty()){
                    String s1= "(i.id like '"+a1.getText()+"%'||i.id like '%"+a1.getText()+"'|| i.id like '%"+a1.getText()+"%'|| i.id='"+a1.getText()+"')";
                    ceshi=1;
                    sql3=sql3+s1;
                }
                if (!a2.getText().isEmpty()) {
                    String s2= "(i.grade like '"+a2.getText()+"%'||i.grade like '%"+a2.getText()+"'|| i.grade like '%"+a2.getText()+"%'|| i.grade='"+a2.getText()+"')";
                    if (ceshi>0) {
                        sql3+="AND"+s2;
                    }else {
                        sql3+=s2;
                    }
                    ceshi=1;
                }
                if (!a3.getText().isEmpty()) {
                    String s3= "(i.name like '"+a3.getText()+"%'||i.name like '%"+a3.getText()+"'|| i.name like '%"+a3.getText()+"%'|| i.name='"+a3.getText()+"')";
                    if (ceshi>0) {
                        sql3+="AND"+s3;
                    }else {
                        sql3+=s3;
                    }
                    ceshi=1;
                }
                if (!a4.getText().isEmpty()) {
                    String s4= "(i.sex like '"+a4.getText()+"%'||i.sex like '%"+a4.getText()+"'|| i.sex like '%"+a4.getText()+"%'|| i.sex='"+a4.getText()+"')";
                    if (ceshi>0) {
                        sql3+="AND"+s4;
                    }else {
                        sql3+=s4;
                    }
                    ceshi=1;
                }
                if (!a5.getText().isEmpty()) {
                    String s5= "(i.birth like '"+a5.getText()+"%'||i.birth like '%"+a5.getText()+"'|| i.birth like '%"+a5.getText()+"%'|| i.birth='"+a5.getText()+"')";
                    if (ceshi>0) {
                        sql3+="AND"+s5;
                    }else {
                        sql3+=s5;
                    }
                    ceshi=1;
                }
                if (!a6.getText().isEmpty()) {
                    String s6= "(i.major like '"+a6.getText()+"%'||i.major like '%"+a6.getText()+"'|| i.major like '%"+a6.getText()+"%'|| i.major='"+a6.getText()+"')";
                    if (ceshi>0) {
                        sql3+="AND"+s6;
                    }else {
                        sql3+=s6;
                    }
                    ceshi=1;
                }
                if (ceshi>0) {  // 输入的有关键信息
                    Connection connection= null;
                    try {
                        connection = DriverManager.getConnection(url,user,pwd);
                    } catch (SQLException throwables) {
                        throwables.printStackTrace();
                    }
                    Statement statement = null;
                    try {
                        statement = connection.createStatement();
                    } catch (SQLException throwables) {
                        throwables.printStackTrace();
                    }
                    ResultSet resultSet = null;
                    try {
                        resultSet = statement.executeQuery(sql3);
                    } catch (SQLException throwables) {
                        throwables.printStackTrace();
                    }
                    try {
                        if (!resultSet.next()){
                            zhuceshibai zhuceshibai1 = new zhuceshibai(stage2,"找不到该信息!");
                        }else {
                            leader_log.mywindow.date(table,sql3);
                        }
                    } catch (SQLException throwables) {
                        throwables.printStackTrace();
                    } catch (ClassNotFoundException e) {
                        e.printStackTrace();
                    }
                }else {
                    if (a1.getText().isEmpty()&&a2.getText().isEmpty()&&a3.getText().isEmpty()&&a4.getText().isEmpty()&&a5.getText().isEmpty()&&a6.getText().isEmpty()){
                        zhuceshibai zhuceshibai = new zhuceshibai(stage2,"请输入关键的信息");
                    }else{
                        try {
                            leader_log.mywindow.date(table,sql3);
                        } catch (ClassNotFoundException e) {
                            e.printStackTrace();
                        }
                        stage2.close();
                    }
                }
            }
        });
    }
    }

在这里插入图片描述

三、完整代码展示

由于代码过长,所以本文展示部分的代码,完整代码在下方的百度网盘里,有需要的可以自行提取
链接:https://pan.baidu.com/s/11jUvvYmMQnkTtlAV9f8dFw?pwd=ojbk
提取码:ojbk

四、 总结

第一次学习Mysql,便尝试做了一个学生信息管理系统,避免不了出现个别的错误,欢迎大家在评论区指出,咱们一起讨论。

猜你喜欢

转载自blog.csdn.net/m0_63512120/article/details/127653565