目录
(1).查询、删除功能(不需要跳转新页面,删除功能会有一个小提示)
学了一个多月,利用javafx中的ScenceBuilder和MySQL写了一个简易的学生管理系统,写完后一些功能相似,在此就介绍用到的
所有结构
一、链接层_dao(存放携带sql语句方法的类)
需要用到查询语句直接调用此类中的方法传递参数即可
(1)封装好的JDBC类
实现数据库的链接使用JDBC,这里直接将增删改,还有查询分装到此类中的不同方法,这样只需要调
用对应的方法传递sql语句和一些参数就可以实现相对应的功能了,如下:
package dao;
import java.sql.*;
import java.util.Objects;
public class JDBC {
/**
* URL地址
*/
private static final String URL = "jdbc:mysql://127.0.0.1:3306/small_systems?serverTimezone=UTC";
/**
* 登录数据库服务器的账号
*/
private static final String USER = "root";
/**
* 登录数据库服务器的密码
*/
private static final String PASSWORD = "123456";
/**
* 返回数据库连接对象
*
* @return
*/
public static Connection getConn() {
try {
return DriverManager.getConnection(URL, USER, PASSWORD);
} catch (SQLException e) {
System.out.println("连接失败");
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 ResultSet executeQuery(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.executeQuery();
} catch (SQLException e) {
e.printStackTrace();
} finally {
//关闭资源
// close(null, ps, conn);
}
return null;
}
}
(2)封装好的增改删查类
增删改功能提供给教师,这里示范dao包中的Teacher_operate类(这里面直接调用JDBC封装好的类,不需要一个一个单独建立连接了)
package dao;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import pojo.Student;
import pojo.Teacher;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Teachers_operate {
/**
* ---------------------------------------------------------------------------
*/
//登录查询
public static boolean login(String username, int password) throws SQLException {
String sql = "select id,password from small_systems.teacher where id= ? and password=?";
// ps.setInt(1,username);
// ps.setInt(2,password);
//查询语句
ResultSet rs = JDBC.executeQuery(sql, username, password);
while (rs.next()) {
return true;
}
return false;
}
/**
* -------------------------------------------------------------------------------------------
*/
//增加学生
public static boolean add_students(Student student) throws SQLException {
String sql = "insert into small_systems.student (id, name, gender, age, profession, phone) values (?, ?, ?, ?, ?, ?)";
boolean rowsAffected = JDBC.exeUpdate(sql, student.getId(), student.getName(), student.getGender(), student.getAge(), student.getProfession(), student.getPhone());
return rowsAffected;
}
/**
* -------------------------------------------------------------------------------------------
*/
//修改学生信息
public static void edit(Student student, int oldId) throws Exception {
// String sql = "UPDATE small_systems.student SET id = ? , name = ? , gender = ? , age = ? , profession = ? , phone = ? where id=?";
// JDBC.exeUpdate(sql,student,oldId);
/**这里一点点细节,直接传Student对象好像不太行,我是一个一个传,然后就可以了,不然总报那个io异常*/
// 修改学生
String sql = "UPDATE small_systems.student SET id = ?, name = ?, gender = ?, age = ?, profession = ?, phone = ? WHERE id = ?";
JDBC.exeUpdate(sql, student.getId(), student.getName(), student.getGender(), student.getAge(),
student.getProfession(), student.getPhone(), oldId);
}
/**
* -------------------------------------------------------------------------------------------
*/
//删除学生
public static void delete_students(int stu) throws Exception {
String sql = "delete from small_systems.student WHERE id = ?";
JDBC.exeUpdate(sql, stu);
}
/**
* -------------------------------------------------------------------------------------------
*/
}
二、实体类_pojo
用来写学生的各种构造方法,方便传递参数存储学生集合(因为对学生表进行增删改查,所以举例学生实体类)
用什么构造方法就构造什么,这里的数据类型指定必须是下面这种
package pojo;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
//新建一个类,确定数据后,构造数据模型
public class Student {
private final SimpleIntegerProperty id;
private final SimpleStringProperty name;
private final SimpleStringProperty gender;
private final SimpleIntegerProperty age;
private final SimpleStringProperty profession;
private final SimpleIntegerProperty phone;
private final SimpleStringProperty classes;
private final SimpleStringProperty course;
//学生基本信息构造函数
public Student(int id, String name, String gender, int age, String profession, int phone) {
this.id = new SimpleIntegerProperty(id);
this.name = new SimpleStringProperty(name);
this.gender = new SimpleStringProperty(gender);
this.age = new SimpleIntegerProperty(age);
this.profession = new SimpleStringProperty(profession);
this.phone = new SimpleIntegerProperty(phone);
this.classes = null;
this.course = null;
}
//使用学生id查询课程构造函数
public Student(int id,String name, String profession, String classes, String course) {
this.id = new SimpleIntegerProperty(id);
this.name = new SimpleStringProperty(name);
this.gender = null;
this.age = null;
this.profession = new SimpleStringProperty(profession);
this.phone = null;
this.classes = new SimpleStringProperty(classes);
this.course = new SimpleStringProperty(course);
}
//使用课程查询学生数量
public Student(String course,int id, String name, String classes ) {
this.course = new SimpleStringProperty(course);
this.id = new SimpleIntegerProperty(id);
this.name = new SimpleStringProperty(name);
this.classes = new SimpleStringProperty(classes);
this.gender = null;
this.age = null;
this.profession = null;
this.phone = null;
}
public int getId() {
return id.get();
}
public SimpleIntegerProperty idProperty() {
return id;
}
public void setId(int id) {
this.id.set(id);
}
public String getName() {
return name.get();
}
public SimpleStringProperty nameProperty() {
return name;
}
public void setName(String name) {
this.name.set(name);
}
public String getGender() {
return gender.get();
}
public SimpleStringProperty genderProperty() {
return gender;
}
public void setGender(String gender) {
this.gender.set(gender);
}
public int getAge() {
return age.get();
}
public SimpleIntegerProperty ageProperty() {
return age;
}
public void setAge(int age) {
this.age.set(age);
}
public String getProfession() {
return profession.get();
}
public SimpleStringProperty professionProperty() {
return profession;
}
public void setProfession(String profession) {
this.profession.set(profession);
}
public int getPhone() {
return phone.get();
}
public SimpleIntegerProperty phoneProperty() {
return phone;
}
public void setPhone(int phone) {
this.phone.set(phone);
}
public String getClasses() {
return classes.get();
}
public SimpleStringProperty classesProperty() {
return classes;
}
public void setClasses(String classes) {
this.classes.set(classes);
}
public String getCourse() {
return course.get();
}
public SimpleStringProperty courseProperty() {
return course;
}
public void setCourse(String course) {
this.course.set(course);
}
}
三、错误弹框页面
(1).删除提示页面
package Tips;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.MouseEvent;
import javafx.stage.Modality;
import javafx.stage.Stage;
import java.sql.SQLException;
//确定要删除吗
public class Comfirmed {
@FXML
private Button Yes;
@FXML
private Button No;
//定义静态变量
public static boolean a=true;
public void set_Yes(MouseEvent mouseEvent) throws Exception {
a=true;
Stage previousStage = (Stage) Yes.getScene().getWindow();
previousStage.close();
}
public void set_No(MouseEvent mouseEvent) {
a=false;
Stage previousStage = (Stage) No.getScene().getWindow();
previousStage.close();
}
}
(2).错误异常页面
package Tips;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;
//import java.awt.*;
//中转站,需要页面就调用方法
public class Warn {
@FXML
private Button rtn;
@FXML
public void set_return(MouseEvent mouseEvent) throws Exception{
// Scene scene = rtn.getScene();
Stage previousStage = (Stage) rtn.getScene().getWindow();
previousStage.close();
}
}
resources包中存放fxml文件,都有与之对应的java文件,如下:
四、主界面的设置
放在了学生类中的Main
五、登录功能的实现
可以实现三种登录方式 ,可以添加三个循环,具体代码如下图所示:
package Students;
import dao.Administrators_operate;
import dao.Students_operate;
import dao.Teachers_operate;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.input.MouseEvent;
import javafx.stage.Modality;
import javafx.stage.Stage;
//import javax.swing.*;
import java.io.IOException;
public class Main extends Application {
@Override
public void start(Stage stage) throws IOException {
//加载登录界面
FXMLLoader fxmlLoader = new FXMLLoader(Main.class.getResource("/Students/Main.fxml"));
// Scene scene = new Scene(fxmlLoader.load());
Scene scene =new Scene(fxmlLoader.load());
stage.setResizable(false);//窗口不可改变高度 宽度 这样就不用调节自适应了
stage.setWidth(614);
stage.setHeight(437);
stage.setTitle("H");
stage.setScene(scene);
stage.show();
System.out.println(stage.getWidth() + " " + stage.getHeight());
}
public static void main(String[] args){
launch();
}
//引用对应fxml中的每一个控件(已经提前用Scence Builder给每一个控件起好了 fx:id)
@FXML
private RadioButton stu;
@FXML
private RadioButton teach;
@FXML
private RadioButton Admin;
@FXML
private TextField username;
@FXML
private TextField password;
//登录按钮
@FXML
private Button loginBut;
//忘记密码
@FXML
private Button reset;
//登录进入用户界面
public void user(MouseEvent mouseEvent) throws Exception{
try {
if (username.getText() != "" && password.getText() != "") {
/**---------------------------------------------------------------*/
//对于第一个 学生登录
String use_1 = username.getText();
int pwd_1 = Integer.parseInt(password.getText());
// 调用学生数据库
boolean ok_1 = Students_operate.login(use_1, pwd_1);
/**---------------------------------------------------------------*/
//对于第二个 教师登录
// int use_2= Integer.parseInt(username.getText());
String use_2 = username.getText();
int pwd_2 = Integer.parseInt(password.getText());
//调用教师数据库
boolean ok_2 = Teachers_operate.login(use_2, pwd_2);
/**---------------------------------------------------------------*/
//对于第三个 管理员登录
String use_3 = username.getText();
int pwd_3 = Integer.parseInt(password.getText());
//调用管理员数据库
boolean ok_3 = Administrators_operate.login(use_3, pwd_3);
/**---------------------------------------------------------------*/
if (ok_1 && stu.isSelected()) {
Stage previousStage = (Stage) loginBut.getScene().getWindow();
// 关闭前一个页面的舞台
previousStage.close();
//创建一个新的根节点
Parent root = FXMLLoader.load(getClass().getResource("/Students/User.fxml"));
Scene scene = new Scene(root, 700, 430); // 创建具有新宽度和高度的场景对象
Stage stage = new Stage();
stage.setResizable(false);
stage.setTitle("User");
stage.setScene(scene);
stage.show();
} else if (ok_2 && teach.isSelected()) {
Stage previousStage = (Stage) loginBut.getScene().getWindow();
// 关闭前一个页面的舞台
previousStage.close();
//创建一个新的根节点
Parent root = FXMLLoader.load(getClass().getResource("/Teachers/user.fxml"));
Scene scene = new Scene(root, 700, 430); // 创建具有新宽度和高度的场景对象
Stage stage = new Stage();
stage.setResizable(false);
stage.setTitle("User");
stage.setScene(scene);
stage.show();
} else if (ok_3 && Admin.isSelected()) {
Stage previousStage = (Stage) loginBut.getScene().getWindow();
// 关闭前一个页面的舞台
previousStage.close();
//创建一个新的根节点
Parent root = FXMLLoader.load(getClass().getResource("/Administrators/aaa.fxml"));
Scene scene = new Scene(root, 700, 430); // 创建具有新宽度和高度的场景对象
Stage stage = new Stage();
stage.setResizable(false);
stage.setTitle("User");
stage.setScene(scene);
stage.show();
} else {
Stage previousStage = (Stage) loginBut.getScene().getWindow();
// loginBut.setDisable(true); // 禁用按钮
// 关闭前一个页面的舞台
// previousStage.close();
//重新获取刷新页面
previousStage.getScene().setRoot(FXMLLoader.load(getClass().getResource("/Students/Main.fxml")));
System.out.println("55666");
//加载新页面
Parent root = FXMLLoader.load(getClass().getResource("/Students/Failed.fxml"));
Scene scene = new Scene(root, 350, 150); // 创建具有新宽度和高度的场景对象
Stage stage = new Stage();
stage.setResizable(false);
stage.setScene(scene);
// 将新窗口置于最高层级
stage.setAlwaysOnTop(true);
// 设置新窗口为应用程序模态对话框
stage.initModality(Modality.APPLICATION_MODAL);
// 设置新窗口的所有者为当前舞台
stage.initOwner(previousStage); // currentStage 是当前窗口的 Stage 对象
stage.showAndWait();
}
} else {
System.out.println("账号或密码为空");
//java里面的一个类
// JOptionPane.showMessageDialog(null,"用户名或密码不能为空","错误",JOptionPane.ERROR_MESSAGE);
//如果什么都没填,就报错
Parent root = null;
root = FXMLLoader.load(getClass().getResource("/Tips/account_number.fxml"));
Scene scene = new Scene(root, 296, 295);
Stage stage = new Stage();
stage.setResizable(false);
stage.setTitle("嘿嘿");
stage.setScene(scene);
// 获取当前窗口的 Stage 对象
Stage currentStage = (Stage) loginBut.getScene().getWindow();
// 将新窗口置于最高层级
stage.setAlwaysOnTop(true);
// 设置新窗口为应用程序模态对话框
stage.initModality(Modality.APPLICATION_MODAL);
// 设置新窗口的所有者为当前舞台
stage.initOwner(currentStage); // currentStage 是当前窗口的 Stage 对象
stage.showAndWait();
//点击按钮刷新页面
Stage previousStage = (Stage) loginBut.getScene().getWindow();
previousStage.getScene().setRoot(FXMLLoader.load(getClass().getResource("/Students/Main.fxml")));
}
}catch (Exception e){
Parent root = FXMLLoader.load(getClass().getResource("/Students/Failed.fxml"));
Scene scene = new Scene(root, 350, 150); // 创建具有新宽度和高度的场景对象
Stage stage = new Stage();
stage.setResizable(false);
stage.setScene(scene);
// stage.show();
// 获取当前窗口的 Stage 对象
Stage currentStage = (Stage) loginBut.getScene().getWindow();
// 将新窗口置于最高层级
stage.setAlwaysOnTop(true);
// 设置新窗口为应用程序模态对话框
stage.initModality(Modality.APPLICATION_MODAL);
// 设置新窗口的所有者为当前舞台
stage.initOwner(currentStage); // currentStage 是当前窗口的 Stage 对象
stage.showAndWait();
//点击按钮刷新页面
Stage previousStage = (Stage) loginBut.getScene().getWindow();
previousStage.getScene().setRoot(FXMLLoader.load(getClass().getResource("/Students/Main.fxml")));
}
}
//获取帮助
@FXML
private Hyperlink help;
@FXML
public void help(MouseEvent mouseEvent) {
System.out.println("帮助");
}
//忘记密码,修改密码
// @FXML
// private Button reset;
@FXML
public void setReset(ActionEvent actionEvent) throws Exception{
Parent root =FXMLLoader.load(getClass().getResource("/Students/Forget.fxml"));
reset.getScene().setRoot(root);
}
// 登录失败
@FXML
private Button okok;
@FXML
public void fail(MouseEvent mouseEvent) {
Stage previousStage = (Stage) okok.getScene().getWindow();
// 关闭此页面
previousStage.close();
}
}
六、增改删查功能的实现
因为这里涉及了Table View的初始化在另一篇 JavaFX使用scene builder对TableView的初始化
初始化完毕后补充增加、修改、删除等等一系列控件
(1).查询、删除功能(不需要跳转新页面,删除功能会有一个小提示)
点击删除弹窗
查询功能,删除功能都是在此页面进行的,而增加和删除功能需要弹出新的对话框
下面是所有在此页面进行的操作(其中包含了查询、删除功能)
//查询按钮
@FXML
private Button search;
//查询数据
public void set_search(MouseEvent mouseEvent) throws Exception {
//获取查询文本框的内容
String cot = content.getText();
System.out.println(cot);
// 将文本框的内容传到查询语句中,并获取查询到的这一列
//学生信息查询,和老师信息查询用一样的方法,所以不需要再单独在老师类中写JDBC的sql语句
ObservableList<Student> searchResult = Teachers_operate.search(cot);
// 清空表格中所有数据
tableView.getItems().clear();
// 将这一列新数据插入表格
tableView.setItems(searchResult);
// 显示表格
tableView.setVisible(true);
}
//增加 按钮
@FXML
private Button add;
//增加行
public void set_add (MouseEvent mouseEvent) throws Exception {
//加载新页面
Parent root=FXMLLoader.load(getClass().getResource("/Teachers/A_increase.fxml"));
Scene scene =new Scene(root,600,400);
Stage stage=new Stage();
stage.setResizable(false);
stage.setTitle("增加");
stage.setScene(scene);
// 获取当前窗口的 Stage 对象
Stage currentStage = (Stage) add.getScene().getWindow();
// 将新窗口置于最高层级
stage.setAlwaysOnTop(true);
// 设置新窗口为应用程序模态对话框
stage.initModality(Modality.APPLICATION_MODAL);
// 设置新窗口的所有者为当前舞台
stage.initOwner(currentStage); // currentStage 是当前窗口的 Stage 对象
stage.showAndWait();
}
//删除 按钮
@FXML
private Button delete;
//删除行
@FXML
private void handleDeleteButtonAction(ActionEvent event) throws Exception{
// 获取选中的行
Student selectedData = tableView.getSelectionModel().getSelectedItem();
if (selectedData != null) {
//先跳转页面,当确认后修改个人信息,如果点的取消,关闭提示页面,重绘页面
Parent root = null;
root = FXMLLoader.load(getClass().getResource("/Tips/comfirmed.fxml"));
Scene scene =new Scene(root,286,161);
Stage stage=new Stage();
stage.setResizable(false);
stage.setTitle("删除");
stage.setScene(scene);
// 获取当前窗口的 Stage 对象
Stage currentStage = (Stage) change.getScene().getWindow();
// 将新窗口置于最高层级
stage.setAlwaysOnTop(true);
// 设置新窗口为应用程序模态对话框
stage.initModality(Modality.APPLICATION_MODAL);
// 设置新窗口的所有者为当前舞台
stage.initOwner(currentStage); // currentStage 是当前窗口的 Stage 对象
stage.showAndWait();
if(Comfirmed.a){
// 从ObservableList中移除选中的行
tableView.getItems().remove(selectedData);
// 获取选中行的主键值
int studentId = selectedData.getId();
System.out.println(studentId);
//删除此行
Teachers_operate.delete_students(studentId);
} else {
//重绘页面
Stage previousStage =(Stage)delete.getScene().getWindow();
previousStage.getScene().setRoot(FXMLLoader.load(getClass().getResource("/Teachers/aaa.fxml")));
}
} else {
//如果什么都没填,就报错
Parent root = null;
root = FXMLLoader.load(getClass().getResource("/Tips/warn.fxml"));
Scene scene =new Scene(root,280,270);
Stage stage=new Stage();
stage.setResizable(false);
stage.setTitle("搞笑");
stage.setScene(scene);
// 获取当前窗口的 Stage 对象
Stage currentStage = (Stage) change.getScene().getWindow();
// 将新窗口置于最高层级
stage.setAlwaysOnTop(true);
// 设置新窗口为应用程序模态对话框
stage.initModality(Modality.APPLICATION_MODAL);
// 设置新窗口的所有者为当前舞台
stage.initOwner(currentStage); // currentStage 是当前窗口的 Stage 对象
stage.showAndWait();
}
}
//修改 按钮
@FXML
private Button change;
//修改行
public void set_change(MouseEvent mouseEvent) throws Exception{
// 检查是否有行被选中
if (tableView.getSelectionModel().getSelectedItem() != null) {
//加载新页面
Parent root=FXMLLoader.load(getClass().getResource("/Teachers/A_modify.fxml"));
Scene scene =new Scene(root,600,400);
Stage stage=new Stage();
stage.setResizable(false);
stage.setTitle("修改学生信息");
stage.setScene(scene);
// 获取当前窗口的 Stage 对象
Stage currentStage = (Stage) change.getScene().getWindow();
// 将新窗口置于最高层级
stage.setAlwaysOnTop(true);
// 设置新窗口为应用程序模态对话框
stage.initModality(Modality.APPLICATION_MODAL);
// 设置新窗口的所有者为当前舞台
stage.initOwner(currentStage); // currentStage 是当前窗口的 Stage 对象
stage.showAndWait();
} else {
Parent root = null;
root = FXMLLoader.load(getClass().getResource("/Tips/warn.fxml"));
Scene scene =new Scene(root,280,270);
Stage stage=new Stage();
stage.setResizable(false);
stage.setTitle("搞笑");
stage.setScene(scene);
// 获取当前窗口的 Stage 对象
Stage currentStage = (Stage) change.getScene().getWindow();
// 将新窗口置于最高层级
stage.setAlwaysOnTop(true);
// 设置新窗口为应用程序模态对话框
stage.initModality(Modality.APPLICATION_MODAL);
// 设置新窗口的所有者为当前舞台
stage.initOwner(currentStage); // currentStage 是当前窗口的 Stage 对象
stage.showAndWait();
}
}
//刷新按钮
@FXML
private Button refresh;
public void set_refresh(MouseEvent mouseEvent) throws Exception{
//点击按钮刷新页面
Stage previousStage =(Stage) add.getScene().getWindow();
previousStage.getScene().setRoot(FXMLLoader.load(getClass().getResource("/Teachers/aaa.fxml")));
}
(2).增加功能(需要跳转新页面)
这个是另一个java文件绑定了新的fxml在此页面实现增加功能,添加数据到数据库
(有一些弊端,当关闭此页面需要手动刷新一下页面,重绘上一个页面)
package Teachers;
import dao.Students_operate;
import dao.Teachers_operate;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseEvent;
import javafx.stage.Modality;
import javafx.stage.Stage;
import pojo.Student;
import java.io.IOException;
//增添学生数据
public class A_A_Increase {
@FXML
private TextField id;
@FXML
private TextField name;
@FXML
private TextField gender;
@FXML
private TextField age;
@FXML
private TextField profession;
@FXML
private TextField phone;
/**这个增加功能写完,感觉到有些不合理,因为学生可以自定义学号,也可以默认系统创建学号,但是如果这样,插入,数据一旦多起来就不好查询了*/
boolean sss=true;
//提交按钮
@FXML
private Button commit;
//提交
public void get_commit(ActionEvent actionEvent) throws Exception {
//默认输入数据是不存在的,如果存在 exit的返回值为true,就不插入数据
boolean exit= false;
try{
//查询数据库中是否已经存在数据,如果存在就不插入数据
//判断输入不能为空
exit = Students_operate.get_forget(Integer.parseInt(id.getText()));
} catch (Exception e) {
}
if(exit){
System.out.println("该用户已存在,请重新录入");
} else {
//执行增加操作
upDate();
if(sss){
//当点击此按钮时,关闭此页面
Stage previousStage = (Stage) commit.getScene().getWindow();
previousStage.close();
}else {
Parent root = FXMLLoader.load(getClass().getResource("/Teachers/A_increase.fxml"));
commit.getScene().setRoot(root);
}
}
}
//返回按钮
@FXML
private Button rtn;
public void set_return(MouseEvent mouseEvent) throws Exception{
Stage previousStage = (Stage) rtn.getScene().getWindow();
// 关闭此页面
previousStage.close();
}
//插入数据
public void upDate() {
ObservableList<Student> studentList = FXCollections.observableArrayList();
try{
int studentId = Integer.parseInt(id.getText());
String studentName = name.getText();
String studentGender = gender.getText();
int studentAge = Integer.parseInt(age.getText());
String studentProfession = profession.getText();
int studentPhone = Integer.parseInt(phone.getText());
Student student = new Student(studentId, studentName, studentGender, studentAge, studentProfession, studentPhone);
Teachers_operate.add_students(student);
} catch (Exception e) {
Parent root = null;
try {
sss=false;
root = FXMLLoader.load(getClass().getResource("/Tips/warn.fxml"));
Scene scene =new Scene(root,280,270);
Stage stage=new Stage();
stage.setResizable(false);
stage.setTitle("搞笑");
stage.setScene(scene);
// 获取当前窗口的 Stage 对象
Stage currentStage = (Stage) commit.getScene().getWindow();
// 将新窗口置于最高层级
stage.setAlwaysOnTop(true);
// 设置新窗口为应用程序模态对话框
stage.initModality(Modality.APPLICATION_MODAL);
// 设置新窗口的所有者为当前舞台
stage.initOwner(currentStage); // currentStage 是当前窗口的 Stage 对象
stage.showAndWait();
} catch (IOException ex) {
ex.printStackTrace();
}
commit.getScene();
}
}
}
(3)修改功能 (需要跳转新页面)
用ScenceBuilde设置不可修改
package Teachers;
import dao.Teachers_operate;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseEvent;
import javafx.stage.Modality;
import javafx.stage.Stage;
import pojo.Student;
import java.io.IOException;
public class A_A_Modify {
@FXML
private TextField id;
@FXML
private TextField name;
@FXML
private TextField gender;
@FXML
private TextField age;
@FXML
private TextField profession;
@FXML
private TextField phone;
Student stu= A_Information.getSelectedData();
//获取老ID,根据老ID修改学生信息
static int oldId;
// 初始化方法
@FXML
public void initialize() {
// setText(); 这个方法里面的值必须是String类型的,所以需要转化一下
id.setText(String.valueOf(stu.getId()));
name.setText(stu.getName());
gender.setText(stu.getGender());
age.setText(String.valueOf(stu.getAge()));
profession.setText(stu.getProfession());
phone.setText(String.valueOf(stu.getPhone()));
//每次重新设置老id
oldId = A_Information.getSelectedData().getId();
}
@FXML
private Button commit;
//将获取到文本框中的内容重新加载,并关闭修改页面
public void get_commit(ActionEvent actionEvent) throws Exception{
//执行修改操作
upDate();
//当点击此按钮时,关闭此页面
Stage previousStage = (Stage) commit.getScene().getWindow();
previousStage.close();
}
private void upDate() throws IOException {
try {
// 从文本框中获取内容
int updatedId = Integer.parseInt(id.getText());
String updatedName = name.getText();
String updatedGender = gender.getText();
int updatedAge = Integer.parseInt(age.getText());
String updatedProfession = profession.getText();
int updatedPhone = Integer.parseInt(phone.getText());
// 更新 stu 对象的属性
A_Information.getSelectedData().setId(updatedId);
A_Information.getSelectedData().setName(updatedName);
A_Information.getSelectedData().setGender(updatedGender);
A_Information.getSelectedData().setAge(updatedAge);
A_Information.getSelectedData().setProfession(updatedProfession);
A_Information.getSelectedData().setPhone(updatedPhone);
Student student = new Student(updatedId, updatedName, updatedGender, updatedAge, updatedProfession, updatedPhone);
// 执行数据库操作
Teachers_operate.edit(student,oldId);
} catch (Exception e) {
Parent root = null;
root = FXMLLoader.load(getClass().getResource("/Tips/warn.fxml"));
Scene scene =new Scene(root,280,270);
Stage stage=new Stage();
stage.setResizable(false);
stage.setTitle("搞笑");
stage.setScene(scene);
// 获取当前窗口的 Stage 对象
Stage currentStage = (Stage) commit.getScene().getWindow();
// 将新窗口置于最高层级
stage.setAlwaysOnTop(true);
// 设置新窗口为应用程序模态对话框
stage.initModality(Modality.APPLICATION_MODAL);
// 设置新窗口的所有者为当前舞台
stage.initOwner(currentStage); // currentStage 是当前窗口的 Stage 对象
stage.showAndWait();
}
System.out.println(oldId);
// 在数据库中对上面的值进行匹配,如果匹配成功就直接修改
}
//返回按钮
@FXML
private Button rtn;
public void set_return(MouseEvent mouseEvent) throws Exception{
Stage previousStage = (Stage) rtn.getScene().getWindow();
// 关闭此页面
previousStage.close();
}
}