FatmaTurk
FatmaTurk

Reputation: 83

java - retrieving data from database and display using JcomboBox

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();
                 }              

            }
        });

Upvotes: 0

Views: 65191

Answers (5)

Temidayo Omotayo
Temidayo Omotayo

Reputation: 1

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;
}

Upvotes: 0

Rahul Sharma
Rahul Sharma

Reputation: 21

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);  
}  

Upvotes: 2

Aseez Liverpool
Aseez Liverpool

Reputation: 31

        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);
    }

Upvotes: 1

user2130173
user2130173

Reputation:

    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();

Upvotes: 0

Sid
Sid

Reputation: 7631

Try this tutorial. It does exactly that.

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

Upvotes: 1

Related Questions