请关注JavaFX MySQL连接示例

前端之家收集整理的这篇文章主要介绍了请关注JavaFX MySQL连接示例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
任何人都可以给我一个连接 JavaFX和 MySQL的类的例子,不想要Main类,有一个,只想要一个将任何应用程序连接到MysqL数据库并从该数据库获取一行到表中的一个例子,搜索整个互联网,我没有找到任何直截了当的地方我不想要任何花哨的东西来完成工作请.
干净简单的东西.

解决方法

至少,您需要三个类:一个用于表示数据,一个用于UI,另一个用于管理数据库连接.当然,在真正的应用程序中,您需要的不仅仅是这个.此示例遵循与 TableView tutorial相同的基本示例 @H_502_7@假设您的数据库有一个包含三列的person表,first_name,last_name,email_address.

@H_502_7@然后你会写一个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);
    }

}
@H_502_7@从数据库访问数据的类:

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
}
@H_502_7@然后是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,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);
    }
}
@H_502_7@(我只是在未经测试的情况下输入,因此可能存在拼写错误,缺少导入等,但它应该足以让您了解.)

原文链接:https://www.f2er.com/java/121195.html

猜你在找的Java相关文章