我遇到这个问题已经3天了,仍然找不到更好的解决方案:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
labelA.setForeground(Color.black);
labelB.setForeground(Color.black);
jLabel3.setForeground(Color.black);
int check=0;
try {
check = search(textUserId.getText(),
textFName.getText());
} catch (SQLException ex) {
// Logger.getLogger(frameRegister.class.getName()).log(Level.SEVERE, null, ex);
} catch (ClassNotFoundException ex) {
//Logger.getLogger(frameRegister.class.getName()).log(Level.SEVERE, null, ex);
}
if(check==1){
try {
if(textPassword.getText() == null ?
"" == null : textPassword.getText().equals("")){
JOptionPane.showMessageDialog(null,
"First Name and Last must contain value","System Message",
JOptionPane.INFORMATION_MESSAGE);
textUserAddress.setText(null);
textUserId.setText(null);
textFName.setText(null);
textLName.setText(null);
textAge.setText(null);
textUBirth.setText(null);
textContactNum.setText(null);
textPassword.setText(null);
textEmailAdd.setText(null);
textMaritalStatus.setText(null);
textUserOccupation.setText(null);
jLabel3.setForeground(Color.red);
}
if((textUserId.getText() == null ? "" == null :
textUserId.getText().equals("")) ||
(textFName.getText() == null ? "" == null :
textFName.getText().equals(""))){
JOptionPane.showMessageDialog(null,
"ID and Name is Required","System Message",
JOptionPane.ERROR_MESSAGE);
labelB.setForeground(Color.red);
labelA.setForeground(Color.red);
}
else{
st.executeUpdate("Insert into info("
+ "id,firstname,lastname,age,dateofbirth,marital_status,address,occupation,contact_no,email_add,"
+ "password" + ") VALUES ('"
+ textUserId.getText() + "','"
+ textFName.getText() + "','"
+ textLName.getText() + "','"
+ textAge.getText() + "','"
+ textUBirth.getText() + "','"
+ textMaritalStatus.getText() + "','"
+ textUserAddress.getText() + "','"
+ textUserOccupation.getText() + "','"
+ textContactNum.getText() + "','"
+ textPassword.getText() + "')");
JOptionPane.showMessageDialog(null, "Record Created",
"System Message",JOptionPane.INFORMATION_MESSAGE);
frameMain.this.setVisible(false);
textUserId.setText("");
frameMain fm= new frameMain();
fm.show();
}
} catch (SQLException ex) {
// Logger.getLogger(frameMain.class.getName()).log(Level.SEVERE, null, ex);
JOptionPane.showMessageDialog(null,
"Record Already Exist","System Message",
JOptionPane.ERROR_MESSAGE);
textUserAddress.setText(null);
textUserId.setText(null);
textFName.setText(null);
textLName.setText(null);
textAge.setText(null);
textUBirth.setText(null);
textContactNum.setText(null);
textLName.setText(null);
textPassword.setText(null);
textMaritalStatus.setText(null);
textUserOccupation.setText(null);
textEmailAdd.setText(null);
}
}
}
我的问题是单击“已保存”按钮后,它会显示“记录已存在”,而实际上它并不存在
我期待我会得到负面和积极的影响,但我希望有人能帮助我
调整后的代码:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
labelA.setForeground(Color.black);
labelB.setForeground(Color.black);
jLabel3.setForeground(Color.black);
int check = 0;
try {
check = search(textUserId.getText(), textFName.getText());
} catch (SQLException ex) {
ex.printStackTrace();
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
if (check == 1) {
try {
// Use .equals for string comparison instead of ==
if (textPassword.getText() == null || textPassword.getText().isEmpty()) {
JOptionPane.showMessageDialog(null, "First Name and Last must contain value", "System Message", JOptionPane.INFORMATION_MESSAGE);
clearTextFields();
jLabel3.setForeground(Color.red);
return; // Exit the method if validation fails
}
if (textUserId.getText() == null || textUserId.getText().isEmpty() || textFName.getText() == null || textFName.getText().isEmpty()) {
JOptionPane.showMessageDialog(null, "ID and Name is Required", "System Message", JOptionPane.ERROR_MESSAGE);
labelB.setForeground(Color.red);
labelA.setForeground(Color.red);
return;
}
// Use PreparedStatement for SQL queries to prevent SQL injection
st.executeUpdate("INSERT INTO info (id, firstname, lastname, age, dateofbirth, marital_status, address, occupation, contact_no, email_add, password) VALUES ('"
+ textUserId.getText() + "', '"
+ textFName.getText() + "', '"
+ textLName.getText() + "', '"
+ textAge.getText() + "', '"
+ textUBirth.getText() + "', '"
+ textMaritalStatus.getText() + "', '"
+ textUserAddress.getText() + "', '"
+ textUserOccupation.getText() + "', '"
+ textContactNum.getText() + "', '"
+ textEmailAdd.getText() + "', '"
+ textPassword.getText() + "')");
JOptionPane.showMessageDialog(null, "Record Created", "System Message", JOptionPane.INFORMATION_MESSAGE);
this.setVisible(false);
clearTextFields();
frameMain fm = new frameMain();
// Use setVisible(true) instead of show() since show() is deprecated.
fm.setVisible(true);
} catch (SQLException ex) {
ex.printStackTrace(); // Log SQL Exception details
JOptionPane.showMessageDialog(null, "Record Already Exist", "System Message", JOptionPane.ERROR_MESSAGE);
clearTextFields();
}
}
}
private void clearTextFields() {
textUserAddress.setText("");
textUserId.setText("");
textFName.setText("");
textLName.setText("");
textAge.setText("");
textUBirth.setText("");
textContactNum.setText("");
textLName.setText("");
textPassword.setText("");
textMaritalStatus.setText("");
textUserOccupation.setText("");
textEmailAdd.setText("");
}