-1

I have been trying to query the data from my database and pass it through my JFXComboBox as value

I tried wrapping it to a while loop and add every item to the JFXComboBox but it still does not show to value from my data.

@FXML
JFXComboBox<String> combobox;
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
  String sql = "Select * from transaction_type from transactions";
  try(Connection conn = SQLConnection.getConnection(); /*Singleton Class DB       Connection*/
  Statement statement = conn.createStatement();
  ResultSet rs = statement.executeQuery(sql))
 {
   while(rs.next()){
     combobox.getItems.add(rs.getString(2));
   }
 }
catch(SQLException e)
 {
   serr(e.getNessage());
 }
}

I expect the items in the JFXComboBox to be the value found in the transaction

fabian
  • 80,457
  • 12
  • 86
  • 114
  • There is a lot wrong with this design, but you'd want to start by creating an appropriate model class to hold the data from your database. Load the data into a `List` and then set that list as the items for your `ComboBox`. I'd recommend looking up some good tutorials on design patterns in Java to get a better feel on how to better approach this. – Zephyr Aug 23 '19 at 02:51
  • That being said, your SQL query has two `FROM` clauses. Did you mean to use `SELECT transaction_type FROM transactions;`? – Zephyr Aug 23 '19 at 03:23
  • Printout the values you get from the database. Are they what you expect ? If not sort it out before you continue. If you do post [mre] hard coding these values as test data. – c0der Aug 23 '19 at 05:32
  • @Zephyr Even with that fix the code won't work, sing `rs.getString(2)` only works with 2+ columns in the `ResultSet`... – fabian Aug 23 '19 at 08:28
  • @fabian - Agreed. I was not attempting to fix the code, but to point out one of the many issues in it. :) – Zephyr Aug 23 '19 at 18:48

2 Answers2

0

The correct way is to create a model class, however, I think this can help you.

Create a method:

public static void fillListFromDataBase (Connection conn, ObservableList<String> list) {
    String sql = "SELECT fieldName FROM transactions";
    try {
        Statement statement = conn.createStatement();
        ResultSet rs = statement.executeQuery(sql))

        while(rs.next()) {
            list.add(rs.getString("fieldName"));
        }
    } catch(SQLException e) {
        e.printStackTrace();
    }
}

Then call the method to load the data in the list and bind the list to Combobox:

JFXComboBox<String> combobox;

private ObservableList<String> list = FXCollections.observableArrayList();

@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
    Connection conn = SQLConnection.getConnection();

    fillListFromDataBase (conn, list);
    combobox.setItems(list);
}
Scarlett
  • 16
  • 2
  • Do i need to use `StringProperty` if i use the model approach and what kind of generic method will i extend i know that when using `JFXTreeTable` you extend `RecursiveTreeObject` – Ivan Lorenzo Aug 23 '19 at 04:24
-1

It will not excute the query it will cause an error due to syntax error and it will thrown an exception

String sql = "Select * from transaction_type from transactions";

This query simplifies to get all the records from transactions

//try this query
String sql = "Select * from transactions";
Cyrille Con Morales
  • 918
  • 1
  • 6
  • 21