JavaFX MySQL Connection Example

Can someone give me one example of a class that connects JavaFX to MySQL, doesn’t want the main class to have one, just need an example of a class that connects any application to the MySQL database and gets a row from this database to the table, I searched the entire Internet, and I didn’t find anything straight to the point. I don’t want something to seem just something to do the job, please. Something clean and simple.

+7
mysql javafx-2
source share
2 answers

At a minimum, you need three classes: one for representing your data, one for your user interface and one for managing your database connection. Of course, in a real application you will need more than that. This example follows the same basic example as the TableView tutorial .

Suppose your database has a person table with three columns, first_name , last_name , email_address .

Then you should write the 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); } } 

Class for accessing data from a 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 } 

And then the user interface 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, 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); } } 

(I just typed this without testing, so there may be typos, missing imports, etc., but that should be enough to give you this idea.)

+17
source share

In addition to James_D's answer:

I wanted to connect to a remote (MySQL) database, so I changed the constructor and connected only using url-only:

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

Initiate through:

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

Note: You will also need to enable the lib connector. I chose mysql-connector-java-5.1.40-bin.jar that came with IntelliJ and was under /Users/martin/Library/Preferences/IntelliJIdea2017.1/jdbc-drivers/MySQL Connector/J/5.1.40/mysql-connector-java-5.1.40-bin.jar

Kudos are owned by James_D.

+2
source share

All Articles