我将combobox与数据库信息一起使用。我想添加相应类别的产品。此类别希望它由组合框选择,并从那里记录在数据库中。
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();
}
这是我的申请表。
谢谢大家的帮助,我希望尽可能地解释
问候
答案 0 :(得分:2)
p1.setcategory(这是我不知道添加什么代码的地方);
您可以将项目逐个绑定到结果集中的组合框。
while (rs1.next()) {
comboBox.addItem(rs1.getString(1));//where 1 is column index for table retrived by query
}
你可以得到这样的价值。
comboBox.getSelectedItem().toString();