0

I am unable to populate a JComboBox with database data. This is my code to fill combobox with database data. I am able to connect to the database. The catch block is there to display error messages. What happened was that the error message does not pop up and when my application runs, the combobox is empty. This is my database table, and I want to populate the names into the combobox.

image

    package testing;

  import java.awt.EventQueue;
  import java.sql.Connection;
  import java.sql.PreparedStatement;
  import java.sql.ResultSet;

  import javax.swing.JFrame;
  import javax.swing.JOptionPane;
  import javax.swing.JPanel;
  import javax.swing.border.EmptyBorder;
  import javax.swing.JComboBox;

 public class combobox extends JFrame {

/**
 * 
 */
  private static final long serialVersionUID = 1L;
  private JPanel contentPane;
  private static JComboBox combobox_database;
  static Connection conn = null;

/**
 * Launch the application.
 */
  public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                sqliteconnection.dbConnector();
                fillcombobox();
                combobox frame = new combobox();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
  }

/**
 * Create the frame.
 */
public combobox() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 619, 524);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JComboBox combobox_database = new JComboBox();
    combobox_database.setBounds(208, 87, 175, 22);
    contentPane.add(combobox_database); 
}

public static void fillcombobox(){
    try{
        String sql = "SELECT name FROM testing_table";
        PreparedStatement pst = conn.prepareStatement(sql);
        ResultSet rs = pst.executeQuery();

        while(rs.next()){
         String s = rs.getString(2);
         combobox_database.addItem(s);

        }

        pst.close();
        rs.close();
        conn.close();

    }catch (Exception e){
        JOptionPane.showMessageDialog(null, e);
    }

   }

}

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    Try `combobox_database.addItem(rs.getString("name"));`. Also, make sure you close your resources. – bradimus Jul 14 '17 at 17:01
  • 1
    1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Jul 14 '17 at 17:09
  • NB: The above won't compile due to `sqliteconnection.dbConnector();` & therefore is not an MCVE / SSCCE of a run-time problem. – Andrew Thompson Jul 15 '17 at 09:15
  • 1) `}catch (Exception e){ JOptionPane.showMessageDialog(null, e);` should be `}catch (Exception e){ e.printStackTrace(); JOptionPane.showMessageDialog(null, e);` 2) See [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/q/3988788/418556) & [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/q/218384/418556) – Andrew Thompson Jul 15 '17 at 09:17

2 Answers2

2

Your query selects a single attribute, name, so the relevant columnIndex passed to getString() should be 1.

while(rs.next()) {
    String s = rs.getString(1);
    combobox_database.addItem(s);
}

As noted here, you can also use the columnLabel; a complete example is shown here; this related example uses JPA.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
1

What I see, is that "JComboBox combobox_database" is defined and initialized in another method, so the method fillcombobox() has no reference to it. I bet that your program has some compiling error.

ugo
  • 284
  • 1
  • 2
  • 10
  • i double checked and i found out that i forgot to call the method but when i call the method , it says java.lang.NullPointerException. I tried 1, 2 and name but to no avail –  Jul 15 '17 at 05:36
  • nvm, i have fixed the issue –  Jul 23 '17 at 09:28