JavaFX MySQL 資料庫連線
將 JavaFX 應用程式連接到 MySQL 資料庫需要一個特定的類別來處理連線和資料擷取。這是一個簡單的範例:
PersonDataAccessor:
import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; import java.sql.ResultSet; import java.util.List; import java.util.ArrayList; public class PersonDataAccessor { private Connection connection; // Constructor public PersonDataAccessor(String driverClassName, String dbURL, String user, String password) throws SQLException, ClassNotFoundException { Class.forName(driverClassName); connection = DriverManager.getConnection(dbURL, user, password); } // Close connection public void shutdown() throws SQLException { if (connection != null) { connection.close(); } } // Get person list public List<Person> getPersonList() throws SQLException { try ( Statement stmnt = connection.createStatement(); ResultSet rs = stmnt.executeQuery("select * from person"); ) { List<Person> personList = new ArrayList<>(); while (rs.next()) { String firstName = rs.getString("first_name"); String lastName = rs.getString("last_name"); String email = rs.getString("email_address"); Person person = new Person(firstName, lastName, email); personList.add(person); } return personList; } } }
此類處理連線建立、查詢執行和資料擷取。 getPersonList() 方法查詢資料庫中的「person」表,並將檢索到的資料轉換為 Person 物件清單。
Person 模型(資料表示):
import javafx.beans.property.StringProperty; import javafx.beans.property.SimpleStringProperty; public class Person { private StringProperty firstName = new SimpleStringProperty(this, "firstName"); public StringProperty firstNameProperty() { return firstName; } public String getFirstName() { return firstNameProperty().get(); } public void setFirstName(String firstName) { firstNameProperty().set(firstName); } private StringProperty lastName = new SimpleStringProperty(this, "lastName"); public StringProperty lastNameProperty() { return lastName; } public String getLastName() { return lastNameProperty().get(); } public void setLastName(String lastName) { lastNameProperty().set(lastName); } private StringProperty email = new SimpleStringProperty(this, "email"); public StringProperty emailProperty() { return email; } public String getEmail() { return emailProperty().get(); } public void setEmail(String email) { emailProperty().set(email); } public Person() {} public Person(String firstName, String lastName, String email) { setFirstName(firstName); setLastName(lastName); setEmail(email); } }
該類別定義了具有名字、姓氏和屬性的Person 物件email.
UI類別(在表格中顯示資料):
import javafx.application.Application; import javafx.scene.control.TableView; import javafx.scene.control.TableColumn; import javafx.scene.control.cell.PropertyValueFactory; import javafx.scene.layout.BorderPane; import javafx.scene.Scene; import javafx.stage.Stage; public class PersonTableApp extends Application { private PersonDataAccessor dataAccessor; @Override public void start(Stage primaryStage) throws Exception { dataAccessor = new PersonDataAccessor(...); // Provide DB connection details TableView<Person> personTable = new TableView<>(); TableColumn<Person, String> firstNameCol = new TableColumn<>("First Name"); firstNameCol.setCellValueFactory(new PropertyValueFactory<>("firstName")); TableColumn<Person, String> lastNameCol = new TableColumn<>("Last Name"); lastNameCol.setCellValueFactory(new PropertyValueFactory<>("lastName")); TableColumn<Person, String> emailCol = new TableColumn<>("Email"); emailCol.setCellValueFactory(new PropertyValueFactory<>("email")); personTable.getColumns().addAll(firstNameCol, lastNameCol, emailCol); personTable.getItems().addAll(dataAccessor.getPersonList()); BorderPane root = new BorderPane(); root.setCenter(personTable); Scene scene = new Scene(root, 600, 400); primaryStage.setScene(scene); primaryStage.show(); } @Override public void stop() throws Exception { if (dataAccessor != null) { dataAccessor.shutdown(); } } public static void main(String[] args) { launch(args); } }
此類初始化 UI 並在 TableView 中顯示 Person 物件。
透過執行以下步驟,您可以建立 JavaFX 應用程序,該應用程式成功連接到 MySQL 資料庫、檢索資料並以使用者友好的方式顯示它表。
以上是如何將 JavaFX 應用程式連接到 MySQL 資料庫並在 TableView 中顯示資料?的詳細內容。更多資訊請關注PHP中文網其他相關文章!