strange_098
strange_098

Reputation: 1371

How to save information from a combobox in the database

I use combobox with database information. I want to add a product of the respective category. This category want it to be selected by a combobox and it is recorded in the database from there.

  public void addProducts() {
      try {
        Products p1 = new Products();
        p1.setIdProduct(jTIdProduct.getText());
        p1.setDescProduct(jTDescProduct.getText());
        p1.setStockActual(jTStockA.getText());
        p1.setStockMin(jTStockM.getText());
        p1.setPrice(jTPrice.getText());
        p1.setNumOrc(jTNOrc.getText());

    -------->     p1.setcategory( THIS IS WHERE i DON'T KNOW WHAT CODE ADD);


        ProductDao dao = new ProductDao();
        dao.addProduct(p1);

    } catch (SQLException ex) {
        Logger.getLogger(jTProduct.class.getName()).log(Level.SEVERE, null, ex);
    }
}

----------------------CLASS DAO-------------------------------------------

  public void addProducts(Products p1) throws SQLException {
    String sql = "insert into Products (idProduct, descProduct, stockActual, stockMin, price, numOrc, category)" + "values (?,?,?,?,?,?,?)";



    PreparedStatement stmt = conexao.prepareStatement(sql);
    stmt.setString(1, p1.idProduct());
    stmt.setString(2, p1.getDescProduct());
    stmt.setString(3, p1.getStockActual());
    stmt.setString(4, p1.getStockMin());
    stmt.setString(5, p1.getPrice());
    stmt.setString(6, p1.getNumOrc());
    stmt.setString(7, p1.getCategory());

    stmt.execute();
    stmt.close();
    conexao.close();

}

enter image description here

This is form from my apllication.

Thank you all for the help, I hope to explain as best as possible

Greetings

Upvotes: 1

Views: 8665

Answers (1)

Akash Thakare
Akash Thakare

Reputation: 22972

p1.setcategory( THIS IS WHERE i DON'T KNOW WHAT CODE ADD);

You can bind items one by one to combobox from result set.

while (rs1.next()) {
 comboBox.addItem(rs1.getString(1));//where 1 is column index for table retrived by query
}

You can get Value like this.

comboBox.getSelectedItem().toString();

Upvotes: 2

Related Questions