如何將 JavaFX 應(yīng)用程序連接到 MySQL 數(shù)據(jù)庫并在 TableView 中顯示數(shù)據(jù)?
Nov 25, 2024 am 05:29 AMJavaFX MySQL 數(shù)據(jù)庫連接
將 JavaFX 應(yīng)用程序連接到 MySQL 數(shù)據(jù)庫需要一個(gè)特定的類來處理連接和數(shù)據(jù)檢索。這是一個(gè)簡單的示例:
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; } } }
此類處理連接建立、查詢執(zhí)行和數(shù)據(jù)檢索。 getPersonList() 方法查詢數(shù)據(jù)庫中的“person”表,并將檢索到的數(shù)據(jù)轉(zhuǎn)換為 Person 對象列表。
Person 模型(數(shù)據(jù)表示):
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 類(在表中顯示數(shù)據(jù)):
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 對象。
通過執(zhí)行以下步驟,您可以創(chuàng)建一個(gè) JavaFX 應(yīng)用程序,該應(yīng)用程序成功連接到 MySQL 數(shù)據(jù)庫、檢索數(shù)據(jù)并以用戶友好的方式顯示它表。
以上是如何將 JavaFX 應(yīng)用程序連接到 MySQL 數(shù)據(jù)庫并在 TableView 中顯示數(shù)據(jù)?的詳細(xì)內(nèi)容。更多信息請關(guān)注PHP中文網(wǎng)其他相關(guān)文章!

熱AI工具

Undress AI Tool
免費(fèi)脫衣服圖片

Undresser.AI Undress
人工智能驅(qū)動(dòng)的應(yīng)用程序,用于創(chuàng)建逼真的裸體照片

AI Clothes Remover
用于從照片中去除衣服的在線人工智能工具。

Clothoff.io
AI脫衣機(jī)

Video Face Swap
使用我們完全免費(fèi)的人工智能換臉工具輕松在任何視頻中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費(fèi)的代碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
功能強(qiáng)大的PHP集成開發(fā)環(huán)境

Dreamweaver CS6
視覺化網(wǎng)頁開發(fā)工具

SublimeText3 Mac版
神級代碼編輯軟件(SublimeText3)

TosecurelyConnectToaremoteMysqlServer,Usesshtunneling,configuremysqlforremoteaccess,setFireWallrules,andConsidersSlencryption 。首先,stardansshtunnelwithssh-l3307:localhost:3306user@remote-Server-server-nandConnectViamySql-h127.0.0.0.0.1-p3307.second,editmys

開啟MySQL慢查詢?nèi)罩静⒎治隹啥ㄎ恍阅軉栴}。 1.編輯配置文件或動(dòng)態(tài)設(shè)置slow_query_log和long_query_time;2.日志包含Query_time、Lock_time、Rows_examined等關(guān)鍵字段,輔助判斷效率瓶頸;3.使用mysqldumpslow或pt-query-digest工具高效分析日志;4.優(yōu)化建議包括添加索引、避免SELECT*、拆分復(fù)雜查詢等。例如為user_id加索引能顯著減少掃描行數(shù),提升查詢效率。

mysqldump是用于執(zhí)行MySQL數(shù)據(jù)庫邏輯備份的常用工具,它生成包含CREATE和INSERT語句的SQL文件以重建數(shù)據(jù)庫。1.它不備份原始文件,而是將數(shù)據(jù)庫結(jié)構(gòu)和內(nèi)容轉(zhuǎn)換為可移植的SQL命令;2.適用于小型數(shù)據(jù)庫或選擇性恢復(fù),不適合TB級數(shù)據(jù)快速恢復(fù);3.常用選項(xiàng)包括--single-transaction、--databases、--all-databases、--routines等;4.恢復(fù)時(shí)使用mysql命令導(dǎo)入,并可關(guān)閉外鍵檢查以提升速度;5.建議定期測試備份、使用壓縮、自動(dòng)化調(diào)

處理MySQL中的NULL值需注意:1.設(shè)計(jì)表時(shí)關(guān)鍵字段設(shè)為NOTNULL,可選字段允許NULL;2.查詢判斷必須用ISNULL或ISNOTNULL,不能用=或!=;3.可用IFNULL或COALESCE函數(shù)替換顯示默認(rèn)值;4.插入或更新時(shí)直接使用NULL值需謹(jǐn)慎,注意數(shù)據(jù)源和ORM框架處理方式。NULL表示未知值,不等于任何值,包括自身,因此查詢、統(tǒng)計(jì)、連接表時(shí)要特別小心,避免漏數(shù)據(jù)或邏輯錯(cuò)誤。合理使用函數(shù)和約束可以有效減少因NULL帶來的干擾。

要查看MySQL數(shù)據(jù)庫和表的大小,可直接查詢information_schema或使用命令行工具。1.查看整個(gè)數(shù)據(jù)庫大?。簣?zhí)行SQL語句SELECTtable_schemaAS'Database',SUM(data_length index_length)/1024/1024AS'Size(MB)'FROMinformation_schema.tablesGROUPBYtable_schema;可獲取所有數(shù)據(jù)庫的總大小,也可加WHERE條件限定具體數(shù)據(jù)庫;2.查看單個(gè)表大小:通過SELECTta

字符集和排序規(guī)則問題常見于跨平臺遷移或多人開發(fā)時(shí),導(dǎo)致亂碼或查詢不一致。核心解決方法有三:一要檢查并統(tǒng)一數(shù)據(jù)庫、表、字段的字符集為utf8mb4,通過SHOWCREATEDATABASE/TABLE查看,用ALTER語句修改;二要在客戶端連接時(shí)指定utf8mb4字符集,在連接參數(shù)或執(zhí)行SETNAMES中設(shè)置;三要合理選擇排序規(guī)則,推薦使用utf8mb4_unicode_ci以確保比較和排序準(zhǔn)確性,并在建庫建表時(shí)指定或通過ALTER修改。

GROUPBY用于按字段分組數(shù)據(jù)并執(zhí)行聚合操作,HAVING用于過濾分組后的結(jié)果。例如,使用GROUPBYcustomer_id可計(jì)算每個(gè)客戶的總消費(fèi)金額;配合HAVING可篩選出總消費(fèi)超過1000的客戶。SELECT后的非聚合字段必須出現(xiàn)在GROUPBY中,HAVING可使用別名或原始表達(dá)式進(jìn)行條件篩選。常見技巧包括統(tǒng)計(jì)每組數(shù)量、多字段分組、結(jié)合多個(gè)條件過濾。

MySQL支持事務(wù)處理,使用InnoDB存儲引擎可確保數(shù)據(jù)一致性和完整性。1.事務(wù)是一組SQL操作,要么全部成功,要么全部失敗回滾;2.ACID屬性包括原子性、一致性、隔離性和持久性;3.手動(dòng)控制事務(wù)的語句為STARTTRANSACTION、COMMIT和ROLLBACK;4.四種隔離級別包括讀未提交、讀已提交、可重復(fù)讀和串行化;5.正確使用事務(wù)需注意避免長時(shí)間運(yùn)行、關(guān)閉自動(dòng)提交、合理處理鎖及異常。通過這些機(jī)制,MySQL可實(shí)現(xiàn)高可靠與并發(fā)控制。
