栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

JavaFX MySQL连接示例请

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JavaFX MySQL连接示例请

至少需要三个类:一个用于表示数据,一个用于UI,以及一个用于管理数据库连接。当然,在真正的应用程序中,您将需要的还不止这些。本示例遵循与教程相同的基本示例

TableView

假设你的数据库有一个

person
三列的表,
first_name
last_name
email_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);    }}

(我只是在未经测试的情况下输入了此内容,因此可能会有错别字,缺少输入内容等,但这足以让您知道。)



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/398869.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号