连接层,创建实体类
一个Student对象,用来充当后续遍历集合的泛型
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;
//有参构造
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);
}
//get、set方法
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);
}
}
使用Scence Builder创建一个fxml页面
在控制器类中加载数据库数据
最开始有初始化页面,当进入到此页面中,也就是fxml绑定的控制器类
在此类中对fxml按钮进行绑定
package Students;
import dao.JDBC;
import dao.students;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.MouseEvent;
import pojo.Student;
import java.sql.ResultSet;
public class a_information {
//返回上一个界面
@FXML
private Button rtn;
public void set_return(MouseEvent mouseEvent) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("/Students/user.fxml"));
rtn.getScene().setRoot(root);
}
// 声明每一个控件
@FXML
private TableView<Student> tableView;;
@FXML
private TableColumn<Student,Integer>id;
@FXML
private TableColumn<Student,String> name;
@FXML
private TableColumn <Student,String> gender;
@FXML
private TableColumn <Student,Integer>age;
@FXML
private TableColumn<Student,String> profession;
@FXML
private TableColumn<Student,Integer> phone;
//此方法,用来将初始化好的表格(下面的loadData();),在页面加载的时候就将查询到的数据插入
//因为当FXML文件加载并关联控制器类时,JavaFX会自动创建控制器类的实例,
// 并自动调用initialize()方法。在initialize()方法内部,loadData()方法被调用,
// 从而实现在加载FXML文件时自动加载数据。
@FXML
public void initialize() throws Exception{
loadData();
}
//这个方法用来初始化表格
public void loadData() throws Exception{
String sql = "SELECT id, name, gender, age, profession, phone FROM small_systems.student";
//这个地方将JDBC的连接等都封装完毕,所以可以直接执行sql语句
ResultSet rs = JDBC.executeQuery(sql);
ObservableList<Student> studentList = FXCollections.observableArrayList();
//循环,获取数据库中的每一个数,并添加到上面定义的
//以Student为泛型的studentList集合当中
while (rs.next()) {
int studentId = rs.getInt("id");
String studentName = rs.getString("name");
String studentGender = rs.getString("gender");
int studentAge = rs.getInt("age");
String studentProfession = rs.getString("profession");
int studentPhone = rs.getInt("phone");
Student student = new Student(studentId, studentName, studentGender, studentAge, studentProfession, studentPhone);
studentList.add(student);
}
//设置每个数据,使每个数据显示出来
id.setCellValueFactory(new PropertyValueFactory<>("id"));
name.setCellValueFactory(new PropertyValueFactory<>("name"));
gender.setCellValueFactory(new PropertyValueFactory<>("gender"));
age.setCellValueFactory(new PropertyValueFactory<>("age"));
profession.setCellValueFactory(new PropertyValueFactory<>("profession"));
phone.setCellValueFactory(new PropertyValueFactory<>("phone"));
tableView.setItems(studentList);
}
}
最后补充一下JDBC的封装类
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)) {