【问题标题】:UCanAccess ConstraintViolationException not being caughtUCanAccess ConstraintViolationException 未被捕获
【发布时间】:2021-04-16 10:21:28
【问题描述】:

我在 Microsoft Access 数据库中的三个字段上创建了一个复合索引。如果我尝试手动添加违反索引的行,则索引有效。它也适用于 Javafx 程序,因为它会导致此异常: UCAExc:::4.0.2 完整性约束违规:唯一约束或索引违规; CLASS_CHECKROOM 表:CLASS

我试图在应用程序不崩溃的情况下捕获此异常,但无济于事。 我试过了:

}catch (SQLException ex) {
        if(ex.getMessage().contains("integrity")) {

            Alert alertUpdateOK = new Alert(AlertType.INFORMATION);
            alertUpdateOK.setTitle("Double Booked");
            alertUpdateOK.setContentText(ex.getMessage());
            alertUpdateOK.show();
       } 
   }

这会输出“No error”然后发生异常 和:

   }catch (SQLException ex) {
        
            Alert alertUpdateOK = new Alert(AlertType.INFORMATION);
            alertUpdateOK.setTitle("Double Booked");
            alertUpdateOK.setContentText(ex.getMessage());
            alertUpdateOK.show();
       } 
   }

这只会导致没有警报的异常并且应用程序崩溃

我见过一些涉及异常错误代码的解决方案 据我了解,Ucanaccess 使用 HSQLDB,我尝试过错误代码 25 58 和 104

}catch (SQLException ex) {
            if (ex.getSQLState().startsWith("25")) {        
                    
            Alert alertUpdateOK = new Alert(AlertType.INFORMATION);
            alertUpdateOK.setTitle("Double Booked");
            alertUpdateOK.setContentText(ex.getMessage());
            alertUpdateOK.show();
            }
        }

“没有错误” PID重复。输入不同的 PID

我们将不胜感激。整个代码是

public void insertIntoDB() throws SQLException{
        Connection connection =null;
        PreparedStatement preparedStatement = null;
        
        
        if (Integer.parseInt(start) < Integer.parseInt(finish)) {
        try {
            connection =AccessConnect.Connector();
            String sql = "INSERT INTO Class(ClassName, WeekDay, ClassStart, ClassFinish, TeacherID, classAge, MaxSize, RoomID, subjectID) VALUES (?,?,?,?,?,?,?,?,?)";
            preparedStatement = connection.prepareStatement(sql);
            
            
            preparedStatement.setString(1, classname);
            preparedStatement.setString(2, weekday);
            preparedStatement.setString(3, start);
            preparedStatement.setString(4,finish);
            preparedStatement.setInt(5, teacherid);
            preparedStatement.setInt(6, classage);
            preparedStatement.setInt(7, maxsize);
            preparedStatement.setInt(8, roomid);
            preparedStatement.setInt(9,subjectid);
            
            preparedStatement.executeUpdate();
            
        }catch (SQLException ex) {
            
            Alert alertUpdateOK = new Alert(AlertType.INFORMATION);
            alertUpdateOK.setTitle("Double Booked");
            alertUpdateOK.setContentText(ex.getMessage());
            alertUpdateOK.show();
       } 
   
        
        finally {
            if (preparedStatement != null)
                preparedStatement.close();
            if (connection !=null)
                
                connection.close();
        }
}
        
        else {
            Alert alertUpdateOK = new Alert(AlertType.INFORMATION);
            alertUpdateOK.setTitle("Times are wrong");
            alertUpdateOK.setContentText(" The start time has to be before the end time");
            alertUpdateOK.show();
        }
    }

【问题讨论】:

    标签: ucanaccess


    【解决方案1】:

    我能够在警报框中捕获异常。我正在使用eclipse,我需要 清理项目,它工作了

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-09
      • 2017-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多