单选按钮在组合框中显示值 [英] radio button show values in combo box
问题描述
我对这个if-else感到困惑,因为我是Java和Java的新手。 MySQL和我试图自己制作。
I confused with this if-else because I'm new in Java & MySQL and I tried to make it by myself.
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);
}
}
if - else
if - 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) {
}
}
单选按钮
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";
}
我在程序的最后声明了这个函数
And I declare the function in the end of the program
private String type;
当我点击饮料/食物时,该类别仍属于饮品类别。
When I click drink / food, the category still drink's category.
数据库
非常感谢任何帮助!
推荐答案
您正在使用 rs.getString(menu_cat)
但格式为 rs.getString(< Column Name>)
但你是使用 rs.getString(< Table Name>)
因为menu_cat是表的名称而不是列的名称。
You are using rs.getString("menu_cat")
But the format is rs.getString(<Column Name>)
But you are using rs.getString(<Table Name>)
As "menu_cat" is name of the table and not the name of the column.
张贴构造函数
我从您发布的代码中看到的是,您已拨打构造函数中的showCategory()
。此方法负责填充 JComboBox
cmbMCat
。现在,当您创建新的菜单
时,正在填充 cmbMCat
。之后 cmbMCat
的项目列表不会改变。
What I see from the code you have posted, is that You have called showCategory()
in the constructor. This method is responsible for populating the JComboBox
cmbMCat
. Now the cmbMCat
is being populated when you are creating the new Menu
. After that the items list of the cmbMCat
does not change.
所以,我建议你打电话给来自 rbMFoodActionPerformed
和 rbMDrinkActionPerformed
方法的 showCategory()
。我希望这可以解决您的问题。
So, What I suggest is that you call the showCategory()
from the rbMFoodActionPerformed
and rbMDrinkActionPerformed
methods. I hope this will solve your problem.
还要在之前添加
删除 cmbMCat.removeAllItems()
Statement stmt; cmbMCat
中的所有项目,并使用新的项目列表重置它。
Also add cmbMCat.removeAllItems()
before Statement stmt;
to remove all the items that are there in the cmbMCat
and reset it with a fresh list of items.
关于if-else的评论
更改 showCatagory()
如下所示:
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) {
}
}
另请注意, eventListener
代码应为:
private void rbMFoodActionPerformed(java.awt.event.ActionEvent evt){
type = "Food";
showCategory();
}
private void rbMDrinkActionPerformed(java.awt.event.ActionEvent evt){
type = "Drink";
showCategory();
}
这篇关于单选按钮在组合框中显示值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!