简体   繁体   中英

How to correctly connect SQLite with textfields and labels in JavaFX?

I'm making an application with JavaFX, Scene Builder and SQLite. For managing SQLite database I'm using DB Browser

I have 3 fields in SQLite: ID, question, answer

When I press on Button "Add" a method is called and sends text from textaria with question to question tab in SQLite and text from textaria with answer do the same.

ID is a number and is autoincremented when I add these fields to SQLite

I successfully sent data from my window but I don't understand how to get data from SQlite and set it to label and combobox in my window

Window

DB Browser

QuestController:

package card;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;

import java.net.URL;
import java.sql.*;
import java.util.ResourceBundle;




public class QuestController implements Initializable {

  @FXML  private TextArea ta_questText, ta_answerText;

  @FXML  private Label lb_numberQuest;

  @FXML
  private ComboBox<?> idQuest;

  @Override
  public void initialize(URL location, ResourceBundle resources) {

    //register QuestController in  Context Class
    Context.getInstance().setQuestController(this);

  }

  @FXML
  void addCard(ActionEvent event) {

    PreparedStatement preparedStatement;
    ResultSet resultSet;
    String query = "select * from Cards where ID  = ? and question = ? and 
answer = ?";
Connection connection = DbConnection.getInstance().getConnection();


    try {
  String question = ta_questText.getText();
  String answer = ta_answerText.getText();

  Statement statement = connection.createStatement();

  int status = statement.executeUpdate("insert into Cards (question, 
 answer) values ('"+question+"','"+answer+"')");

  if (status > 0) {
    System.out.println("question registered");
      }

    } catch (SQLException e) {
      e.printStackTrace();
    }

  }
}

DbConnection class:

package card;

import org.sqlite.SQLiteConnection;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DbConnection {

  private DbConnection() {

  }

  public static DbConnection getInstance() {

   return new DbConnection();

 }

  public Connection getConnection() {



    String connect_string = "jdbc:sqlite:database.db";

    Connection connection = null;
    try {
  Class.forName("org.sqlite.JDBC");
   connection = DriverManager.getConnection("jdbc:sqlite:database.db");
} catch (SQLException e) {
  e.printStackTrace();
} catch (ClassNotFoundException e) {
  e.printStackTrace();
     }

     return connection;
  }

}

How can I get all existing ID(numbers) from SQLite and put it to combobox?

How can choose a number(ID) and apply number to a label?

if I choose any number from combobox

How can I apply text with question and answer from SQLite connected to that number to textarias in my window?

Just like the comments in your post, I highly suggest breaking down your codes into layers for better manageability. Good job on starting with your DbConnection class. Additionally, JavaFx already setup some layer for you to start on.

These are the:

  • View : your FXML file that's created on scene builder
  • Controller : the JFx controller that FXML forces you to use

Now it is up to you to add more layers to manage your code. I will suggest starting with these:

  • Model : this will be the main data structure you will work on. For starters, maybe you can follow the structure of your database? Example: class Card with fields id , question , and answer .
  • Persistence : this Java class will hold your SQL code. This is also responsible for converting the ResultSet object to your model object.

Then finally, keep in mind that your are working with layers. Make sure that their interactions don't leak.

View (FXML) <--> Controller + Model <--> Persistence + Model

To answer your questions:

How can I get all existing ID(numbers) from SQLite and put it to combobox?

  1. Perform an SQL SELECT using your SQLite connection to fetch all the id s. ( SELECT id FROM cards perhaps?)

  2. On successful SELECT call, iterate through the ResultSet object. Each iteration should fetch the data from id column, convert it to string (or whatever type your combobox accepts), then add them all. (something like this: comboBox.getItems().add(id) )

How can choose a number(ID) and apply number to a label?

How can I apply text with question and answer from SQLite connected to that number to textarias in my window?

  1. Perform an SQL SELECT using your connection, this time add a WHERE clause in the statement to filter results. Since there is a dynamic part in your SQL now, using PreparedStatement will be good. Example: SELECT id, question, answer FROM card WHERE id=?

  2. Using the results of the SQL calls, assign them to the proper JFx Components such as labels and text areas.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM