MySQL Record不保存

时间:2017-04-15 22:12:26

标签: java mysql

我在将记录插入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);
}
}

2 个答案:

答案 0 :(得分:0)

connection.commit;此声明之后尝试preparedStatement.executeUpdate();。可能是自动提交是假的。

答案 1 :(得分:0)

之前我遇到过这样的问题。因此,假设您的表具有基于整数的主键,应将其设置为自动递增,则插入记录的失败可能是由于声明:

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的最新副本。