-1

I am using a DAL and I am trying to populate a ComboBox with data from the DAL.

I am aware of how to do this when I am connecting to the database directly from the GUI code, however I am trying to use Classes so that I do not need to reconnect to the database every time I make a new method in the GUI code.

I use NetBeans IDE, is there a way to do this?

1 Answers1

0

Welcome to Stack Overflow.

In the constructor, or whenever you fill your combobox, you will get the data from the database with a query:

Statement statement = dbConnection.createStatement();
ResultSet rs = statement.executeQuery("SELECT name FROM users"); // a query example, you will replace it with the query you need
List<String> comboContent = new ArrayList<>();

while (rs.next()) {
    comboContent.add(rs.getString("name"); // get content by column name
}

And in your JComboBox you will add the result in it's construction:

JComboBox myCombo = new JComboBox(comboContent.asList());

tomrlh
  • 1,006
  • 1
  • 18
  • 39