JavaFX MySQL连接示例请

任何人都可以给我一个连接JavaFX与MySQL的类的一个例子,不想要主类,有一个,只是想要一个类的示例,将任何应用程序连接到MySQL数据库,并从该数据库获取一行到一个表中,search整个互联网,我没有find任何直接的事情,我不想要什么幻想只是为了完成这项工作。 干净和简单的东西。

至less需要三个类:一个用于表示数据,一个用于UI,另一个用于pipe理数据库连接。 当然,在真实的应用程序中,您需要的不仅仅是这些。 本示例遵循与TableView教程相同的基本示例

假设你的数据库有一个有三列的person表, first_namelast_nameemail_address

那么你会写一个Person类:

 import javafx.beans.property.StringProperty ; import javafx.beans.property.SimpleStringProperty ; public class Person { private final StringProperty firstName = new SimpleStringProperty(this, "firstName"); public StringProperty firstNameProperty() { return firstName ; } public final String getFirstName() { return firstNameProperty().get(); } public final void setFirstName(String firstName) { firstNameProperty().set(firstName); } private final StringProperty lastName = new SimpleStringProperty(this, "lastName"); public StringProperty lastNameProperty() { return lastName ; } public final String getLastName() { return lastNameProperty().get(); } public final void setLastName(String lastName) { lastNameProperty().set(lastName); } private final StringProperty email = new SimpleStringProperty(this, "email"); public StringProperty emailProperty() { return email ; } public final String getEmail() { return emailProperty().get(); } public final void setEmail(String email) { emailProperty().set(email); } public Person() {} public Person(String firstName, String lastName, String email) { setFirstName(firstName); setLastName(lastName); setEmail(email); } } 

一个从数据库访问数据的类:

 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 { // in real life, use a connection pool.... private Connection connection ; public PersonDataAccessor(String driverClassName, String dbURL, String user, String password) throws SQLException, ClassNotFoundException { Class.forName(driverClassName); connection = DriverManager.getConnection(dbURL, user, password); } public void shutdown() throws SQLException { if (connection != null) { connection.close(); } } 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 ; } } // other methods, eg. addPerson(...) etc } 

然后是一个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 driverName, dbURL, user, password... 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); } } 

(我只是在没有testing的情况下input的,所以可能会出现拼写错误,缺less导入等,但它应该足以给你的想法。)

除了James_D的回答外 :

我想连接到一个远程(MySQL)数据库,所以我改变了构造函数并通过url-only连接:

 public UserAccessor(String dbURL, String user, String password) throws SQLException, ClassNotFoundException { connection = DriverManager.getConnection(dbURL, user, password); } 

初始化通过:

 UserAccessor userAccessor = new UserAccessor( "jdbc:mysql://xxx.xxx.xxx.xxx:YOUR_PORT", "YOUR_DB_USER", "YOUR_PASSWORD") 

请注意:您还需要包含连接器库。 我select了IntelliJ附带的mysql-connector-java-5.1.40-bin.jar ,位于/Users/martin/Library/Preferences/IntelliJIdea2017.1/jdbc-drivers/MySQL Connector/J/5.1.40/mysql-connector-java-5.1.40-bin.jar

荣誉属于James_D。