gpt4 book ai didi

java - 验证重复条目后插入按钮不起作用

转载 作者:行者123 更新时间:2023-12-02 11:24:00 25 4
gpt4 key购买 nike

嗨,我是初学者,我正在尝试以某种方式创建一个预订系统,我现在可以检查是否有重复的条目,但是如果没有重复的条目,它不会将数据插入数据库,但如果我删除我的函数来检查复制它可以将数据插入数据库中。有什么建议吗?我试图弄清楚它,但它仍然不起作用。没有错误,但没有添加。

  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);
}

最佳答案

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

关于java - 验证重复条目后插入按钮不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49725400/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com