【问题标题】:MySQL Record does not saveMySQL 记录不保存
【发布时间】:2017-09-11 21:48:16
【问题描述】:

我在将记录插入 mySQL 数据库时遇到问题,查询根本没有被执行,我不明白为什么它在捕获异常时不打印任何错误。请问有什么想法吗? 我已经多次检查拼写和表格本身,连接也有效,因为它在我的登录窗口中使用,该窗口出现在下面显示的窗口之前。

public class RegisterStudent implements Initializable{

@FXML
TextField txtID;
@FXML
TextField txtFirstName;
@FXML
TextField txtSecondName;
@FXML
TextField txtGender;
@FXML
TextField txtDOB;
@FXML
TextField txtAddress;
@FXML
TextField txtPostCode;
@FXML
TextField txtMobileNumber;
@FXML
Button btnRegister;
@FXML
Button btnClear;

Connection connection = null;
PreparedStatement preparedStatement = null;

// Set Clear Button.
@FXML
private void setBtnClear() {
    txtID.clear();
    txtFirstName.clear();
    txtSecondName.clear();
    txtGender.clear();
    txtDOB.clear();
    txtAddress.clear();
    txtPostCode.clear();
    txtMobileNumber.clear();
}
// Set Register Button - Saves student record to database.
@FXML
private void setBtnRegister(ActionEvent event) {
    try {
        if(txtFirstName.getText().trim().isEmpty() || 
        txtSecondName.getText().trim().isEmpty() || 
        txtGender.getText().trim().isEmpty() || 
        txtDOB.getText().trim().isEmpty() || txtAddress.getText().trim().isEmpty()
                || txtPostCode.getText().trim().isEmpty() || txtMobileNumber.getText().trim().isEmpty()) {
            DBUtilities.showErrorMsg("Error:", "All fields must be populated!");
        }else {
            connection = DBUtilities.getConnection();
            String SQLQuery = "INSERT INTO student_details ('First_Name', 'Second_Name', 'Gender', 'Date_Of_Birth', 'Address', 'Post_Code', 'Mobile_Number')" + " VALUES (?, ?, ? ,? ,? ,? ,?)";
            preparedStatement = connection.prepareStatement(SQLQuery);
            preparedStatement.setString(1, txtFirstName.getText().trim());
            preparedStatement.setString(2, txtSecondName.getText().trim());
            preparedStatement.setString(3, txtGender.getText().trim());
            preparedStatement.setString(4, txtDOB.getText().trim());
            preparedStatement.setString(5, txtAddress.getText().trim());
            preparedStatement.setString(6, txtPostCode.getText().trim());
            preparedStatement.setString(7, 
            txtMobileNumber.getText().trim());
            preparedStatement.executeUpdate();
            DBUtilities.showInforMsg("Record Saved", "Record has been saved 
    successfully!");
        }
    }catch (Exception exception) {
        //DBUtilities.showErrorMsg("Error:", "Record could not be saved!");
        exception.printStackTrace();
    }finally {
        DBUtilities.closePreparedStatement(preparedStatement);
        DBUtilities.closeConnection(connection);
    }
}
@Override
public void initialize(URL location, ResourceBundle resources) {
    btnRegister.setOnAction(this::setBtnRegister);
}
}

【问题讨论】:

  • 你当然必须至少给出某种错误信息。如果没有别的,在 catch 块中放置一个断点,并手动检查异常类型和异常消息。
  • 我去掉了括号,什么也没有。断点是什么意思?
  • 您应该捕获 SQLException 而不是 Exception 才能准确找出问题所在。在Difference between Exception and SQLException 上查看答案。

标签: java mysql


【解决方案1】:

试试,connection.commit; 在preparedStatement.executeUpdate(); 这个语句之后。可能是自动提交是假的。

【讨论】:

  • 该表的 ID 列设置为自动递增,这可能是问题吗?
  • 尝试升级驱动看看是否有帮助!
【解决方案2】:

我以前也遇到过这样的问题。所以假设你的表有一个基于整数的主键,应该设置为自增,那么插入记录失败可能是由于语句:

preparedStatement = connection.prepareStatement(SQLQuery);

尝试将其替换为:

preparedStatement = connection.prepareStatement(query, preparedStatement.RETURN_GENERATED_KEYS);

当您的代码执行插入时,测试 MySQL 生成的密钥:

 try {
     int noRows = ps.executeUpdate();            
     if (noRows == 1) {
         ResultSet rs = ps.getGeneratedKeys();
         if (rs.next()) {

             // Get the newly generated primary key from MySQL.            
             int id = (int) keyResultSet.getInt(1);  

             // Call setter method for primary key's attribute in Java student_details object.
         }           
     }
 }
 catch(SQLException ex) {                  
     ex.printstackTrace();
 }

这已经解决了我过去的问题。并且,请确保您在项目的构建路径或文件夹中有 MySQLConnector-X.X.X.jar 的最新副本。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多