Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/395.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 捕获完整性ConstraintViolationException错误_Java_Mysql_Swing_Validation - Fatal编程技术网

Java 捕获完整性ConstraintViolationException错误

Java 捕获完整性ConstraintViolationException错误,java,mysql,swing,validation,Java,Mysql,Swing,Validation,我从控制台收到这个错误消息 com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry 'PROTOCOL AND INTERNATIONAL RELATIONS-S9614275G' for key 'uniqueindex' 在执行这些代码时 public boolean createAssignRequest(AssignmentRequests assignR

我从控制台收到这个错误消息

com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry   'PROTOCOL AND INTERNATIONAL RELATIONS-S9614275G' for key 'uniqueindex'
在执行这些代码时

public boolean createAssignRequest(AssignmentRequests assignReq) {

    int id = assignReq.getReqId();
    String dutyName = assignReq.getDutyName();
    String volNric = assignReq.getVolNric();

    boolean success = false;
    DBController db = new DBController();
    String dbQuery = "";
    Connection conn = null;


    db.getConnection();

    dbQuery = "INSERT into assignrequests (dutyName, volNric)"
            + " VALUES ('" + dutyName + "','" + volNric + "')";

    if (db.updateRequest(dbQuery) == 1) {
        success = true;
    }
    db.terminate();

    return success;

}
如何捕获错误并显示JOptionPane以显示错误消息

我的updateRequest方法代码:

    public int updateRequest(String dbQuery) {
    int count = 0;
    System.out.println("DB Query: " + dbQuery);
    try {
        // create a statement object
        Statement stmt = con.createStatement();
        // execute an SQL query and get the result
        count = stmt.executeUpdate(dbQuery);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return count;
}
试试这个:

public boolean createAssignRequest(AssignmentRequests assignReq) {

    int id = assignReq.getReqId();
    String dutyName = assignReq.getDutyName();
    String volNric = assignReq.getVolNric();

    boolean success = false;
    DBController db = new DBController();
    String dbQuery = "";
    Connection conn = null;

    ResultSet resultSet = null;

    db.getConnection();

    dbQuery = "INSERT into assignrequests (dutyName, volNric)"
            + " VALUES ('" + dutyName + "','" + volNric + "')";


    resultSet = db.updateRequest(dbQuery) == 1;

    try {
        if (rs.next()) {
            success = true;
        }
    }
    catch (Exception e)
    {
        // Error handling here.
    }

    db.terminate();

    return success;

}

在try..catch块中包围导致异常的代码(可能是块开始的部分
if(db.updateRequest(dbQuery)==1){
。当我尝试这样做时,它会出现一个错误,无法访问catch块!-(rs与resultSet是相同的变量吗?我得到错误“无法将布尔值转换为结果集”其中resultSet=db.updateRequest(dbQuery)==1;