JAVAfx +MySql实现学生信息管理系统

源码:https://download.csdn.net/download/Wksycxy/87454713

文章目录

  • 目录

    一、功能介绍

    ER图

     系统结构图

     登录界面

    学生登录

     管理员登录

    学生信息管理

     学生成绩管理

    二、局部功能实现

    1.View类

    登录界面

    单选框添加监听事件

     注册界面

     提示框界面

    学生操作界面

    个人信息查询界面

    个人成绩查询界面

    退出登陆

    管理员操作界面 

     名字模糊查询表单

     学生信息管理

     学生成绩管理

    2.Connect类 

    简单封装

    增、删、改

    登录功能

     学号查询与成绩查询

    表单中学生类初始化

    模糊查询 

    3.学生类 

     三.总代码

    1.View类

    2.Student 

    4.Main 



前言

最近初学MySQL,利用MySQL与Javafx写了一个小型学生信息管理系统。因为是初学,在很多方面做的还是有一些不足。在写的过程中,也借鉴了其它博主的一些思路,但大部分代码都是自己写的。(不然代码结构也不会这么拉跨)

由于很多地方代码都是嵌套的,所以在文章前面的代码中可能会看着有点乱。如果觉得代码乱的话,可以直接看最后的总码。


提示:以下是本篇文章正文内容,下面案例可供参考

一、功能介绍

ER图

JAVAfx +MySql实现学生信息管理系统_第1张图片

 系统结构图

JAVAfx +MySql实现学生信息管理系统_第2张图片

 登录界面

当单选按钮为学生时,可以登录与注册,当单选按钮为管理员时,只能登录。

JAVAfx +MySql实现学生信息管理系统_第3张图片

学生登录

学生账号登录后可以查看自己的个人信息与成绩

JAVAfx +MySql实现学生信息管理系统_第4张图片

个人信息

JAVAfx +MySql实现学生信息管理系统_第5张图片

 学生本人成绩

 JAVAfx +MySql实现学生信息管理系统_第6张图片

 管理员登录

管理员可以增、删、改、查、所有的学生的信息与成绩,并在修改与增加新数据时进行一定的校验

JAVAfx +MySql实现学生信息管理系统_第7张图片

学生信息管理

  1. 实现了对学生姓名进行模糊查询
  2. 并且在点击某一行数据时,会对数据暂时储存,在点击修改时能使文本框初始值为刚才点击的那一行的数据(这一功能我还是觉得很赞的)

JAVAfx +MySql实现学生信息管理系统_第8张图片

 JAVAfx +MySql实现学生信息管理系统_第9张图片

 学生成绩管理

实现功能:

  1. 多表查询
  2. 模糊查询
  3. 多条件查询
  4. 同上2

JAVAfx +MySql实现学生信息管理系统_第10张图片

 JAVAfx +MySql实现学生信息管理系统_第11张图片

JAVAfx +MySql实现学生信息管理系统_第12张图片

二、局部功能实现

1.View类

这个类写的并不好,甚至有点糟糕,写代码时想要偷懒没有将视图与功能分离出去。以至于现在想要再分离出去需要耗费大量时间,这次引以为戒,下次注意。

下面先单独讲几个我认为不错的地方:

  1. 单选框添加监听事件
  2. 表单
  3. 获取行数据

登录界面

当用户成功登录后,登录界面消失,新的界面出现。于是我将一个舞台对象设为了静态,做为主舞台来使用。

图片路径推荐使用相对路径,但我在idea中用相对路径时,总是找不到文件,多次尝试未果,我才使用了绝对路径。

//主舞台
private static Stage stage;

//背景图片
private static Image img=new Image("D:\\JAVA\\id\\JavaFX\\image\\bg1.jpg");

public void start(Stage stage)throws Exception{
    //将舞台赋值给静态属性
    View.stage=stage;
    stage.setTitlt("学生信息管理系统");
    
    //设置窗口大小不可调节
    stage.setResizable(false);

    //登录
    login();
    stage.show();
}
    static void login() {
        //        新建布局
        GridPane gp = new GridPane();
//        设置居中方式
        gp.setAlignment(Pos.CENTER);
//        调整空隙
        gp.setHgap(10);
        gp.setVgap(20);
//        新建文本标签
        Label l1 = new Label("学号");
        TextField idt = new TextField();

//        用户密码
        Label l2 = new Label("密码");

//        字体
        Font fon1 = Font.font("Times New Roman", FontWeight.BOLD, FontPosture.REGULAR, 20);

        l1.setFont(fon1);
        l2.setFont(fon1);
//        新建密码框
        PasswordField pwd = new PasswordField();
//        按钮的创建

        Button b1 = new Button("登录");
        Button b2 = new Button("注册");

        //        单选框:学生,老师
        ToggleGroup group = new ToggleGroup();
        RadioButton student = new RadioButton("学生");
        RadioButton manager = new RadioButton("管理员");
        HBox h1 = new HBox();
        student.setToggleGroup(group);
        manager.setToggleGroup(group);
        
        h1.getChildren().addAll(student, manager);

//        背景图片

        gp.setBackground(bGround);

        //        添加画板
        gp.add(l1, 0, 0);
        gp.add(idt, 1, 0);
        gp.add(l2, 0, 1);
        gp.add(pwd, 1, 1);
        gp.add(b1, 0, 2);
        gp.add(b2, 1, 2);
        gp.add(h1, 1, 3);
        Scene sc = new Scene(gp, 400, 300);
        stage.setScene(sc);
    }

}

单选框添加监听事件

我想要实现当在登陆页面点击学生或管理员时,界面会发生一定的改变。在尝试了很多代码后,我了解到,想要在未点击按钮时获取单选按钮选中情况,需要给单选按钮添加监听事件。

代码实现: 

//创建单选框 :学生,管理员
ToggleGroup group = new ToggleGroup();
RadioButton student =new RadioButton("学生");
RadioButton manager =new RadioButton("管理员");


//将学生 管理员 绑定
student.setToggleGroup(group);
manager.setToggleGroup(group);


//        添加单选按钮监听
group.selectedToggleProperty().addListener((observable, oldValue, newValue) -> {
    if(newValue.equals(student)){     //如果学生被选中
          l1.setText("学号");
            //点击后界面
    }else if (newValue.eauals(manager)){   //如果管理员被选中
              //隐藏注册按钮
              b2.setVisible(false);
              l1.setText("姓名");
              //点击后界面
    }
});

//创建水平布局,将学生,管理员单选按钮放入
HBox h1=new HBox();
h1.getChildren().addAll(student, manager);

效果展示:

当选择学生时

JAVAfx +MySql实现学生信息管理系统_第13张图片

当选择管理员时

 JAVAfx +MySql实现学生信息管理系统_第14张图片

 注册界面

注册方法和管理员登陆后的“增加”功能原理一直,出于偷懒,我使用的是相同的方法。

