基本上,数据库中的值被提取并显示在JTable中。如果其中一个字段包含值,我想要一个弹出窗口。例如,我希望如果JTable中的section_name是“Executive Summary”,那么应该会出现一个弹出窗口。但我发现即使section_name不是“执行摘要”,第12行中的if条件也会被执行。这是我所指的if块: -
if (section_name.equals("Executive Summary"));
{
JOptionPane.showMessageDialog(null, "it works", "Error", JOptionPane.ERROR_MESSAGE);
}
每次都会显示弹出窗口。错误在哪里以及如何纠正错误。
ResultSet rs = pst.executeQuery();
int i = 0;
while (rs.next()) {
section_name = rs.getString("Section_Name");
report_name = rs.getString("Report_Name");
contact_name = rs.getString("Contact_Name");
link = rs.getString("Link");
String m="apple";
model.addRow(new Object[]{section_name, report_name, contact_name, link,m});
i++;
}
if (section_name.equals("Executive Summary"));
{
JOptionPane.showMessageDialog(null, "it works", "Error", JOptionPane.ERROR_MESSAGE);
}
if (i < 1)
{
JOptionPane.showMessageDialog(null, "No Record Found", "Error", JOptionPane.ERROR_MESSAGE);
}
if (i == 1)
{
System.out.println(i + " Record Found");
} else {
System.out.println(i + " Records Found");
}
}
catch (Exception ex)
{
JOptionPane.showMessageDialog(null, ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
答案 0 :(得分:6)
您需要删除此行末尾的分号:
if (section_name.equals("Executive Summary"));
成功:
if (section_name.equals("Executive Summary"))
{
....
}
通过留下分号,它就相当于:
if (section_name.equals("Executive Summary")) {}
{
.... /* always executed */
}