简体   繁体   中英

How do I create an update button using JComboBox dropdown list in netbeans 8.2?

I wanted to create an update button for a dropdown list that gets the data from the database in the netbeans. Tried this method below but couldn't get it to work. Any suggestions? Thanks alot.

public class ViewProduct extends javax.swing.JFrame {
 ResultSet rs;
 PreparedStatement pst;
 Connection conn;    
 final void FillList(){
 try{   
        //establish connection to table
        String url = "jdbc:derby://localhost:1527/ProductInformation";
        String username = "admin1";
        String password = "admin1";

        Connection conn = DriverManager.getConnection(url,username,password);
        Statement stat = conn.createStatement();
        // get data from table in sql database
        String Query = "SELECT * FROM VIEWPRODUCT";
        ResultSet rs = stat.executeQuery(Query);
        //
        DefaultListModel DLM = new DefaultListModel();

        while(rs.next()){

        JList list = new JList();
        JComboBox ProductID_dropdown = new JComboBox();
        DefaultComboBoxModel listModel = new DefaultComboBoxModel();

        list.setModel(listModel);
        ProductID_dropdown.setModel(listModel);
        }

}catch(SQLException ex){
    JOptionPane.showMessageDialog(null, ex.toString());
}

There are several things not right or missing in your code. For instance you have no GUI.

And as I dont know the exact structure of your project or your database, I have to improvise and give you a pseudo-code approach. You will have to change and configure several things I am showing here.

First, create a JComboBox outside of your method and add it to a container like JPanel:

JPanel pane = new JPanel();
JComboBox productID_dropdown = new JComboBox();
JButton btn_updateViewProducts = new JButton("Update Results");

Here the Button for updating your results is added, it contains an ActionListener , that "listens" to when someone is clicking on the button. Usually you want to retrieve a ResultSet and put its contents into variables, like in this example :

// ActionListener for the button
btn_updateViewProducts.add(new ActionListener{
    @Override
    // When the button is clicked...
    public void actionPerformed(ActionEvent arg0) {
        // ... get the results out of your database (here it's an example database! 
        // Configure for your own one)
        ResultSet rs = stat.executeQuery(Query);
        while(rs.next()){
            // "de-construct" every result of the ResultList into variables
            String query = "select COF_NAME, SUP_ID, PRICE, " + "SALES, TOTAL " + "from " + dbName + ".COFFEES";
            String coffeeName = rs.getString("COF_NAME");
            int supplierID = rs.getInt("SUP_ID");
            float price = rs.getFloat("PRICE");
            int sales = rs.getInt("SALES");
            int total = rs.getInt("TOTAL");
            System.out.println(coffeeName + "\t" + supplierID + "\t" + price + "\t" + sales + "\t" + total);

            // Put items into JComboBox
            productID_dropdown.addItem(coffeeName);
        }        
    }
});

// Add the now filled JComboBox to the pane
pane.add(productID_dropdown);

Other issues you should think about:

  • GUI is missing
  • the method FillList is final, which seems unusual, any reason behind this? final in a method means, that it cant be overridden by subclasses, it this needed here?
  • FillList is a method and should be written with a small letter at the start due to coding convention, the same goes for ProductID_dropdown (coding convention)
  • most of the time it's quite okay to use short variables like rs for a ResultSet , but if your projects get bigger, it gets harder to keep all those variables in mind. Better: make them tell you, what they are. Instead of rs -> resultSetViewProduct or similar.
  • PreparedStatement pst is never used

I hope this helps. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM