我正在库存管理中构建Java项目。以下是我用于使用equalsIgnorecase在数据库中插入颜色的代码,但该代码连续显示已经存在。请有人修复我的代码。
谢谢

private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {
        if(txtNewColor.getText().equals(""))
        {
            JOptionPane.showMessageDialog(null, "Fields should not be empty");
        }
        else
        {

            try {

                String c = txtNewColor.getText();
                ps =DbConnection.cn.prepareStatement("Select Color from color_details");
                rs = ps.executeQuery();
                int color = 0;
                while (rs.next())
                {
                    String cl= rs.getString("Color");
                    if(cl.equalsIgnoreCase(cl));

                    {
                        JOptionPane.showMessageDialog(null, "Aready Exist");
                        txtNewColor.setText("");
                        color=1;
                    }
                }

                if (color==0)
                {

                    String strdata="Insert into color_details (Color)values(?)";
                    ps=DbConnection.cn.prepareStatement(strdata);

                    ps.setString(1, txtNewColor.getText());
                    ps.execute();

                    JOptionPane.showMessageDialog(null, "New Color Added Successfully");
                    cleartext();

                }
            }
            catch (Exception e)
            {
                JOptionPane.showMessageDialog(null, e);
            }
        }
        refreshTable();

        }

最佳答案

尝试将if(cl.equalsIgnoreCase(cl));更改为if(c.equalsIgnoreCase(cl))

在if语句的末尾没有发现分号

10-05 19:51