请使用 JavaFX MySQL 连接示例 [英] JavaFX MySQL connection example please

查看:29
本文介绍了请使用 JavaFX MySQL 连接示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能给我一个连接 JavaFX 和 MySQL 的类的例子,不需要 Main 类,有一个,只想要一个将任何应用程序连接到 MySQL 数据库并从该数据库中获取一行的类的例子表,搜索了整个互联网,我没有找到任何直截了当的内容,我不想要任何花哨的东西,只是为了完成工作.一些干净和简单的东西.

解决方案

您至少需要三个类:一个用于表示您的数据,一个用于您的 UI,一个用于管理数据库连接.当然,在真正的应用程序中,您需要的不止这些.此示例遵循与 TableView<相同的基本示例/code> 教程

假设您的数据库有一个 person 表,其中包含三列,first_namelast_nameemail_address.

然后你会写一个Person类:

import javafx.beans.property.StringProperty ;导入 javafx.beans.property.SimpleStringProperty ;公共类人{private final StringProperty firstName = new SimpleStringProperty(this, "firstName");公共 StringProperty firstNameProperty() {返回名字;}公共最终字符串 getFirstName() {返回 firstNameProperty().get();}public final void setFirstName(String firstName) {firstNameProperty().set(firstName);}private final StringProperty lastName = new SimpleStringProperty(this, "lastName");公共 StringProperty lastNameProperty() {返回姓氏;}公共最终字符串 getLastName() {返回 lastNameProperty().get();}public final void setLastName(String lastName) {lastNameProperty().set(lastName);}private final StringProperty email = new SimpleStringProperty(this, "email");公共 StringProperty emailProperty() {回邮件;}公共最终字符串 getEmail() {返回 emailProperty().get();}公共最终无效 setEmail(字符串电子邮件){emailProperty().set(email);}公共人(){}公共人(字符串名字,字符串姓氏,字符串电子邮件){设置名字(名字);设置姓氏(姓氏);设置电子邮件(电子邮件);}}

从数据库访问数据的类:

import java.sql.Connection;导入 java.sql.DriverManager ;导入 java.sql.SQLException ;导入 java.sql.Statement ;导入 java.sql.ResultSet ;导入 java.util.List ;导入 java.util.ArrayList ;公共类 PersonDataAccessor {//在现实生活中,使用连接池....私人连接连接;public PersonDataAccessor(String driverClassName, String dbURL, String user, String password) 抛出 SQLException, ClassNotFoundException {Class.forName(driverClassName);connection = DriverManager.getConnection(dbURL, 用户, 密码);}public void shutdown() 抛出 SQLException {如果(连接!= null){connection.close();}}公共列表<人>getPersonList() 抛出 SQLException {尝试 (语句 stmnt = connection.createStatement();ResultSet rs = stmnt.executeQuery("select * from person");){列表<人>personList = new ArrayList<>();而(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);}返回人员列表;}}//其他方法,例如.addPerson(...) 等}

然后是一个 UI 类:

import javafx.application.Application ;导入 javafx.scene.control.TableView ;导入 javafx.scene.control.TableColumn ;导入 javafx.scene.control.cell.PropertyValueFactory ;导入 javafx.scene.layout.BorderPane ;导入 javafx.scene.Scene ;导入 javafx.stage.Stage ;公共类 PersonTableApp 扩展应用程序 {私有 PersonDataAccessor 数据访问器;@覆盖public void start(Stage primaryStage) 抛出异常 {dataAccessor = new PersonDataAccessor(...);//提供驱动程序名称、dbURL、用户、密码...TableView<人>personTable = new TableView<>();TableColumnfirstNameCol = new TableColumn<>("名字");firstNameCol.setCellValueFactory(new PropertyValueFactory<>("firstName"));TableColumnlastNameCol = new TableColumn<>("Last Name");lastNameCol.setCellValueFactory(new PropertyValueFactory<>("lastName"));TableColumnemailCol = 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);场景场景 = 新场景(root, 600, 400);primaryStage.setScene(场景);primaryStage.show();}@覆盖public void stop() 抛出异常 {如果(数据访问器!= null){dataAccessor.shutdown();}}公共静态无效主(字符串 [] args){发射(参数);}}

(我只是在没有测试的情况下输入了它,所以可能会有拼写错误、缺少导入等,但应该足以让您了解.)

Can anyone give me one example of a class that connects JavaFX with MySQL, dont want Main class, have one, just want a example of a class that connects any application to a MySQL database and gets a row from that database into a table, searched the whole internet and i didn't find anything straight to the point i do not want anything fancy just something to get the job done please. Something clean and simple.

解决方案

At a minimum, you need three classes: one to represent your data, one for your UI, and one to manage the database connection. In a real app you'd need more than this, of course. 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 would 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);
    }

}

A class to access the 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
}

And then a 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, 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 that in without testing, so there may be typos, missing imports, etc, but it should be enough to give you the idea.)

这篇关于请使用 JavaFX MySQL 连接示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