Results 1 to 3 of 3
  1. #1
    joeyxaza is offline Member
    Join Date
    Jan 2009
    Posts
    5
    Rep Power
    0

    Default populate jCombobox with database data

    i would like to populate a jcombobox with data from a database (using two columns, the firstname and lastname). can anyone provide a solution

  2. #2
    markw8500's Avatar
    markw8500 is offline Senior Member
    Join Date
    Jul 2008
    Location
    Pennsylvania, USA
    Posts
    136
    Rep Power
    0

    Default

    After you initiate your connection this should work:

    Java Code:
    public class TestComboBox {
    
        private JComboBox combo;
        private Connection sqlCon;
        private Statement st;
    
        public TestComboBox() {
            initComponents();
    
        }
    
        private void initComponents() {
    
            combo = new JComboBox();
    
    
            try {
                
                st = sqlCon.createStatement();
    
            } catch (SQLException sqle) {
                System.out.println(sqle);
            }
    
        }
    
        public JComboBox getComboBox() {
    
            combo.removeAllItems();
            combo.addItem("Please Select...");
    
            try {
                
                ResultSet rs = st.executeQuery("select firstname, lastname from myTable");
    
                while (rs.next()) {
                    combo.addItem(rs.getString("firstname") + " " + rs.getString("lastname"));
                }
    
    
    
            } catch (SQLException sqle) {
                System.out.println(sqle);
            }
    
    
            return combo;
        }
    }


    If you want tobe really slick you can override the JComboBox class...

    Like so:

    Java Code:
    public class TestComboBox extends JComboBox {
    
        private Connection sqlCon;
        private Statement st;
        
        public TestComboBox() {
            super();
            
            initComponents();
    
        }
    
        private void initComponents() {
    
            try {
    
                
                st = sqlCon.createStatement();
    
                loadCombobox();
    
    
            } catch (SQLException sqle) {
                System.out.println(sqle);
            }
    
        }
    
    
    
        public void loadCombobox(){
    
            this.removeAllItems();
            this.addItem("Please Select...");
    
            try {
    
                ResultSet rs = st.executeQuery("select firstName, lastName from myTable");
    
                while (rs.next()) {
                    this.addItem(rs.getString("firstname") + " " + rs.getString("lastname"));
                }
    
    
    
            } catch (SQLException sqle) {
                System.out.println(sqle);
            }
    
        }
    
    
    }

  3. #3
    Join Date
    Dec 2013
    Location
    Dublin
    Posts
    32
    Rep Power
    0

    Default Re: populate jCombobox with database data

    That is a really good answer. I know its not my question but it really helped me thanks :)

Similar Threads

  1. populate jCombobox with database data
    By joeyxaza in forum JDBC
    Replies: 0
    Last Post: 01-19-2009, 05:30 PM
  2. how to populate data in drop-down box
    By ma-la in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 11-01-2008, 12:24 PM
  3. jComboBox and database
    By pravin2008 in forum AWT / Swing
    Replies: 3
    Last Post: 08-25-2008, 05:08 PM
  4. Replies: 0
    Last Post: 03-11-2008, 05:55 PM
  5. how to populate html:select with values from database
    By sathya_k_83 in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 11-07-2007, 11:53 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •