0

I would like to be able to select a data from database and display it using the comboBox.

I have the following code but it does not display the data in the comboBox. I do realize there is codes missing to display the data and my SQL statement is not correct. I am just not sure what I can do any advise is appreciated.

try { Statement st = db.con.createStatement(); ResultSet rs = st.executeQuery("SELECT Name, Size, Price FROM item WHERE Name=" + comboBox_1.getToolkit());

                JOptionPane.showMessageDialog(frame, "displayed");

                while (rs.next()) {
                    String name  = rs.getString("Name");
                    String size  = rs.getString("size");
                    String price  = rs.getString("price");
                    textArea_Name.append(name);
                    textArea_size.append(size);
                    textArea_price.append(price);
                    comboBox_1.addItem(rs.getString("Name"));
                    comboBox_1.getSelectedItem();

                }}              


                 catch (SQLException e ) {
                    System.out.println("user not added");
                    e.printStackTrace();
                 }              

            }
        });
2
  • 2
    You really need to add more information. What have you got so far? Commented Feb 17, 2012 at 16:31
  • An example using JPA is shown here. Commented Dec 30, 2015 at 23:11

5 Answers 5

2
try {  
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");  

Connection conn= DriverManager.getConnection("jdbc:odbc:driverName");  
Statement st = conn.createStatement();

 ResultSet r=st.executeQuery("select * from tableName");

 while (r.next()) {  

     JComboBox.addItem(r.getString("Key_Coloumn_Name"));  
 }


    conn.close();
    } catch (Exception e) {  
JOptionPane.showMessageDialog(null,"Failed to Connect to Database","Error Connection", JOptionPane.WARNING_MESSAGE);  
System.exit(0);  
}  
Sign up to request clarification or add additional context in comments.

Comments

1

Try this tutorial. It does exactly that.

http://www.roseindia.net/tutorial/java/swing/comboboxwithdatabaseValues.html

1 Comment

...but afterwards, never visit roseindia ever again. ever.
1
        try {
             Statement stmt = db.con.createStatement();
        ResultSet rs = stmt5.executeQuery("select * from tbl_your_table");
        while (rs.next()) {
            String pat = rs.getString("name");
            jComboBox.addItem(pat);
        }

    } catch (Exception e) {

        JOptionPane.showMessageDialog(null, e);
    }

Comments

0
    con = DriverManager.getConnection("jdbc:mysql://:3306/database","user","password");
    stat = con.createStatement();
    ResultSet rs = stat.executeQuery("select name from student;");
    while(rs.next()){
        jComboBox1.addItem(rs.getString("name"));
        }
    rs.close();
    stat.close();
    con.close();

Comments

0
comb = new JComboBox();
  DefaultComboBoxModel dftm = (DefaultComboBoxModel) comb.getModel();
    ArrayList<stock_data> list = userLists();
     row = new Object[1];
    for (int i = 0; i < list.size(); i++) {
        row[0] = list.get(i).get_item();
        dftm.addElement(row);
        // comb.setModel(dftm);
    }

Then create your method and also remember you need a stock_data Class

private ArrayList<stock_data> userLists() {
    try {

        connection = localhost.dbConnector();
        ArrayList<stock_data> usersList = new ArrayList();
        try {
            String query = "Select * from itemlist";
            Statement st = connection.createStatement();
            ResultSet rs = st.executeQuery(query);
            stock_data users;
            while (rs.next()) {
                // users = new stock_data(rs.getString("name"));
                // usersList.add(users);
                comb.addItem(rs.getString("name"));

            }
            rs.close();
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "blah blah");
            e.printStackTrace();
        }
        return usersList;
    } catch (Exception e1) {
        e1.printStackTrace();
    }
    return null;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.