我在注册这里添加了一些简单的判断,实现了如下简单功能:

  • 对输入数据进行一些简单的限制,性别只能填男、女,电话只能为11位,任何框不能输入为空。
  • 当在登录界面多次点击注册按钮后,只会生成一个登录页面。
  • 当注册成功或失败时,只会弹出一个提示框
    //    增加界面
    static void add(BorderPane bp) {
        anum++;
        GridPane gp = new GridPane();
//        设置间距
        gp.setVgap(10);
        gp.setHgap(10);
        gp.setAlignment(Pos.CENTER);
//        文本
        Label l1 = new Label("姓名");
        Label l2 = new Label("性别");
        Label l3 = new Label("班级");
        Label l4 = new Label("地址");
        Label l5 = new Label("年龄");
        Label l6 = new Label("电话");
        Label l7 = new Label("密码");
        Label l8=new Label("确认密码");
        Button b1 = new Button("提交");

//        文本框
        TextField t1 = new TextField();
        TextField t2 = new TextField();
        TextField t3 = new TextField();
        TextField t4 = new TextField();
        TextField t5 = new TextField();
        TextField t6 = new TextField();
        PasswordField t7 = new PasswordField();
        PasswordField t8=new PasswordField();

        //        提示文字
        t6.setPromptText("请输入11位号码");

        //监听文本输入的内容
        t2.textProperty().addListener(new ChangeListener() {
            @Override
            public void changed(ObservableValue observable, String oldValue, String newValue) {
                if(newValue.length()<2&&(newValue.equals("男")||newValue.equals("女"))){
                    t2.setText(newValue);
                }else{
                    t2.setText("");
                }
            }
        });


        b1.setOnAction(actionEvent -> {
            if (number == 0) {   //如果不存在提示框
                try {
                    if (t1.getText().trim().equals("") || t2.getText().trim().equals("") || t3.getText().trim().equals("") || t4.getText().trim().equals("") || t5.getText().trim().equals("") || t6.getText().trim().equals("") || t7.getText().trim().equals("")||!t7.getText().equals(t8.getText())||t6.getText().length()!=11) {
                        tips("添加失败");
                    } else {
                        if (Connect.add(t1.getText(), t2.getText(), t3.getText(), t4.getText(), t5.getText(), t6.getText(), t7.getText())) {
                            tips("添加成功");
                            ArrayList students = new ArrayList<>();
                            init(bp, students);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

//        添加到面板中
        gp.add(l1, 0, 0);
        gp.add(t1, 1, 0);
        gp.add(l2, 0, 1);
        gp.add(t2, 1, 1);
        gp.add(l3, 0, 2);
        gp.add(t3, 1, 2);
        gp.add(l4, 0, 3);
        gp.add(t4, 1, 3);
        gp.add(l5, 0, 4);
        gp.add(t5, 1, 4);
        gp.add(l6, 0, 5);
        gp.add(t6, 1, 5);
        gp.add(l7, 0, 6);
        gp.add(t7, 1, 6);
        gp.add(l8,0,7);
        gp.add(t8,1,7);
        gp.add(b1, 1, 8);

//        添加场景舞台
        Scene sc = new Scene(gp, 300, 300);
        Stage stage1 = new Stage();
        stage1.setResizable(false);
//        窗口关闭时减一
        stage1.setOnCloseRequest(windowEvent -> {
            anum--;
        });
        stage1.setScene(sc);
        stage1.setTitle("添加");
        stage1.show();
    }

 提示框界面

当登录失败或者注册失败等操作时,会弹出提示框提示“登陆失败”或“注册失败”等提示框

  • 提示框同时只能存在一个
  • 提示框内容可以更改
    private static int number=0;
    //    提示框
    static void tips(String str) {
//        窗口数+1
        number++;
//        添加文本
        Label l1 = new Label(str);
        Button b1 = new Button("返回");
//        创建面板
        VBox vb = new VBox(l1, b1);
        Scene sc = new Scene(vb, 100, 100);
        vb.setSpacing(10);
        vb.setAlignment(Pos.CENTER);
        Stage stage1 = new Stage();
        stage1.setScene(sc);
//        设置窗口大小不可调节
        stage1.setResizable(false);
        stage1.setOnCloseRequest(windowEvent -> {
            number--;
        });
        b1.setOnAction(actionEvent -> {
            number--;
            stage1.close();
        });
        stage1.setTitle("提示");
        stage1.show();
    }

学生操作界面

在登陆成功时,存储学生id。由于id为主键唯一,通过id查询,以达到不同学生登陆后只能查看自己信息的效果

    private static int id=0;  
  //    学生操作界面
    static void homepage() {
//        创建文本
        Label l5 = new Label("学生查询系统");
        Button l1 = new Button("个人信息");
        Button l3 = new Button("成绩查询");
        Button l4 = new Button("返回登录");
//        设置字体颜色
        l1.setTextFill(Color.BLUE);
        l3.setTextFill(Color.BLUE);
        l4.setTextFill(Color.BLUE);
//        设置字体大小
        Font fon1 = Font.font("Times New Roman", FontWeight.BOLD, FontPosture.REGULAR, 30);
        Font fon2 = Font.font("Times New Roman", FontWeight.BOLD, FontPosture.REGULAR, 20);
        l5.setFont(fon1);
        l1.setFont(fon2);
        l3.setFont(fon2);
        l4.setFont(fon2);

//        个人信息查询
        l1.setOnAction(actionEvent -> {
            query();
        });
//        个人成绩查询
        l3.setOnAction(actionEvent -> {
            try {
                score(id);
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
//        返回登陆
        l4.setOnAction(actionEvent -> {
            login();
        });

//        添加面板
        VBox bv = new VBox(50, l5, l1, l3, l4);
        bv.setAlignment(Pos.CENTER);
        bv.setBackground(bGround);
        Scene sc = new Scene(bv, 500, 600);
        stage.setScene(sc);
    }

个人信息查询界面

  •  创建BorderPane面板,将“首页”加入表单上侧
  • 点击“首页”按钮返回学生操作界面
    //    学生个人信息查询界面
    static void query() {
        BorderPane bp = new BorderPane();
//        顶部
        FlowPane fp1 = new FlowPane();

        Button b3 = new Button("首页");
        b3.setOnAction(actionEvent -> {
            homepage();
        });
        fp1.getChildren().addAll(b3);
        fp1.setHgap(10);
//        设置上下之间的间隙内间距
        fp1.setPadding(new Insets(10));
        bp.setTop(fp1);


//        中部————表格控件
        ArrayList students = new ArrayList<>();
        init(bp, students, id);
//        添加场景
        Scene sc = new Scene(bp);
        stage.setScene(sc);
    }

个人成绩查询界面

懒得设置间距,就用空格手动设置文本与文本框的间距了

    //    学生查看成绩界面
    static void score(int id) throws Exception {

        Student student = new Student();
        Connect.score(student, id);
        Label t1 = new Label("  语文:  " + student.getLanguage());
        Label t2 = new Label("  数学:  " + student.getMath());
        Label t3 = new Label("  英语:  " + student.getEnglish());
        Label t4 = new Label("  物理:  " + student.getPhysics());
        Label t5 = new Label("  化学:  " + student.getChemistry());
        Label t6 = new Label("  生物:  " + student.getBiology());
        Label t7 = new Label("  总分:  " + student.getScore());

//        字体
        Font fon2 = Font.font("Times New Roman", FontWeight.BOLD, FontPosture.REGULAR, 20);
//        设置字体
        t1.setFont(fon2);
        t2.setFont(fon2);
        t3.setFont(fon2);
        t4.setFont(fon2);
        t5.setFont(fon2);
        t6.setFont(fon2);
        t7.setFont(fon2);


        Button b1 = new Button("返回");
        b1.setOnAction(actionEvent -> {
            homepage();
        });

        VBox vb = new VBox(t1, t2, t3, t4, t5, t6, t7, b1);
        vb.setAlignment(Pos.CENTER);
        vb.setSpacing(10);
        vb.setBackground(bGround);
        Scene sc = new Scene(vb, 500, 600);
        stage.setScene(sc);
    }

退出登陆

我在这里直接重新调用了登录界面...

管理员操作界面 

    //    管理员操作界面
    static void maghomepage() {
//        创建文本
        Label l5 = new Label("学生查询系统");
        Button l1 = new Button("学生信息管理");
        Button l2 = new Button("学生成绩管理");
        Button l3 = new Button("返回登陆");
//        设置字体颜色
        l1.setTextFill(Color.BLUE);
        l2.setTextFill(Color.BLUE);
        l3.setTextFill(Color.BLUE);

//        设置字体大小
        Font fon1 = Font.font("Times New Roman", FontWeight.BOLD, FontPosture.REGULAR, 30);
        Font fon2 = Font.font("Times New Roman", FontWeight.BOLD, FontPosture.REGULAR, 20);
        l5.setFont(fon1);
        l1.setFont(fon2);
        l2.setFont(fon2);
        l3.setFont(fon2);

//        学生信息管理
        l1.setOnAction(actionEvent -> {
            magquery();
        });

//        学生成绩管理
        l2.setOnAction(actionEvent -> {
            magscore();
        });
//        返回登陆
        l3.setOnAction(actionEvent -> {
            login();
        });

//        添加面板
        VBox bv = new VBox(50, l5, l1, l2, l3);
        bv.setAlignment(Pos.CENTER);
        bv.setBackground(bGround);
        Scene sc = new Scene(bv, 500, 600);
        stage.setScene(sc);
    }

 名字模糊查询表单

    //    名字模糊查询表格
    static void init(BorderPane bp, ArrayList students, String name) {
        //        中部————表格控件
        TableView tv = new TableView();
//        创建列
        TableColumn c1 = new TableColumn("学号");
        TableColumn c2 = new TableColumn("姓名");
        TableColumn c3 = new TableColumn("性别");
        TableColumn c4 = new TableColumn("班级");
        TableColumn c5 = new TableColumn("地址");
        TableColumn c6 = new TableColumn("年龄");
        TableColumn c7 = new TableColumn("电话");
        TableColumn c8 = new TableColumn("密码");
//        表格列宽宽度设置
        c1.setMinWidth(100);
        c2.setMinWidth(100);
        c3.setMinWidth(100);
        c4.setMinWidth(100);
        c5.setMinWidth(100);
        c6.setMinWidth(100);
        c7.setMinWidth(100);
        c8.setMinWidth(100);

//        确定数据导入的列
        c1.setCellValueFactory(new PropertyValueFactory<>("id"));
        c2.setCellValueFactory(new PropertyValueFactory<>("name"));
        c3.setCellValueFactory(new PropertyValueFactory<>("gender"));
        c4.setCellValueFactory(new PropertyValueFactory<>("clas"));
        c5.setCellValueFactory(new PropertyValueFactory<>("address"));
        c6.setCellValueFactory(new PropertyValueFactory<>("age"));
        c7.setCellValueFactory(new PropertyValueFactory<>("phone"));
        c8.setCellValueFactory(new PropertyValueFactory<>("password"));

        tv.getColumns().addAll(c1, c2, c3, c4, c5, c6, c7,c8);
        //        创建学生集合
        try {
            Connect.inquire(students, name);
        } catch (Exception e) {
            e.printStackTrace();
        }

        tv.getItems().addAll(students);
        bp.setCenter(tv);
    }

 学生信息管理

    //    学生信息管理
    static void magquery() {
        BorderPane bp = new BorderPane();
//        顶部
        FlowPane fp1 = new FlowPane();
        Label l1 = new Label("姓名");
//        Label l2 =new Label("班级");
        TextField f1 = new TextField();
//        TextField f2 = new TextField();
        Button b1 = new Button("查询");
//        模糊查询
        b1.setOnAction(actionEvent -> {
            ArrayList students = new ArrayList<>();
            init(bp, students, f1.getText());
        });
        Button b2 = new Button("刷新");
        b2.setOnAction(actionEvent -> {
            ArrayList students = new ArrayList<>();
            init(bp, students);
        });
        fp1.getChildren().addAll(l1, f1, b1, b2);
        fp1.setHgap(10);
//        设置上下之间的间隙内间距
        fp1.setPadding(new Insets(10));
        bp.setTop(fp1);

//        底部
        FlowPane fp2 = new FlowPane();
        Button b3 = new Button("增加");
        Button b4 = new Button("删除");
        Button b5 = new Button("修改");
        Button b6 = new Button("退出");
//        增加
        b3.setOnAction(actionEvent -> {
            if (anum == 0) {
                add(bp);

            }
        });
//        删除
        b4.setOnAction(actionEvent -> {
            if (anum == 0) {
                delete(bp);
            }
        });
//        修改
        b5.setOnAction(actionEvent -> {
            if (anum == 0) {
                alter(bp);
            }
        });
        b6.setOnAction((actionEvent -> {
            maghomepage();
        }));
        fp2.getChildren().addAll(b3, b4, b5, b6);
        fp2.setHgap(10);
        fp2.setPadding(new Insets(10));
        bp.setBottom(fp2);

//        中部————表格控件
        ArrayList students = new ArrayList<>();
        init(bp, students);
//        添加场景
        Scene sc = new Scene(bp);
        stage.setScene(sc);
    }

 学生成绩管理

在表单上方加入了两个搜索框。一个为班级,一个为姓名。两个都可以单独,或者共同进行模糊查询。因为每种查询方式所用到的sql'语句不同,于是我在点击查询后对文本框的数据首先进行了一些简单的判断。

    //    学生成绩管理界面
    static void magscore() {
        BorderPane bp = new BorderPane();
//        顶部
        FlowPane fp1 = new FlowPane();

        Label l1 = new Label("姓名");
        Label l3 = new Label("班级");

        TextField f1 = new TextField();
        TextField f3 = new TextField();

        Button b1 = new Button("查询");
        Button b3 = new Button("刷新");

//        模糊查询
        b1.setOnAction(actionEvent -> {
            if (!f1.getText().trim().equals("") && f3.getText().trim().equals("")) {   //姓名
                ArrayList students = new ArrayList<>();
                maginit(bp, students, f1.getText(), 1);
            } else if (f1.getText().trim().equals("") && !f3.getText().trim().equals("")) {  //班级
                ArrayList students = new ArrayList<>();
                maginit(bp, students, f3.getText(), 2);
            } else if (!f1.getText().trim().equals("") && !f3.getText().trim().equals("")) {  //姓名+班级
                ArrayList students = new ArrayList<>();
                maginit1(bp, students, f1.getText(), f3.getText());
            }
        });
        b3.setOnAction(actionEvent -> {
            ArrayList students = new ArrayList<>();
            maginit(bp, students);
        });
        fp1.getChildren().addAll(l1, f1, l3, f3, b1, b3);
        fp1.setHgap(10);
//        设置上下之间的间隙内间距
        fp1.setPadding(new Insets(10));
        bp.setTop(fp1);

//        底部
        FlowPane fp2 = new FlowPane();
        Button b5 = new Button("修改");
        Button b6 = new Button("退出");

//        修改
        b5.setOnAction(actionEvent -> {
            if (anum == 0) {
                magalter(bp);
            }
        });
        b6.setOnAction((actionEvent -> {
            maghomepage();
        }));
        fp2.getChildren().addAll(b5, b6);
        fp2.setHgap(10);
        fp2.setPadding(new Insets(10));
        bp.setBottom(fp2);

//        中部————表格控件
        ArrayList students = new ArrayList<>();
        maginit(bp, students);
//        添加场景
        Scene sc = new Scene(bp);
        stage.setScene(sc);
    }

2.Connect类 

简单封装

JDBC在使用过程中,有一部分代码是固定不变的,将这部分代码封装。

/**
     * URL地址
     */
    private static final String URL = "jdbc:mysql://127.0.0.1:3306/ycx?useSSL=false";
    /**
     * 登录数据库服务器的账号
     */
    private static final String USER = "root";
    /**
     * 登录数据库服务器的密码
     */
    private static final String PASSWORD = "15194538986a";

    /**
     * 返回数据库连接对象
     *
     * @return
     */
    public static Connection getConn() {
        try {
            return DriverManager.getConnection(URL, USER, PASSWORD);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 关闭资源
     *
     * @param rs
     * @param stat
     * @param conn
     */
    public static void close(ResultSet rs, Statement stat, Connection conn) {
        try {
            if (rs != null) {
                rs.close();
            }
            if (stat != null) {
                stat.close();
            }
            if (conn != null) {
                conn.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 封装通用的更新操作(即通过该方法实现对弈任意数据表的insert,update,delete操作)
     *
     * @param sql    需要执行的SQL语句
     * @param params 执行SQL语句时需要传递进去参数
     * @return 执行结果
     */
    public static boolean exeUpdate(String sql, Object... params) {
        //获取连接对象
        Connection conn = getConn();
        PreparedStatement ps = null;
        try {
            //获取预编译对象
            ps = conn.prepareStatement(sql);
            //执行参数赋值操作
            if (Objects.nonNull(params)) {
                //循环将所有的参数赋值
                for (int i = 0; i < params.length; i++) {
                    ps.setObject(i + 1, params[i]);
                }
            }
            //执行更新
            return ps.executeUpdate() > 0;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //关闭资源
            close(null, ps, conn);
        }
        return false;
    }

增、删、改

    //    增加
    public static boolean add(String name, String gender, String clas, String address,
                              String age, String phone, String password) throws Exception {
        String sql = "insert into student(name,gender,clas,address,age,phone,password) values (?,?,?,?,?,?,?)";
        return exeUpdate(sql, name, gender, clas, address, age, phone, password);

    }

    //    修改
    public static boolean update(String name, String gender, String clas, String address,
                                 String age, String phone, String password, int id) throws Exception {
        String sql = "update student set name=?,gender=?,clas=?,address=?,age=?,phone=?,password=? where id=?";
        return exeUpdate(sql, name, gender, clas, address, age, phone, password, id);

    }

    //    删除
    public static boolean delete(int id) {
        String sql = "delete from student where id=?";
        return exeUpdate(sql, id);
    }

    //    学生成绩修改
    public static boolean magupdate(int id, int language, int math, int english, int physics, int chemistry,
                                    int biology) throws Exception {
        String sql1 = "update score set language=?,math=?,english=? where student_id=?";
        String sql2 = "update elective set physics=?,chemistry=?,biology=? where student_id=?";
        return exeUpdate(sql1, language, math, english, id) && exeUpdate(sql2, physics, chemistry, biology, id);

    }

登录功能

 实现登录功能需要与数据库进行连接。判断输入的账号与密码在数据库中多条件查询是否存在结果。

   //    学生登录
    public static int login(int id, String pwd) throws Exception {

        String sql = "select * from student where id=? and password=?";
        PreparedStatement pstmt = getConn().prepareStatement(sql);
        pstmt.setInt(1, id);
        pstmt.setString(2, pwd);
        ResultSet rs = pstmt.executeQuery();
        if (rs.next()) {

            return rs.getInt("id");
        } else {

            return 0;
        }
    }
   //    管理员登录
    public static boolean maglogin(String name, String pwd) throws Exception {

        String sql = "select * from admin_login_k where admin_id=? and admin_pass=?";
        PreparedStatement pstmt = getConn().prepareStatement(sql);
        pstmt.setString(1, name);
        pstmt.setString(2, pwd);
        ResultSet rs = pstmt.executeQuery();
        if (rs.next()) {
            return true;
        } else {
            return false;
        }
    }

 学号查询与成绩查询

由于刚开始写的管理系统学生登陆系统通过名字与密码登录,所以需要获取学号。后面我换成了学号和密码登录,学号查询已经没有存在的意义了。但是我懒得改了...

    //        学号查询
    public static void inquire(ArrayList list, int id) throws Exception {

        String sql = "select * from student where id=?";
        PreparedStatement pstmt = getConn().prepareStatement(sql);
        pstmt.setInt(1, id);
        ResultSet rs = pstmt.executeQuery();
        if (rs.next()) {
            Student student = new Student();
            student.setId(rs.getInt("id"));
            student.setName(rs.getString("name"));
            student.setGender(rs.getString("gender"));
            student.setClas(rs.getString("clas"));
            student.setAddress(rs.getString("address"));
            student.setAge(rs.getString("age"));
            student.setPhone(rs.getString("phone"));
            list.add(student);
        }
        //        释放资源
        rs.close();
        pstmt.close();
    }

    //    成绩查询--学生
    public static void score(Student student, int id) throws Exception {

        String sql = "select id,name,clas,language,math,english,physics,chemistry,biology,language+math+english+physics+chemistry+biology 'score' from student,score,elective where id=score.student_id and id=elective.student_id and id=?;";
        PreparedStatement pstmt = getConn().prepareStatement(sql);
        pstmt.setInt(1, id);
        ResultSet rs = pstmt.executeQuery();
        if (rs.next()) {
            student.setLanguage(rs.getInt("language"));
            student.setMath(rs.getInt("math"));
            student.setEnglish(rs.getInt("english"));
            student.setLanguage(rs.getInt("language"));
            student.setMath(rs.getInt("math"));
            student.setEnglish(rs.getInt("english"));
            student.setPhysics(rs.getInt("physics"));
            student.setChemistry(rs.getInt("chemistry"));
            student.setBiology(rs.getInt("biology"));
            student.setScore(rs.getInt("score"));
        }
        //        释放资源
        rs.close();
        pstmt.close();
    }

表单中学生类初始化

    //   学生类初始化
    public static void student(ArrayList list) throws Exception {
        String sql = "select * from student ";
        PreparedStatement pstmt = getConn().prepareStatement(sql);
        ResultSet rs = pstmt.executeQuery();
        while (rs.next()) {

//            获取数据
            int id = rs.getInt("id");
            String name = rs.getString("name");
            String gender = rs.getString("gender");
            String clas = rs.getString("clas");
            String address = rs.getString("address");
            String age = rs.getString("age");
            String phone = rs.getString("phone");
            String password=rs.getString("password");

//            封装数据
            Student student = new Student();
            student.setId(id);
            student.setName(name);
            student.setGender(gender);
            student.setClas(clas);
            student.setAddress(address);
            student.setAge(age);
            student.setPhone(phone);
            student.setPassword(password);

            list.add(student);
        }
//        释放资源
        rs.close();
        pstmt.close();
    }

    //   学生类成绩初始化--管理员
    public static void magstudent(ArrayList list) throws Exception {
        String sql = "select id,name,clas,language,math,english,physics,chemistry,biology,language+math+english+physics+chemistry+biology 'score' from student,score,elective where id=score.student_id and id=elective.student_id; ";
        PreparedStatement pstmt = getConn().prepareStatement(sql);
        ResultSet rs = pstmt.executeQuery();
        while (rs.next()) {

//            获取数据
            int id = rs.getInt("id");
            String name = rs.getString("name");
            String clas = rs.getString("clas");
            int language = rs.getInt("language");
            int math = rs.getInt("math");
            int english = rs.getInt("english");
            int physics = rs.getInt("physics");
            int chemistry = rs.getInt("chemistry");
            int biology = rs.getInt("biology");
            int score = rs.getInt("score");

//            封装数据
            Student student = new Student();
            student.setId(id);
            student.setName(name);
            student.setClas(clas);
            student.setLanguage(language);
            student.setMath(math);
            student.setEnglish(english);
            student.setPhysics(physics);
            student.setChemistry(chemistry);
            student.setBiology(biology);
            student.setScore(score);

            list.add(student);
        }
//        释放资源
        rs.close();
        pstmt.close();
    }

模糊查询 

表单中需要传入学生类的集合,在不同操作下传入的学生类的集合不同。将符合模糊查询条件的学生对象传入加载入学生集合中,并上传至学生类。

    //        模糊查询
    public static void inquire(ArrayList list, String name) throws Exception {

        String sql = "select * from student where name like ?";
        PreparedStatement pstmt = getConn().prepareStatement(sql);
        pstmt.setString(1, "%" + name + "%");
        ResultSet rs = pstmt.executeQuery();
        while(rs.next()) {
            Student student = new Student();
            student.setId(rs.getInt("id"));
            student.setName(rs.getString("name"));
            student.setGender(rs.getString("gender"));
            student.setClas(rs.getString("clas"));
            student.setAddress(rs.getString("address"));
            student.setAge(rs.getString("age"));
            student.setPhone(rs.getString("phone"));
            student.setPassword(rs.getString("password"));
            list.add(student);
        }
        //        释放资源
        rs.close();
        pstmt.close();
    }
    //        模糊查询--学生成绩
    public static void maginquire(ArrayList list, String s, int a) throws Exception {

        String sql = "";
        if (a == 1) {   //名字模糊查询
            sql = "select id,name,clas,language,math,english,physics,chemistry,biology,language+math+english+physics+chemistry+biology 'score' from student,score,elective where id=score.student_id and id=elective.student_id and name like?;";
        } else if (a == 2) {  //班级模糊查询
            sql = "select id,name,clas,language,math,english,physics,chemistry,biology,language+math+english+physics+chemistry+biology 'score' from student,score,elective where id=score.student_id and id=elective.student_id and clas like?;";
        }
        PreparedStatement pstmt = getConn().prepareStatement(sql);
        pstmt.setString(1, "%" + s + "%");
        ResultSet rs = pstmt.executeQuery();
        while(rs.next()){
            Student student = new Student();
            student.setId(rs.getInt("id"));
            student.setName(rs.getString("name"));
            student.setClas(rs.getString("clas"));
            student.setLanguage(rs.getInt("language"));
            student.setMath(rs.getInt("math"));
            student.setEnglish(rs.getInt("english"));
            student.setPhysics(rs.getInt("physics"));
            student.setChemistry(rs.getInt("chemistry"));
            student.setBiology(rs.getInt("biology"));
            student.setScore(rs.getInt("score"));
            list.add(student);
        }
        //        释放资源
        rs.close();
        pstmt.close();
    }

    //        模糊查询--学生成绩
    public static void maginquire(ArrayList list, String name, String clas) throws Exception {


        //名字+班级
        String sql = "select id,name,clas,language,math,english,physics,chemistry,biology,language+math+english+physics+chemistry+biology 'score' from student,score,elective where id=score.student_id and id=elective.student_id and  name like? and  clas like?;";

        PreparedStatement pstmt = getConn().prepareStatement(sql);
        pstmt.setString(1, "%" + name + "%");
        pstmt.setString(2, "%" + clas + "%");
        ResultSet rs = pstmt.executeQuery();
        while (rs.next()) {
            Student student = new Student();
            student.setId(rs.getInt("id"));
            student.setName(rs.getString("name"));
            student.setClas(rs.getString("clas"));
            student.setLanguage(rs.getInt("language"));
            student.setMath(rs.getInt("math"));
            student.setEnglish(rs.getInt("english"));
            student.setPhysics(rs.getInt("physics"));
            student.setChemistry(rs.getInt("chemistry"));
            student.setBiology(rs.getInt("biology"));
            student.setScore(rs.getInt("score"));
            list.add(student);
        }
        //        释放资源
        rs.close();
        pstmt.close();
    }

3.学生类 

public class Student {
    private int id;
    private String name;
    private String gender;
    private String clas;
    private String address;
    private String age;
    private String phone;
    private String password;
    private int language;
    private int math;
    private int english;
    private int physics;
    private int chemistry;
    private int biology;
    private int score;

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    public int getLanguage() {
        return language;
    }

    public void setLanguage(int language) {
        this.language = language;
    }

    public int getMath() {
        return math;
    }

    public void setMath(int math) {
        this.math = math;
    }

    public int getEnglish() {
        return english;
    }

    public void setEnglish(int english) {
        this.english = english;
    }

    public int getPhysics() {
        return physics;
    }

    public void setPhysics(int physics) {
        this.physics = physics;
    }

    public int getChemistry() {
        return chemistry;
    }

    public void setChemistry(int chemistry) {
        this.chemistry = chemistry;
    }

    public int getBiology() {
        return biology;
    }

    public void setBiology(int biology) {
        this.biology = biology;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getClas() {
        return clas;
    }

    public void setClas(String clas) {
        this.clas = clas;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

}

 三.总代码

1.View类


import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.image.Image;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;
import javafx.util.Callback;

import java.util.ArrayList;


public class View extends Application {
    //    登录学生学号
    private static int id;
    private static Stage stage;
    //    提示框数目
    private static int number = 0;
    //    增删改查的页面数
    private static int anum = 0;
    private static Student student0;

    private static Image img=new Image("D:\\JAVA\\id\\JavaFX\\image\\bg1.jpg" +
            "");
    private static BackgroundImage bImg = new BackgroundImage(img,
            BackgroundRepeat.NO_REPEAT,
            BackgroundRepeat.NO_REPEAT,
            BackgroundPosition.DEFAULT,
            BackgroundSize.DEFAULT);
    private static Background bGround = new Background(bImg);

    @Override
    public void start(Stage stage) throws Exception {
//        将舞台复制给静态属性
        View.stage = stage;
        stage.setTitle("学生信息管理系统");

//        设置窗口大小不可调节
        stage.setResizable(false);
//        登录
        login();
        stage.show();
    }

    //     登录界面
    static void login() {
        //        新建布局
        GridPane gp = new GridPane();
//        设置居中方式
        gp.setAlignment(Pos.CENTER);
//        调整空隙
        gp.setHgap(10);
        gp.setVgap(10);
//        新建文本标签
        Label l1 = new Label("姓名");
        TextField name = new TextField();

//        用户密码
        Label l2 = new Label("密码");

//        字体
        Font fon1 = Font.font("Times New Roman", FontWeight.BOLD, FontPosture.REGULAR, 20);

        l1.setFont(fon1);
        l2.setFont(fon1);
//        新建密码框
        PasswordField pwd = new PasswordField();


//        按钮的创建

        Button b1 = new Button("登录");
        Button b2 = new Button("注册");

        //        单选框:学生,老师
        ToggleGroup group = new ToggleGroup();
        RadioButton student = new RadioButton("学生");
        RadioButton manager = new RadioButton("管理员");
        HBox h1 = new HBox();
        student.setToggleGroup(group);
        manager.setToggleGroup(group);

//        添加单选按钮监听
        group.selectedToggleProperty().addListener((observable, oldValue, newValue) -> {
            if (newValue.equals(student)) {   //单选按钮为学生
                b1.setOnAction(actionEvent -> {
                    try {
                        if (Connect.login(name.getText(), pwd.getText()) > 0) {
                            id = Connect.login(name.getText(), pwd.getText());
                            homepage();
                        } else {
                            if (number == 0) {
                                tips("登陆失败");
                            }
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                });
                //        注册按钮事件
                b2.setOnAction(actionEvent -> {
                    BorderPane bp = new BorderPane();
                    add(bp);
                });
//                设置”注册“按钮可见
                b2.setVisible(true);
            } else if (newValue.equals(manager)) {
//                设置”注册“按钮不可见
                b2.setVisible(false);
                b1.setOnAction(actionEvent -> {
                    try {
                        if (Connect.maglogin(name.getText(), pwd.getText())) {
                            maghomepage();
                        } else {
                            if (number == 0) {
                                tips("登录失败");
                            }
                        }
                        ;
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                });

            }
        });
        h1.getChildren().addAll(student, manager);

//        背景图片

        gp.setBackground(bGround);

        //        添加画板
        gp.add(l1, 0, 0);
        gp.add(name, 1, 0);
        gp.add(l2, 0, 1);
        gp.add(pwd, 1, 1);
        gp.add(b1, 0, 2);
        gp.add(b2, 1, 2);
        gp.add(h1, 1, 3);
        Scene sc = new Scene(gp, 500, 600);
        stage.setScene(sc);
    }


    //    注册页面
    static void regist() {
        //        新建文本标签
        Label l1 = new Label("姓名");
        TextField name = new TextField();
//        用户密码
        Label l2 = new Label("密码");
//        新建密码框
        PasswordField pwd = new PasswordField();

//        按钮的创建
        Button b2 = new Button("注册");
        Button b3 = new Button("取消");

//            创建注册画板
        GridPane gp2 = new GridPane();

//            设置居中方式
        gp2.setAlignment(Pos.CENTER);
//            调整空隙
        gp2.setHgap(10);
        gp2.setVgap(10);

//            姓名
        gp2.add(l1, 0, 0);
        gp2.add(name, 1, 0);

//            密码
        gp2.add(l2, 0, 1);
        gp2.add(pwd, 1, 1);


//            按钮
        gp2.add(b2, 0, 3);
        gp2.add(b3, 1, 3);


        Scene sc2 = new Scene(gp2, 500, 600);
        stage.setScene(sc2);

        //        点击注册
        b2.setOnAction(actionEvent -> {
            try {
//                Connect.enroll(name.getText(), pwd.getText());
                tips("注册成功");
            } catch (Exception e) {
                e.printStackTrace();
            }
        });

//        点击取消,返回登录界面
        b3.setOnAction(actionEvent -> {
            login();
        });
    }

    //    学生操作界面
    static void homepage() {
//        创建文本
        Label l5 = new Label("学生查询系统");
        Button l1 = new Button("个人信息");
        Button l3 = new Button("成绩查询");
        Button l4 = new Button("返回登录");
//        设置字体颜色
        l1.setTextFill(Color.BLUE);
        l3.setTextFill(Color.BLUE);
        l4.setTextFill(Color.BLUE);
//        设置字体大小
        Font fon1 = Font.font("Times New Roman", FontWeight.BOLD, FontPosture.REGULAR, 30);
        Font fon2 = Font.font("Times New Roman", FontWeight.BOLD, FontPosture.REGULAR, 20);
        l5.setFont(fon1);
        l1.setFont(fon2);
        l3.setFont(fon2);
        l4.setFont(fon2);

//        学生信息查询
        l1.setOnAction(actionEvent -> {
            query();
        });
//        成绩查询
        l3.setOnAction(actionEvent -> {
            try {
                score(id);
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
//        返回登陆
        l4.setOnAction(actionEvent -> {
            login();
        });

//        添加面板
        VBox bv = new VBox(50, l5, l1, l3, l4);
        bv.setAlignment(Pos.CENTER);
        bv.setBackground(bGround);
        Scene sc = new Scene(bv, 500, 600);
        stage.setScene(sc);
    }

    //    管理员操作界面
    static void maghomepage() {
//        创建文本
        Label l5 = new Label("学生查询系统");
        Button l1 = new Button("学生信息管理");
        Button l2 = new Button("学生成绩管理");
        Button l3 = new Button("返回登陆");
//        设置字体颜色
        l1.setTextFill(Color.BLUE);
        l2.setTextFill(Color.BLUE);
        l3.setTextFill(Color.BLUE);

//        设置字体大小
        Font fon1 = Font.font("Times New Roman", FontWeight.BOLD, FontPosture.REGULAR, 30);
        Font fon2 = Font.font("Times New Roman", FontWeight.BOLD, FontPosture.REGULAR, 20);
        l5.setFont(fon1);
        l1.setFont(fon2);
        l2.setFont(fon2);
        l3.setFont(fon2);

//        学生信息管理
        l1.setOnAction(actionEvent -> {
            magquery();
        });

//        学生成绩管理
        l2.setOnAction(actionEvent -> {
            magscore();
        });
//        返回登陆
        l3.setOnAction(actionEvent -> {
            login();
        });

//        添加面板
        VBox bv = new VBox(50, l5, l1, l2, l3);
        bv.setAlignment(Pos.CENTER);
        bv.setBackground(bGround);
        Scene sc = new Scene(bv, 500, 600);
        stage.setScene(sc);
    }


    //    学生信息查询界面
    static void query() {
        BorderPane bp = new BorderPane();
//        顶部
        FlowPane fp1 = new FlowPane();

        Button b3 = new Button("首页");
        b3.setOnAction(actionEvent -> {
            homepage();
        });
        fp1.getChildren().addAll(b3);
        fp1.setHgap(10);
//        设置上下之间的间隙内间距
        fp1.setPadding(new Insets(10));
        bp.setTop(fp1);


//        中部————表格控件
        ArrayList students = new ArrayList<>();
        init(bp, students, id);
//        添加场景
        Scene sc = new Scene(bp);
        stage.setScene(sc);
    }

    //    学生信息管理
    static void magquery() {
        BorderPane bp = new BorderPane();
//        顶部
        FlowPane fp1 = new FlowPane();
        Label l1 = new Label("姓名");
//        Label l2 =new Label("班级");
        TextField f1 = new TextField();
//        TextField f2 = new TextField();
        Button b1 = new Button("查询");
//        模糊查询
        b1.setOnAction(actionEvent -> {
            ArrayList students = new ArrayList<>();
            init(bp, students, f1.getText());
        });
        Button b2 = new Button("刷新");
        b2.setOnAction(actionEvent -> {
            ArrayList students = new ArrayList<>();
            init(bp, students);
        });
        fp1.getChildren().addAll(l1, f1, b1, b2);
        fp1.setHgap(10);
//        设置上下之间的间隙内间距
        fp1.setPadding(new Insets(10));
        bp.setTop(fp1);

//        底部
        FlowPane fp2 = new FlowPane();
        Button b3 = new Button("增加");
        Button b4 = new Button("删除");
        Button b5 = new Button("修改");
        Button b6 = new Button("退出");
//        增加
        b3.setOnAction(actionEvent -> {
            if (anum == 0) {
                add(bp);

            }
        });
//        删除
        b4.setOnAction(actionEvent -> {
            if (anum == 0) {
                delete(bp);
            }
        });
//        修改
        b5.setOnAction(actionEvent -> {
            if (anum == 0) {
                alter(bp);
            }
        });
        b6.setOnAction((actionEvent -> {
            maghomepage();
        }));
        fp2.getChildren().addAll(b3, b4, b5, b6);
        fp2.setHgap(10);
        fp2.setPadding(new Insets(10));
        bp.setBottom(fp2);

//        中部————表格控件
        ArrayList students = new ArrayList<>();
        init(bp, students);
//        添加场景
        Scene sc = new Scene(bp);
        stage.setScene(sc);
    }

    //    学生查看成绩界面
    static void score(int id) throws Exception {

        Student student = new Student();
        Connect.score(student, id);
        Label t1 = new Label("  语文:  " + student.getLanguage());
        Label t2 = new Label("  数学:  " + student.getMath());
        Label t3 = new Label("  英语:  " + student.getEnglish());
        Label t4 = new Label("  物理:  " + student.getPhysics());
        Label t5 = new Label("  化学:  " + student.getChemistry());
        Label t6 = new Label("  生物:  " + student.getBiology());
        Label t7 = new Label("  总分:  " + student.getScore());

//        字体
        Font fon2 = Font.font("Times New Roman", FontWeight.BOLD, FontPosture.REGULAR, 20);
//        设置字体
        t1.setFont(fon2);
        t2.setFont(fon2);
        t3.setFont(fon2);
        t4.setFont(fon2);
        t5.setFont(fon2);
        t6.setFont(fon2);
        t7.setFont(fon2);


        Button b1 = new Button("返回");
        b1.setOnAction(actionEvent -> {
            homepage();
        });

        VBox vb = new VBox(t1, t2, t3, t4, t5, t6, t7, b1);
        vb.setAlignment(Pos.CENTER);
        vb.setSpacing(10);
        vb.setBackground(bGround);
        Scene sc = new Scene(vb, 500, 600);
        stage.setScene(sc);
    }

    //    增加界面
    static void add(BorderPane bp) {
        anum++;
        GridPane gp = new GridPane();
//        设置间距
        gp.setVgap(10);
        gp.setHgap(10);
        gp.setAlignment(Pos.CENTER);
//        文本
        Label l1 = new Label("姓名");
        Label l2 = new Label("性别");
        Label l3 = new Label("班级");
        Label l4 = new Label("地址");
        Label l5 = new Label("年龄");
        Label l6 = new Label("电话");
        Label l7 = new Label("密码");
        Label l8=new Label("确认密码");
        Button b1 = new Button("提交");

//        文本框
        TextField t1 = new TextField();
        TextField t2 = new TextField();
        TextField t3 = new TextField();
        TextField t4 = new TextField();
        TextField t5 = new TextField();
        TextField t6 = new TextField();
        PasswordField t7 = new PasswordField();
        PasswordField t8=new PasswordField();

        //        提示文字
        t6.setPromptText("请输入11位号码");

        //监听文本输入的内容
        t2.textProperty().addListener(new ChangeListener() {
            @Override
            public void changed(ObservableValue observable, String oldValue, String newValue) {
                if(newValue.length()<2&&(newValue.equals("男")||newValue.equals("女"))){
                    t2.setText(newValue);
                }else{
                    t2.setText("");
                }
            }
        });


        b1.setOnAction(actionEvent -> {
            if (number == 0) {   //如果不存在提示框
                try {
                    if (t1.getText().trim().equals("") || t2.getText().trim().equals("") || t3.getText().trim().equals("") || t4.getText().trim().equals("") || t5.getText().trim().equals("") || t6.getText().trim().equals("") || t7.getText().trim().equals("")||!t7.getText().equals(t8.getText())||t6.getText().length()!=11) {
                        tips("添加失败");
                    } else {
                        if (Connect.add(t1.getText(), t2.getText(), t3.getText(), t4.getText(), t5.getText(), t6.getText(), t7.getText())) {
                            tips("添加成功");
                            ArrayList students = new ArrayList<>();
                            init(bp, students);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

//        添加到面板中
        gp.add(l1, 0, 0);
        gp.add(t1, 1, 0);
        gp.add(l2, 0, 1);
        gp.add(t2, 1, 1);
        gp.add(l3, 0, 2);
        gp.add(t3, 1, 2);
        gp.add(l4, 0, 3);
        gp.add(t4, 1, 3);
        gp.add(l5, 0, 4);
        gp.add(t5, 1, 4);
        gp.add(l6, 0, 5);
        gp.add(t6, 1, 5);
        gp.add(l7, 0, 6);
        gp.add(t7, 1, 6);
        gp.add(l8,0,7);
        gp.add(t8,1,7);
        gp.add(b1, 1, 8);

//        添加场景舞台
        Scene sc = new Scene(gp, 300, 300);
        Stage stage1 = new Stage();
        stage1.setResizable(false);
//        窗口关闭时减一
        stage1.setOnCloseRequest(windowEvent -> {
            anum--;
        });
        stage1.setScene(sc);
        stage1.setTitle("添加");
        stage1.show();
    }

    //    修改--管理员成绩修改
    public static void magalter(BorderPane bp) {
        anum++;
        GridPane gp = new GridPane();
//        设置间距
        gp.setVgap(10);
        gp.setHgap(10);
        gp.setAlignment(Pos.CENTER);
//        文本
        Label l0 = new Label("学号");
        Label l1 = new Label("语文");
        Label l2 = new Label("数学");
        Label l3 = new Label("英语");
        Label l4 = new Label("物理");
        Label l5 = new Label("化学");
        Label l6 = new Label("生物");
        Button b1 = new Button("提交");
//        文本框
        TextField t0 = new TextField();
        TextField t1 = new TextField();
        TextField t2 = new TextField();
        TextField t3 = new TextField();
        TextField t4 = new TextField();
        TextField t5 = new TextField();
        TextField t6 = new TextField();

        if(!student0.equals("")){
            t0.setText(Integer.toString(student0.getId()));
            t1.setText(Integer.toString(student0.getLanguage()));
            t2.setText(Integer.toString(student0.getMath()));
            t3.setText(Integer.toString(student0.getEnglish()));
            t4.setText(Integer.toString(student0.getPhysics()));
            t5.setText(Integer.toString(student0.getChemistry()));
            t6.setText(Integer.toString(student0.getBiology()));
        }

        b1.setOnAction(actionEvent -> {
            if (number == 0) {
                try {
                    if (t0.getText().trim().equals("") || t1.getText().trim().equals("") || t2.getText().trim().equals("") || t3.getText().trim().equals("") || t4.getText().trim().equals("") || t5.getText().trim().equals("") || t6.getText().trim().equals("")) {
                        tips("修改失败");
                    } else {
                        if (Connect.magupdate(Integer.valueOf(t0.getText()), Integer.valueOf(t1.getText()), Integer.valueOf(t2.getText()), Integer.valueOf(t3.getText()), Integer.valueOf(t4.getText()), Integer.valueOf(t5.getText()), Integer.valueOf(t6.getText()))) {
                            tips("修改成功");
                            ArrayList students = new ArrayList<>();
                            maginit(bp, students);
                        } else {
                            tips("修改失败");
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

//        添加到面板中
        gp.add(l0, 0, 0);
        gp.add(t0, 1, 0);
        gp.add(l1, 0, 1);
        gp.add(t1, 1, 1);
        gp.add(l2, 0, 2);
        gp.add(t2, 1, 2);
        gp.add(l3, 0, 3);
        gp.add(t3, 1, 3);
        gp.add(l4, 0, 4);
        gp.add(t4, 1, 4);
        gp.add(l5, 0, 5);
        gp.add(t5, 1, 5);
        gp.add(l6, 0, 6);
        gp.add(t6, 1, 6);
        gp.add(b1, 1, 7);

//        添加场景舞台
        Scene sc = new Scene(gp, 300, 300);
        Stage stage1 = new Stage();
//        窗口关闭时减一
        stage1.setOnCloseRequest(windowEvent -> {
            anum--;
        });
        stage1.setScene(sc);
        stage1.setTitle("修改");
        stage1.show();
    }


    //    修改界面
    static void alter(BorderPane bp) {
        anum++;
        GridPane gp = new GridPane();
//        设置间距
        gp.setVgap(10);
        gp.setHgap(10);
        gp.setAlignment(Pos.CENTER);
//        文本
        Label l0 = new Label("学号");
        Label l1 = new Label("姓名");
        Label l2 = new Label("性别");
        Label l3 = new Label("班级");
        Label l4 = new Label("地址");
        Label l5 = new Label("年龄");
        Label l6 = new Label("电话");
        Label l7 = new Label("密码");


        Button b1 = new Button("提交");
//        文本框
        TextField t0 = new TextField();
        TextField t1 = new TextField();
        TextField t2 = new TextField();
        TextField t3 = new TextField();
        TextField t4 = new TextField();
        TextField t5 = new TextField();
        TextField t6 = new TextField();
        TextField t7 =new TextField();

        //监听文本输入的内容
        t2.textProperty().addListener(new ChangeListener() {
            @Override
            public void changed(ObservableValue observable, String oldValue, String newValue) {
                if(newValue.length()<2&&(newValue.equals("男")||newValue.equals("女"))){
                    t2.setText(newValue);
                }else{
                    t2.setText("");
                }
            }
        });

        if(!student0.equals("")){
            t0.setText(Integer.toString(student0.getId()));
            t1.setText(student0.getName());
            t2.setText(student0.getGender());
            t3.setText(student0.getClas());
            t4.setText(student0.getAddress());
            t5.setText(student0.getAge());
            t6.setText(student0.getPhone());
            t7.setText(student0.getPassword());

        }

        b1.setOnAction(actionEvent -> {
            if (number == 0) {
                try {
                    if (t0.getText().trim().equals("") || t1.getText().trim().equals("") || t2.getText().trim().equals("") || t3.getText().trim().equals("") || t4.getText().trim().equals("") || t5.getText().trim().equals("") || t6.getText().trim().equals("") || t7.getText().trim().equals("")||t6.getText().length()!=11) {
                        tips("修改失败");
                    } else {
                        if (Connect.update(t1.getText(), t2.getText(), t3.getText(), t4.getText(), t5.getText(), t6.getText(), t7.getText(), Integer.valueOf(t0.getText()))) {
                            tips("修改成功");
                            ArrayList students = new ArrayList<>();
                            init(bp, students);
                        }
                        ;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

//        添加到面板中
        gp.add(l0, 0, 0);
        gp.add(t0, 1, 0);
        gp.add(l1, 0, 1);
        gp.add(t1, 1, 1);
        gp.add(l2, 0, 2);
        gp.add(t2, 1, 2);
        gp.add(l3, 0, 3);
        gp.add(t3, 1, 3);
        gp.add(l4, 0, 4);
        gp.add(t4, 1, 4);
        gp.add(l5, 0, 5);
        gp.add(t5, 1, 5);
        gp.add(l6, 0, 6);
        gp.add(t6, 1, 6);
        gp.add(l7, 0, 7);
        gp.add(t7, 1, 7);
        gp.add(b1, 1, 8);

//        添加场景舞台
        Scene sc = new Scene(gp, 300, 300);
        Stage stage1 = new Stage();
//        窗口关闭时减一
        stage1.setOnCloseRequest(windowEvent -> {
            anum--;
        });
        stage1.setScene(sc);
        stage1.setTitle("修改");
        stage1.show();
    }

    //    删除界面
    static void delete(BorderPane bp) {
        anum++;
        GridPane gp = new GridPane();
//        设置间距
        gp.setVgap(10);
        gp.setHgap(10);
        gp.setAlignment(Pos.CENTER);
//        文本
        Label l0 = new Label("学号");
        Button b1 = new Button("提交");
//        文本框
        TextField t0 = new TextField();

        b1.setOnAction(actionEvent -> {
            if (number == 0) {
                try {
                    if (t0.getText().trim().equals("")) {
                        tips("删除失败");
                    } else {
                        if (Connect.delete(Integer.valueOf(t0.getText()))) {
                            tips("删除成功");
                            ArrayList students = new ArrayList<>();
                            init(bp, students);
                        }
                        ;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

//        添加到面板中
        gp.add(l0, 0, 0);
        gp.add(t0, 1, 0);
        gp.add(b1, 1, 1);

//        添加场景舞台
        Scene sc = new Scene(gp, 300, 300);
        Stage stage1 = new Stage();
//        窗口关闭时减一
        stage1.setOnCloseRequest(windowEvent -> {
            anum--;
        });
        stage1.setScene(sc);
        stage1.setTitle("删除");
        stage1.show();
    }

    //    提示框
    static void tips(String str) {
//        窗口数+1
        number++;
//        添加文本
        Label l1 = new Label(str);
        Button b1 = new Button("返回");
//        创建面板
        VBox vb = new VBox(l1, b1);
        Scene sc = new Scene(vb, 100, 100);
        vb.setSpacing(10);
        vb.setAlignment(Pos.CENTER);
        Stage stage1 = new Stage();
        stage1.setScene(sc);
//        设置窗口大小不可调节
        stage1.setResizable(false);
        stage1.setOnCloseRequest(windowEvent -> {
            number--;
        });
        b1.setOnAction(actionEvent -> {
            number--;
            stage1.close();
        });
        stage1.setTitle("提示");
        stage1.show();
    }


    //    管理员表格初始化
    static void init(BorderPane bp, ArrayList students) {
        //        中部————表格控件
        TableView tv = new TableView();
//        创建列
        TableColumn c1 = new TableColumn("学号");
        TableColumn c2 = new TableColumn("姓名");
        TableColumn c3 = new TableColumn("性别");
        TableColumn c4 = new TableColumn("班级");
        TableColumn c5 = new TableColumn("地址");
        TableColumn c6 = new TableColumn("年龄");
        TableColumn c7 = new TableColumn("电话");
        TableColumn c8 =new TableColumn("密码");
//
        
        c1.setMinWidth(100);
        c2.setMinWidth(100);
        c3.setMinWidth(100);
        c4.setMinWidth(100);
        c5.setMinWidth(100);
        c6.setMinWidth(100);
        c7.setMinWidth(100);
        c8.setMinWidth(100);

//        确定数据导入的列
        c1.setCellValueFactory(new PropertyValueFactory<>("id"));
        c2.setCellValueFactory(new PropertyValueFactory<>("name"));
        c3.setCellValueFactory(new PropertyValueFactory<>("gender"));
        c4.setCellValueFactory(new PropertyValueFactory<>("clas"));
        c5.setCellValueFactory(new PropertyValueFactory<>("address"));
        c6.setCellValueFactory(new PropertyValueFactory<>("age"));
        c7.setCellValueFactory(new PropertyValueFactory<>("phone"));
        c8.setCellValueFactory(new PropertyValueFactory<>("password"));


//        鼠标点击后储存该行数据
        tv.setRowFactory( a -> {
            TableRow row = new TableRow();
            row.setOnMouseClicked(event -> {
                if (event.getClickCount() == 1 && (! row.isEmpty()) ) {
                    student0 =row.getItem();
                }
            });
            return row ;
        });


        tv.getColumns().addAll(c1, c2, c3, c4, c5, c6, c7,c8);
        //        创建学生集合
        try {
            Connect.student(students);
        } catch (Exception e) {
            e.printStackTrace();
        }

        tv.getItems().addAll(students);
        bp.setCenter(tv);
    }

    //    学生成绩管理界面
    static void magscore() {
        BorderPane bp = new BorderPane();
//        顶部
        FlowPane fp1 = new FlowPane();

        Label l1 = new Label("姓名");
        Label l3 = new Label("班级");

        TextField f1 = new TextField();
        TextField f3 = new TextField();

        Button b1 = new Button("查询");
        Button b3 = new Button("刷新");

//        模糊查询
        b1.setOnAction(actionEvent -> {
            if (!f1.getText().trim().equals("") && f3.getText().trim().equals("")) {   //姓名
                ArrayList students = new ArrayList<>();
                maginit(bp, students, f1.getText(), 1);
            } else if (f1.getText().trim().equals("") && !f3.getText().trim().equals("")) {  //班级
                ArrayList students = new ArrayList<>();
                maginit(bp, students, f3.getText(), 2);
            } else if (!f1.getText().trim().equals("") && !f3.getText().trim().equals("")) {  //姓名+班级
                ArrayList students = new ArrayList<>();
                maginit1(bp, students, f1.getText(), f3.getText());
            }
        });
        b3.setOnAction(actionEvent -> {
            ArrayList students = new ArrayList<>();
            maginit(bp, students);
        });
        fp1.getChildren().addAll(l1, f1, l3, f3, b1, b3);
        fp1.setHgap(10);
//        设置上下之间的间隙内间距
        fp1.setPadding(new Insets(10));
        bp.setTop(fp1);

//        底部
        FlowPane fp2 = new FlowPane();
        Button b5 = new Button("修改");
        Button b6 = new Button("退出");

//        修改
        b5.setOnAction(actionEvent -> {
            if (anum == 0) {
                magalter(bp);
            }
        });
        b6.setOnAction((actionEvent -> {
            maghomepage();
        }));
        fp2.getChildren().addAll(b5, b6);
        fp2.setHgap(10);
        fp2.setPadding(new Insets(10));
        bp.setBottom(fp2);

//        中部————表格控件
        ArrayList students = new ArrayList<>();
        maginit(bp, students);
//        添加场景
        Scene sc = new Scene(bp);
        stage.setScene(sc);
    }

    //    学生成绩管理表单
    static void maginit(BorderPane bp, ArrayList students) {
        //        中部————表格控件
        TableView tv = new TableView();
//        创建列
        TableColumn c1 = new TableColumn("学号");
        TableColumn c2 = new TableColumn("姓名");
        TableColumn c3 = new TableColumn("班级");
        TableColumn c4 = new TableColumn("语文");
        TableColumn c5 = new TableColumn("数学");
        TableColumn c6 = new TableColumn("英语");
        TableColumn c7 = new TableColumn("物理");
        TableColumn c8 = new TableColumn("化学");
        TableColumn c9 = new TableColumn("生物");
        TableColumn c10 = new TableColumn("总分");

//        表格列宽宽度设置
        c1.setMinWidth(100);
        c2.setMinWidth(100);
        c3.setMinWidth(100);
        c4.setMinWidth(100);
        c5.setMinWidth(100);
        c6.setMinWidth(100);
        c7.setMinWidth(100);
        c8.setMinWidth(100);
        c9.setMinWidth(100);
        c10.setMinWidth(100);

//        确定数据导入的列
        c1.setCellValueFactory(new PropertyValueFactory<>("id"));
        c2.setCellValueFactory(new PropertyValueFactory<>("name"));
        c3.setCellValueFactory(new PropertyValueFactory<>("clas"));
        c4.setCellValueFactory(new PropertyValueFactory<>("language"));
        c5.setCellValueFactory(new PropertyValueFactory<>("math"));
        c6.setCellValueFactory(new PropertyValueFactory<>("english"));
        c7.setCellValueFactory(new PropertyValueFactory<>("physics"));
        c8.setCellValueFactory(new PropertyValueFactory<>("chemistry"));
        c9.setCellValueFactory(new PropertyValueFactory<>("biology"));
        c10.setCellValueFactory(new PropertyValueFactory<>("score"));

        //        鼠标点击后储存该行数据
        tv.setRowFactory( a -> {
            TableRow row = new TableRow();
            row.setOnMouseClicked(event -> {
                if (event.getClickCount() == 1 && (! row.isEmpty()) ) {
                    student0 =row.getItem();
                }
            });
            return row ;
        });

        tv.getColumns().addAll(c1, c2, c3, c4, c5, c6, c7, c8, c9, c10);
        //        创建学生集合
        try {
            Connect.magstudent(students);
        } catch (Exception e) {
            e.printStackTrace();
        }

        tv.getItems().addAll(students);

//        双击输出行

        bp.setCenter(tv);
    }

    //    名字或班级   模糊查询表格
    static void maginit(BorderPane bp, ArrayList students, String s, int a) {
        //        中部————表格控件
        TableView tv = new TableView();
//        创建列
        TableColumn c1 = new TableColumn("学号");
        TableColumn c2 = new TableColumn("姓名");
        TableColumn c3 = new TableColumn("班级");
        TableColumn c4 = new TableColumn("语文");
        TableColumn c5 = new TableColumn("数学");
        TableColumn c6 = new TableColumn("英语");
        TableColumn c7 = new TableColumn("物理");
        TableColumn c8 = new TableColumn("化学");
        TableColumn c9 = new TableColumn("生物");
        TableColumn c10 = new TableColumn("总分");
//        表格列宽宽度设置
        c1.setMinWidth(100);
        c2.setMinWidth(100);
        c3.setMinWidth(100);
        c4.setMinWidth(100);
        c5.setMinWidth(100);
        c6.setMinWidth(100);
        c7.setMinWidth(100);
        c8.setMinWidth(100);
        c9.setMinWidth(100);
        c10.setMinWidth(100);

//        确定数据导入的列
        c1.setCellValueFactory(new PropertyValueFactory<>("id"));
        c2.setCellValueFactory(new PropertyValueFactory<>("name"));
        c3.setCellValueFactory(new PropertyValueFactory<>("clas"));
        c4.setCellValueFactory(new PropertyValueFactory<>("language"));
        c5.setCellValueFactory(new PropertyValueFactory<>("math"));
        c6.setCellValueFactory(new PropertyValueFactory<>("english"));
        c7.setCellValueFactory(new PropertyValueFactory<>("physics"));
        c8.setCellValueFactory(new PropertyValueFactory<>("chemistry"));
        c9.setCellValueFactory(new PropertyValueFactory<>("biology"));
        c10.setCellValueFactory(new PropertyValueFactory<>("score"));

        tv.getColumns().addAll(c1, c2, c3, c4, c5, c6, c7, c8, c9, c10);
        //        创建学生集合
        try {
            Connect.maginquire(students, s, a);
        } catch (Exception e) {
            e.printStackTrace();
        }

        tv.getItems().addAll(students);
        bp.setCenter(tv);
    }

    //    名字和班级   模糊查询表格
    static void maginit1(BorderPane bp, ArrayList students, String name, String clas) {
        //        中部————表格控件
        TableView tv = new TableView();
//        创建列
        TableColumn c1 = new TableColumn("学号");
        TableColumn c2 = new TableColumn("姓名");
        TableColumn c3 = new TableColumn("班级");
        TableColumn c4 = new TableColumn("语文");
        TableColumn c5 = new TableColumn("数学");
        TableColumn c6 = new TableColumn("英语");
        TableColumn c7 = new TableColumn("物理");
        TableColumn c8 = new TableColumn("化学");
        TableColumn c9 = new TableColumn("生物");
        TableColumn c10 = new TableColumn("总分");
//        表格列宽宽度设置
        c1.setMinWidth(100);
        c2.setMinWidth(100);
        c3.setMinWidth(100);
        c4.setMinWidth(100);
        c5.setMinWidth(100);
        c6.setMinWidth(100);
        c7.setMinWidth(100);
        c8.setMinWidth(100);
        c9.setMinWidth(100);
        c10.setMinWidth(100);

//        确定数据导入的列
        c1.setCellValueFactory(new PropertyValueFactory<>("id"));
        c2.setCellValueFactory(new PropertyValueFactory<>("name"));
        c3.setCellValueFactory(new PropertyValueFactory<>("clas"));
        c4.setCellValueFactory(new PropertyValueFactory<>("language"));
        c5.setCellValueFactory(new PropertyValueFactory<>("math"));
        c6.setCellValueFactory(new PropertyValueFactory<>("english"));
        c7.setCellValueFactory(new PropertyValueFactory<>("physics"));
        c8.setCellValueFactory(new PropertyValueFactory<>("chemistry"));
        c9.setCellValueFactory(new PropertyValueFactory<>("biology"));
        c10.setCellValueFactory(new PropertyValueFactory<>("score"));

        tv.getColumns().addAll(c1, c2, c3, c4, c5, c6, c7, c8, c9, c10);
        //        创建学生集合
        try {
            Connect.maginquire(students, name, clas);
        } catch (Exception e) {
            e.printStackTrace();
        }

        tv.getItems().addAll(students);
        bp.setCenter(tv);
    }


    //    名字模糊查询表格
    static void init(BorderPane bp, ArrayList students, String name) {
        //        中部————表格控件
        TableView tv = new TableView();
//        创建列
        TableColumn c1 = new TableColumn("学号");
        TableColumn c2 = new TableColumn("姓名");
        TableColumn c3 = new TableColumn("性别");
        TableColumn c4 = new TableColumn("班级");
        TableColumn c5 = new TableColumn("地址");
        TableColumn c6 = new TableColumn("年龄");
        TableColumn c7 = new TableColumn("电话");
//        表格列宽宽度设置
        c1.setMinWidth(100);
        c2.setMinWidth(100);
        c3.setMinWidth(100);
        c4.setMinWidth(100);
        c5.setMinWidth(100);
        c6.setMinWidth(100);
        c7.setMinWidth(100);

//        确定数据导入的列
        c1.setCellValueFactory(new PropertyValueFactory<>("id"));
        c2.setCellValueFactory(new PropertyValueFactory<>("name"));
        c3.setCellValueFactory(new PropertyValueFactory<>("gender"));
        c4.setCellValueFactory(new PropertyValueFactory<>("clas"));
        c5.setCellValueFactory(new PropertyValueFactory<>("address"));
        c6.setCellValueFactory(new PropertyValueFactory<>("age"));
        c7.setCellValueFactory(new PropertyValueFactory<>("phone"));

        tv.getColumns().addAll(c1, c2, c3, c4, c5, c6, c7);
        //        创建学生集合
        try {
            Connect.inquire(students, name);
        } catch (Exception e) {
            e.printStackTrace();
        }

        tv.getItems().addAll(students);
        bp.setCenter(tv);
    }

    //    id查询后表格
    static void init(BorderPane bp, ArrayList students, int id) {
        //        中部————表格控件
        TableView tv = new TableView();
//        创建列
        TableColumn c1 = new TableColumn("学号");
        TableColumn c2 = new TableColumn("姓名");
        TableColumn c3 = new TableColumn("性别");
        TableColumn c4 = new TableColumn("班级");
        TableColumn c5 = new TableColumn("地址");
        TableColumn c6 = new TableColumn("年龄");
        TableColumn c7 = new TableColumn("电话");
//        表格列宽宽度设置
        c1.setMinWidth(100);
        c2.setMinWidth(100);
        c3.setMinWidth(100);
        c4.setMinWidth(100);
        c5.setMinWidth(100);
        c6.setMinWidth(100);
        c7.setMinWidth(100);

//        确定数据导入的列
        c1.setCellValueFactory(new PropertyValueFactory<>("id"));
        c2.setCellValueFactory(new PropertyValueFactory<>("name"));
        c3.setCellValueFactory(new PropertyValueFactory<>("gender"));
        c4.setCellValueFactory(new PropertyValueFactory<>("clas"));
        c5.setCellValueFactory(new PropertyValueFactory<>("address"));
        c6.setCellValueFactory(new PropertyValueFactory<>("age"));
        c7.setCellValueFactory(new PropertyValueFactory<>("phone"));

        tv.getColumns().addAll(c1, c2, c3, c4, c5, c6, c7);
        //        创建学生集合
        try {
            Connect.inquire(students, id);
        } catch (Exception e) {
            e.printStackTrace();
        }

        tv.getItems().addAll(students);
        bp.setCenter(tv);
    }


}

2.Student 


public class Student {
    private int id;
    private String name;
    private String gender;
    private String clas;
    private String address;
    private String age;
    private String phone;
    private String password;
    private int language;
    private int math;
    private int english;
    private int physics;
    private int chemistry;
    private int biology;
    private int score;

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    public int getLanguage() {
        return language;
    }

    public void setLanguage(int language) {
        this.language = language;
    }

    public int getMath() {
        return math;
    }

    public void setMath(int math) {
        this.math = math;
    }

    public int getEnglish() {
        return english;
    }

    public void setEnglish(int english) {
        this.english = english;
    }

    public int getPhysics() {
        return physics;
    }

    public void setPhysics(int physics) {
        this.physics = physics;
    }

    public int getChemistry() {
        return chemistry;
    }

    public void setChemistry(int chemistry) {
        this.chemistry = chemistry;
    }

    public int getBiology() {
        return biology;
    }

    public void setBiology(int biology) {
        this.biology = biology;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getClas() {
        return clas;
    }

    public void setClas(String clas) {
        this.clas = clas;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

}

3.Connect


import java.sql.*;
import java.util.ArrayList;
import java.util.Objects;

public class Connect {
    /**
     * URL地址
     */
    private static final String URL = "jdbc:mysql://127.0.0.1:3306/ycx?useSSL=false";
    /**
     * 登录数据库服务器的账号
     */
    private static final String USER = "root";
    /**
     * 登录数据库服务器的密码
     */
    private static final String PASSWORD = "15194538986a";

    /**
     * 返回数据库连接对象
     *
     * @return
     */
    public static Connection getConn() {
        try {
            return DriverManager.getConnection(URL, USER, PASSWORD);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 关闭资源
     *
     * @param rs
     * @param stat
     * @param conn
     */
    public static void close(ResultSet rs, Statement stat, Connection conn) {
        try {
            if (rs != null) {
                rs.close();
            }
            if (stat != null) {
                stat.close();
            }
            if (conn != null) {
                conn.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 封装通用的更新操作(即通过该方法实现对弈任意数据表的insert,update,delete操作)
     *
     * @param sql    需要执行的SQL语句
     * @param params 执行SQL语句时需要传递进去参数
     * @return 执行结果
     */
    public static boolean exeUpdate(String sql, Object... params) {
        //获取连接对象
        Connection conn = getConn();
        PreparedStatement ps = null;
        try {
            //获取预编译对象
            ps = conn.prepareStatement(sql);
            //执行参数赋值操作
            if (Objects.nonNull(params)) {
                //循环将所有的参数赋值
                for (int i = 0; i < params.length; i++) {
                    ps.setObject(i + 1, params[i]);
                }
            }
            //执行更新
            return ps.executeUpdate() > 0;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //关闭资源
            close(null, ps, conn);
        }
        return false;
    }

    //    学生登录
    public static int login(int id, String pwd) throws Exception {

        String sql = "select * from student where id=? and password=?";
        PreparedStatement pstmt = getConn().prepareStatement(sql);
        pstmt.setInt(1, id);
        pstmt.setString(2, pwd);
        ResultSet rs = pstmt.executeQuery();
        if (rs.next()) {

            return rs.getInt("id");
        } else {

            return 0;
        }
    }

    //    管理员登录
    public static boolean maglogin(String name, String pwd) throws Exception {

        String sql = "select * from admin_login_k where admin_id=? and admin_pass=?";
        PreparedStatement pstmt = getConn().prepareStatement(sql);
        pstmt.setString(1, name);
        pstmt.setString(2, pwd);
        ResultSet rs = pstmt.executeQuery();
        if (rs.next()) {
            return true;
        } else {
            return false;
        }
    }

    //    增加
    public static boolean add(String name, String gender, String clas, String address,
                              String age, String phone, String password) throws Exception {
        String sql = "insert into student(name,gender,clas,address,age,phone,password) values (?,?,?,?,?,?,?)";
        return exeUpdate(sql, name, gender, clas, address, age, phone, password);

    }

    //    修改
    public static boolean update(String name, String gender, String clas, String address,
                                 String age, String phone, String password, int id) throws Exception {
        String sql = "update student set name=?,gender=?,clas=?,address=?,age=?,phone=?,password=? where id=?";
        return exeUpdate(sql, name, gender, clas, address, age, phone, password, id);

    }

    //    删除
    public static boolean delete(int id) {
        String sql = "delete from student where id=?";
        return exeUpdate(sql, id);
    }

    //    学生成绩修改
    public static boolean magupdate(int id, int language, int math, int english, int physics, int chemistry,
                                    int biology) throws Exception {
        String sql1 = "update score set language=?,math=?,english=? where student_id=?";
        String sql2 = "update elective set physics=?,chemistry=?,biology=? where student_id=?";
        return exeUpdate(sql1, language, math, english, id) && exeUpdate(sql2, physics, chemistry, biology, id);

    }


    //        模糊查询
    public static void inquire(ArrayList list, String name) throws Exception {

        String sql = "select * from student where name like ?";
        PreparedStatement pstmt = getConn().prepareStatement(sql);
        pstmt.setString(1, "%" + name + "%");
        ResultSet rs = pstmt.executeQuery();
        while(rs.next()) {
            Student student = new Student();
            student.setId(rs.getInt("id"));
            student.setName(rs.getString("name"));
            student.setGender(rs.getString("gender"));
            student.setClas(rs.getString("clas"));
            student.setAddress(rs.getString("address"));
            student.setAge(rs.getString("age"));
            student.setPhone(rs.getString("phone"));
            student.setPassword(rs.getString("password"));
            list.add(student);
        }
        //        释放资源
        rs.close();
        pstmt.close();
    }

    //        模糊查询--学生成绩
    public static void maginquire(ArrayList list, String s, int a) throws Exception {

        String sql = "";
        if (a == 1) {   //名字模糊查询
            sql = "select id,name,clas,language,math,english,physics,chemistry,biology,language+math+english+physics+chemistry+biology 'score' from student,score,elective where id=score.student_id and id=elective.student_id and name like?;";
        } else if (a == 2) {  //班级模糊查询
            sql = "select id,name,clas,language,math,english,physics,chemistry,biology,language+math+english+physics+chemistry+biology 'score' from student,score,elective where id=score.student_id and id=elective.student_id and clas like?;";
        }
        PreparedStatement pstmt = getConn().prepareStatement(sql);
        pstmt.setString(1, "%" + s + "%");
        ResultSet rs = pstmt.executeQuery();
        while(rs.next()){
            Student student = new Student();
            student.setId(rs.getInt("id"));
            student.setName(rs.getString("name"));
            student.setClas(rs.getString("clas"));
            student.setLanguage(rs.getInt("language"));
            student.setMath(rs.getInt("math"));
            student.setEnglish(rs.getInt("english"));
            student.setPhysics(rs.getInt("physics"));
            student.setChemistry(rs.getInt("chemistry"));
            student.setBiology(rs.getInt("biology"));
            student.setScore(rs.getInt("score"));
            list.add(student);
        }
        //        释放资源
        rs.close();
        pstmt.close();
    }

    //        模糊查询--学生成绩
    public static void maginquire(ArrayList list, String name, String clas) throws Exception {


        //名字+班级
        String sql = "select id,name,clas,language,math,english,physics,chemistry,biology,language+math+english+physics+chemistry+biology 'score' from student,score,elective where id=score.student_id and id=elective.student_id and  name like? and  clas like?;";

        PreparedStatement pstmt = getConn().prepareStatement(sql);
        pstmt.setString(1, "%" + name + "%");
        pstmt.setString(2, "%" + clas + "%");
        ResultSet rs = pstmt.executeQuery();
        while (rs.next()) {
            Student student = new Student();
            student.setId(rs.getInt("id"));
            student.setName(rs.getString("name"));
            student.setClas(rs.getString("clas"));
            student.setLanguage(rs.getInt("language"));
            student.setMath(rs.getInt("math"));
            student.setEnglish(rs.getInt("english"));
            student.setPhysics(rs.getInt("physics"));
            student.setChemistry(rs.getInt("chemistry"));
            student.setBiology(rs.getInt("biology"));
            student.setScore(rs.getInt("score"));
            list.add(student);
        }
        //        释放资源
        rs.close();
        pstmt.close();
    }

    //        学号查询
    public static void inquire(ArrayList list, int id) throws Exception {

        String sql = "select * from student where id=?";
        PreparedStatement pstmt = getConn().prepareStatement(sql);
        pstmt.setInt(1, id);
        ResultSet rs = pstmt.executeQuery();
        if (rs.next()) {
            Student student = new Student();
            student.setId(rs.getInt("id"));
            student.setName(rs.getString("name"));
            student.setGender(rs.getString("gender"));
            student.setClas(rs.getString("clas"));
            student.setAddress(rs.getString("address"));
            student.setAge(rs.getString("age"));
            student.setPhone(rs.getString("phone"));
            list.add(student);
        }
        //        释放资源
        rs.close();
        pstmt.close();
    }

    //    成绩查询--学生
    public static void score(Student student, int id) throws Exception {

        String sql = "select id,name,clas,language,math,english,physics,chemistry,biology,language+math+english+physics+chemistry+biology 'score' from student,score,elective where id=score.student_id and id=elective.student_id and id=?;";
        PreparedStatement pstmt = getConn().prepareStatement(sql);
        pstmt.setInt(1, id);
        ResultSet rs = pstmt.executeQuery();
        if (rs.next()) {
            student.setLanguage(rs.getInt("language"));
            student.setMath(rs.getInt("math"));
            student.setEnglish(rs.getInt("english"));
            student.setLanguage(rs.getInt("language"));
            student.setMath(rs.getInt("math"));
            student.setEnglish(rs.getInt("english"));
            student.setPhysics(rs.getInt("physics"));
            student.setChemistry(rs.getInt("chemistry"));
            student.setBiology(rs.getInt("biology"));
            student.setScore(rs.getInt("score"));
        }
        //        释放资源
        rs.close();
        pstmt.close();
    }


    //   学生类初始化
    public static void student(ArrayList list) throws Exception {
        String sql = "select * from student ";
        PreparedStatement pstmt = getConn().prepareStatement(sql);
        ResultSet rs = pstmt.executeQuery();
        while (rs.next()) {

//            获取数据
            int id = rs.getInt("id");
            String name = rs.getString("name");
            String gender = rs.getString("gender");
            String clas = rs.getString("clas");
            String address = rs.getString("address");
            String age = rs.getString("age");
            String phone = rs.getString("phone");
            String password=rs.getString("password");

//            封装数据
            Student student = new Student();
            student.setId(id);
            student.setName(name);
            student.setGender(gender);
            student.setClas(clas);
            student.setAddress(address);
            student.setAge(age);
            student.setPhone(phone);
            student.setPassword(password);

            list.add(student);
        }
//        释放资源
        rs.close();
        pstmt.close();
    }

    //   学生类成绩初始化--管理员
    public static void magstudent(ArrayList list) throws Exception {
        String sql = "select id,name,clas,language,math,english,physics,chemistry,biology,language+math+english+physics+chemistry+biology 'score' from student,score,elective where id=score.student_id and id=elective.student_id; ";
        PreparedStatement pstmt = getConn().prepareStatement(sql);
        ResultSet rs = pstmt.executeQuery();
        while (rs.next()) {

//            获取数据
            int id = rs.getInt("id");
            String name = rs.getString("name");
            String clas = rs.getString("clas");
            int language = rs.getInt("language");
            int math = rs.getInt("math");
            int english = rs.getInt("english");
            int physics = rs.getInt("physics");
            int chemistry = rs.getInt("chemistry");
            int biology = rs.getInt("biology");
            int score = rs.getInt("score");

//            封装数据
            Student student = new Student();
            student.setId(id);
            student.setName(name);
            student.setClas(clas);
            student.setLanguage(language);
            student.setMath(math);
            student.setEnglish(english);
            student.setPhysics(physics);
            student.setChemistry(chemistry);
            student.setBiology(biology);
            student.setScore(score);

            list.add(student);
        }
//        释放资源
        rs.close();
        pstmt.close();
    }


}

4.Main 

public class Main {
    public static void main(String[] args) {

        View.launch(args);
    }
}

你可能感兴趣的:(java,mysql,数据库)