java - opcion - cuadro de lista de selección múltiple excel
botón de opción mostrar valores en cuadro combinado (1)
Confundo con este if-else porque soy nuevo en Java y MySQL y traté de hacerlo por mí mismo.
public Menu() {
initComponents();
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/restaurant", "root", "");
System.out.println("ODBC Connection Successful");
showCategory();
} catch (ClassNotFoundException | SQLException e) {
System.out.println("ODBC Connection Failed" + e);
}
}
si - else
private void showCategory() {
try {
Statement stmt;
stmt = con.createStatement();
if (rbMFood.isSelected()) {
ResultSet rs = stmt.executeQuery("SELECT * FROM menu_cat WHERE type_id = ''TY02''");
while (rs.next()) {
cmbMCat.addItem(rs.getString("menu_cat"));
}
} else {
ResultSet rs = stmt.executeQuery("SELECT * FROM menu_cat WHERE type_id = ''TY01''");
while (rs.next()) {
cmbMCat.addItem(rs.getString("menu_cat"));
}
}
} catch (Exception e) {
}
}
Boton de radio
private void rbMFoodActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
type = "Food";
}
private void rbMDrinkActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
type = "Drink";
}
Y declaro la función al final del programa
private String type;
Cuando hago clic en bebida / comida, la categoría sigue siendo bebida.
La base de datos
¡Cualquier ayuda sería muy apreciada!
Está utilizando rs.getString("menu_cat")
Pero el formato es rs.getString(<Column Name>)
Pero está utilizando rs.getString(<Table Name>)
Como "menu_cat" es el nombre de la tabla y no el nombre de la columna
DESPUÉS DE PUBLICAR EL CONSTRUCTOR
Lo que veo del código que ha publicado, es que ha llamado a showCategory()
en el constructor. Este método es responsable de completar JComboBox
cmbMCat
. Ahora se está cmbMCat
el cmbMCat
cuando está creando el nuevo Menu
. Después de eso, la lista de elementos de cmbMCat
no cambia.
Entonces, lo que sugiero es que llame a showCategory()
desde los métodos rbMFoodActionPerformed
y rbMDrinkActionPerformed
. Espero que esto resuelva tu problema.
También agregue cmbMCat.removeAllItems()
antes de Statement stmt;
para eliminar todos los elementos que están en el cmbMCat
y restablecerlo con una nueva lista de elementos.
Después de comentar sobre if-else
Cambie el showCatagory()
siguiente manera:
private void showCategory() {
cmbMCat.removeAllItems();
try {
PreparedStatement stmt; //Used Prepared statement
String sql = "SELECT * FROM menu_cat WHERE type_id = ?";
stmt = con.prepareStatement(sql);
if (type.equals("Drink")) {
stmt.setString("TY01");
} else {
stmt.setString("TY02");
}
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
cmbMCat.addItem(rs.getString("menu_cat"));
}
}
} catch (Exception e) {
}
}
También tenga en cuenta que su código eventListener
debería ser:
private void rbMFoodActionPerformed(java.awt.event.ActionEvent evt){
type = "Food";
showCategory();
}
private void rbMDrinkActionPerformed(java.awt.event.ActionEvent evt){
type = "Drink";
showCategory();
}