Follow the JavaFX MySQL connection example
Anyone can give me an example of a class connecting JavaFX and mysql. I don't want the main class. There is one. I just want to connect any application to the MySQL database and get a row from the database to an example in the table. Search the whole Internet. I can't find any direct place. I don't want any fancy things to complete the work. Please
Solution
At a minimum, you need three classes: one for representing data, one for UI, and one for managing database connections Of course, in a real application, you need more than that This example follows the same basic example as tableview tutorial
Suppose your database has a person table with three columns, first_ name,last_ name,email_ address.
Then you will write a person class:
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); } }
Classes that access data from the database:
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 }
Then the UI class:
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,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); } }
(I just input without test, so there may be spelling errors, lack of import, etc., but it should be enough for you to understand.)