【问题标题】:creating System.out.println for MySQLIntegrityConstraintViolationException: Duplicate entry for key 'PRIMARY'为 MySQLIntegrityConstraintViolationException 创建 System.out.println:键“PRIMARY”的重复条目
【发布时间】:2016-07-06 07:09:49
【问题描述】:

我有一段代码,用户可以在其中选择学生 ID 和班级 ID,然后将该学生注册到班级中。我正在尝试拥有它,因此如果学生尝试注册他们已经参加的课程,则会出现错误消息。我收到 CVE 错误,提示重复输入,这是我想要的,但我怎样才能打印出来这个错误给用户?我尝试捕获 CVE 错误,但一定是出错了。

public static void addClass() {

    try {
        rs = myStmt.executeQuery("SELECT * FROM ClassSelector.classes");
        while (rs.next()) {
            String availableClasses = rs.getString("class_id") + "\t" + rs.getString("class_name") + "\t" + rs.getString("description");
            System.out.println(availableClasses);
        }
        System.out.println("Enter Class ID from Classes Listed Above to Join: ");
        selectedClass = sc.nextLine();
        rs = myStmt.executeQuery("SELECT * FROM ClassSelector.classes WHERE class_id = " + selectedClass);
        while (rs.next()) {
            String innerJoin = (userEnterIdAsName + " has been added to " + rs.getString("class_name") + " " + rs.getString("class_id"));
            System.out.println(innerJoin);
            String student_x_classJoin = "INSERT INTO student_x_class" + "(student_id, student_name, class_id, class_name)" + "VALUES (?, ?, ?, ?)";
            PreparedStatement pStmt = con.prepareStatement(student_x_classJoin);
                pStmt.setString(1, user_entered_student_id);
                pStmt.setString(2, userEnterIdAsName);
                pStmt.setString(3, rs.getString("class_id"));
                pStmt.setString(4, rs.getString("class_name"));
                pStmt.executeUpdate();
                System.out.println("Would you like to enroll " + userEnterIdAsName + " into another class? (Y/N)");
                String addAdditionalClass = sc.nextLine();
                if (addAdditionalClass.equalsIgnoreCase("Y")) {
                    addClass();
                } else if (addAdditionalClass.equalsIgnoreCase("N")) {
                    return;
                }
            }
    }
    catch (java.sql.SQLException SQL) {
        SQL.printStackTrace();
    }
}

错误来自这一行:

字符串 student_x_classJoin

谢谢

这是错误:

com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '33-315' for key 'PRIMARY'
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
    at com.mysql.jdbc.Util.getInstance(Util.java:387)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:932)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3878)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3814)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2478)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2625)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2551)
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1861)
    at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2073)
    at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2009)
    at com.mysql.jdbc.PreparedStatement.executeLargeUpdate(PreparedStatement.java:5094)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1994)
    at ClassSelectorApp.addClass(ClassSelectorApp.java:168)
    at ClassSelectorApp.signUp(ClassSelectorApp.java:122)
    at ClassSelectorApp.main(ClassSelectorApp.java:29)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

【问题讨论】:

  • 是的,问题出在第 423 行,我的水晶球说。如果您不信任水晶球,请发布您的代码。就是说,插入前为什么不检查一下?
  • 好的,那么捕获异常并向用户显示错误消息的catch块在哪里?
  • 那是我想不通的。我尝试添加 catch (MySQLIntegrityConstraintViolationException e) { e.printStackTrace(); } 在另一个 catch 之前,但它没有识别 MySQLIntegrityConstraintViolationException...
  • 也许 MySQL 没有抛出异常 stackoverflow.com/questions/21904707/…
  • 复制您从 printStackTrace 获得的消息,也许我们可以指出您应该捕获的异常

标签: java mysql


【解决方案1】:

你可以在你的句子后面使用另一个 try-catch

     pStmt.executeUpdate();

就像Catching the wrong exception中解释的那样

在堆栈跟踪后编辑

    try {
        pstmt.executeUpdate();
    } catch (MySQLIntegrityConstraintViolationException ex) {
          System.out.println("Duplicated entry"); // or whatever message you want
          throw new SQLException("Duplicated entry"); // "jump" to external catch
    } 

【讨论】:

  • 嗨,感谢您的帮助,但这似乎不起作用
  • 我在阅读您的更新后更改了答案。我无法理解为什么 MySQL 生成的异常没有被捕获。它应该在您的原始捕获中捕获(它从 SQLException docjar.com/docs/api/com/mysql/jdbc/exceptions/… 扩展)或我建议的捕获中。
猜你喜欢
  • 2014-05-20
  • 2019-07-03
  • 2019-02-15
  • 2016-08-04
  • 2017-02-17
  • 2012-07-23
  • 2013-09-30
  • 2015-09-13
相关资源
最近更新 更多