Mysql
 sql >> डेटाबेस >  >> RDS >> Mysql

JavaFX MySQL कनेक्शन उदाहरण कृपया

कम से कम, आपको तीन वर्गों की आवश्यकता है:एक आपके डेटा का प्रतिनिधित्व करने के लिए, एक आपके UI के लिए, और दूसरा डेटाबेस कनेक्शन को प्रबंधित करने के लिए। एक वास्तविक ऐप में आपको निश्चित रूप से इससे अधिक की आवश्यकता होगी। यह उदाहरण उसी मूल उदाहरण का अनुसरण करता है जैसे TableView ट्यूटोरियल

मान लीजिए आपके डेटाबेस में एक person है तीन स्तंभों वाली तालिका, first_name , last_name , email_address

तब आप एक person write लिखेंगे कक्षा:

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);
    }
}

(मैंने अभी इसे बिना परीक्षण के टाइप किया है, इसलिए टाइपो, लापता आयात आदि हो सकते हैं, लेकिन यह आपको विचार देने के लिए पर्याप्त होना चाहिए।)



  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. पायथन MySQL कनेक्टर - फ़ेचोन का उपयोग करते समय अपठित परिणाम मिला

  2. DEFAULT क्लॉज में CURRENT_TIMESTAMP के साथ केवल एक TIMESTAMP कॉलम क्यों हो सकता है?

  3. WEEKOFYEAR () उदाहरण – MySQL

  4. MySQL FULL OUTER JOIN पर सिंटैक्स त्रुटि की रिपोर्ट क्यों करता है?

  5. MySQL में दो टाइमस्टैम्प के बीच अंतर की गणना कैसे करें