【问题标题】:Insert Button Not working after verifying duplicate entry验证重复输入后插入按钮不起作用
【发布时间】:2018-09-18 09:45:48
【问题描述】:

嗨,我是初学者,我正在尝试以某种方式创建预订系统,我现在可以检查是否有重复条目,但是如果没有重复条目,它不会将数据插入数据库,但如果我删除了检查功能复制它可以将数据插入数据库。有什么建议吗?我正在尝试解决它,但它仍然无法正常工作。没有错误但没有添加。

  private void btnreserveActionPerformed(java.awt.event.ActionEvent evt) {                                           
     try {
        DB_Operation DB =new DB_Operation();
        String cx1,contactnum11,email1,t11,venuee;
        String amount1=aval.getText();
        String bal1=b1.getText();
        cx1=cx.getText();
        contactnum11=contactnum1.getText();
        email1=email.getText();
        t11=t1.getSelectedItem().toString();
        venuee=v.getSelectedItem().toString();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String Dateee = sdf.format(dad.getSelectedDate().getTime());
        boolean unique;
        unique = true;

        String c1="8am to 12 pm";
        String c3="8am to 5pm";
        String c2="1pm to 5 pm";
        ResultSet rsa= DB.searchQuery("SELECT * FROM `reserve1` WHERE `Date`='"+Dateee+"'");
        if ( cx.getText().trim().length() == 0||  contactnum1.getText().trim().length() == 0||email.getText().trim().length() == 0){
            final JPanel panel = new JPanel();
            JOptionPane.showMessageDialog(panel, "All fields must be completed", "Error", JOptionPane.ERROR_MESSAGE);

        }
        else if(rsa.isBeforeFirst()){
            while(rsa.next()){


                if(rsa.getString("Venue").equalsIgnoreCase(venuee)&&
                        rsa.getString("Date").equalsIgnoreCase(Dateee)&& rsa.getString("Time").equals(t11)){
                    JOptionPane.showMessageDialog(null,"Duplicate Resevation!");
                    unique = false;
                }
                else if((t11.equals(c3))&&(rsa.getString("Venue").equalsIgnoreCase(venuee)&&
                        rsa.getString("Date").equalsIgnoreCase(Dateee)&& rsa.getString("Time").equals(c1))){
                    JOptionPane.showMessageDialog(null,"Half Day Resevation Exist!");
                    unique = false;
                }
                 else if((t11.equals(c3))&&(rsa.getString("Venue").equalsIgnoreCase(venuee)&&
                        rsa.getString("Date").equalsIgnoreCase(Dateee)&& rsa.getString("Time").equals(c2))){
                       JOptionPane.showMessageDialog(null,"Half Day Resevation Exist!");
                    unique = false;
                }
                else if(unique=true){
                    try
 {

     String url="jdbc:mysql://localhost:3306/reservation?zeroDateTimeBehavior=convertToNull";
    String userdb="root";
    String passdb="";  

    Class.forName("com.mysql.jdbc.Driver");
      Connection conn=DriverManager.getConnection(url,userdb,passdb);

     Statement stmt=(Statement)conn.createStatement();

      stmt.executeUpdate ("INSERT INTO `reserve1`( `Customer Name`, `Contact Number`, `Email Address`, `Venue`, `Date`, `Time`, `Price`, `Balance`) "+ "VALUES ("+"\""+cx1+"\""+","+"\""+contactnum11+"\""+","+"\""+email1+"\""+","+"\""+venuee+"\""+","+"\""+Date1+"\""+","+"\""+t11+"\""+","+"\""+amount1+"\""+","+"\""+bal1+"\""+")");
    JOptionPane.showMessageDialog(null,"Data Added!");


 }
 catch(        HeadlessException | ClassNotFoundException | SQLException e)
 {
          JOptionPane.showMessageDialog(null, e.getMessage() ,"Error", 1);


 }


                }
            }
        }
      }    catch (SQLException ex) {
        Logger.getLogger(reservation.class.getName()).log(Level.SEVERE, 
     null, ex);
      }

【问题讨论】:

  • 能否为您的查询提供结果集:SELECT * FROM reserve1 WHERE Date='"+Dateee+"'" 以及场地e、Dateee 和 t11 的值
  • 它会检查所选日期是否在数据库中,以及预订的场地和时间,如果输入的数据与数据库中的日期、场地、时间相同,则会显示“重复预订”消息
  • 我认为您将通过 GUI 提供这些值。请显示您传递的值和查询结果。

标签: java sql netbeans wamp


【解决方案1】:

我认为您应该将 if(unique=true) 作为单独的 if 语句,而不是作为 else if 阶梯的一部分。

【讨论】:

  • 非常感谢您的帮助。我确实通过在其他方法之前放置一些 }} 解决了它,它起作用了:) 再次感谢您
猜你喜欢
  • 2018-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-16
  • 2014-11-29
  • 2014-02-07
  • 1970-01-01
相关资源
最近更新 更多