B
B
BaLahmuT2021-09-14 16:15:21
MySQL
BaLahmuT, 2021-09-14 16:15:21

How to make sql query from MySQL to TableView?

I need to display data from MySQL in TableView. Deployed Spring, made a connection to the database:

spring.datasource.url=jdbc:mysql://localhost:3306/usersdb?createDatabaseIfNotExist=true
spring.datasource.username=root
spring.datasource.password=pass
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update

server.port=8080

Set up JavaFX, made a table model:
public class ModelTable
{
    String id, login, password, surname, name;
    
    public ModelTable(String id, String login, String password, String surname, String name)
    {
        this.id = id;
        this.login = login;
        this.password = password;
        this.surname = surname;
        this.name = name;
    }
   //здесь сеттеры и геттеры
}

Based on the model, I made a controller:
public class TableController implements Initializable
{
    @FXML
    private TableView<ModelTable> table;
    
    @FXML
    private TableColumn<ModelTable, String> userId;
    
    @FXML
    private TableColumn<ModelTable, String> userLogin;
    
    @FXML
    private TableColumn<ModelTable, String> userPass;
    
    @FXML
    private TableColumn<ModelTable, String> userSurname;
    
    @FXML
    private TableColumn<ModelTable, String> userName;
    
    ObservableList<ModelTable> list = FXCollections.observableArrayList();
    
    @Override
    public void initialize(URL location, ResourceBundle resources)
    {
        //id, login, password, surname, name
        userId.setCellValueFactory(new PropertyValueFactory<>("id"));
        userLogin.setCellValueFactory(new PropertyValueFactory<>("login"));
        userPass.setCellValueFactory(new PropertyValueFactory<>("password"));
        userSurname.setCellValueFactory(new PropertyValueFactory<>("surname"));
        userName.setCellValueFactory(new PropertyValueFactory<>("name"));

       //если правильно понял запрос должен быть здесь
    }
}

Can you tell me how to query the database?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Orkhan, 2021-09-14
Hasanly @azerphoenix

Good evening.
I would ask you to specify your question a little, because the following points are not clear:
1) Is your project a client-server application? If so, do I understand correctly that you have a client - JavaFX and a server - Spring Boot. If so, then it turns out that you want to execute a request from the client to the server and get the data stored in the database. In this case, you need to write a REST service on the server side (in Spring). For example, a controller that processes GET requests and returns the corresponding data in json format. Then on a successful response, your client (javaFX) parses the json and maps it to the pojo. After that it is displayed in the TableView.
Based on the above, the answer to your question is:

Can you tell me how to query the database?

You need to:
- Write a REST service (no need to access the database directly from the client)
- Use the library to execute queries. For example, Retrofit
2) If your application is not a client-server, but only a JavaFX application that uses the Spring framework. For example, using the JavaFX Weaver lib
https://habr.com/ru/post/478402/
https://github.com/rgielen/javafx-weaver
Then, of course, a third-party http client and generally an http client is not needed. You can access the database by writing methods on the Repository and then mapping them to a dto (pojo) and displaying them in a table.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question