-5

条件に基づいてストアド プロシージャをロールバックする必要があるという要件がありました。

最初にストアド プロシージャを呼び出し、後で条件をチェックし、条件が失敗した場合はロールバックする必要があります。以下は私が試したコードです。

public static void main(String[] args) {
    Student student=new Student();

    student.setName("AAAA");
    student.setAge("20");
    student.setDob("14/08/1988");
    student.setPhone("98841");
    student.setSslc("1111");
    student.setHsc("222");
    student.setCollege("333");
    System.out.println(student);
    try {
        Connection conn=ConnectDB.getConnection();
        conn.setAutoCommit(false);

        CallableStatement callableStatement = null;
        String proc = "{call STUDENT_OP(?,?,?,?,?,?,?,?)}";
        callableStatement = conn.prepareCall(proc);
        Savepoint savepoint1 = conn.setSavepoint("ROLLBACK_SP");

        int age=Integer.parseInt(student.getAge());

        callableStatement.setString(1, student.getName());
        callableStatement.setInt(2, age);
        callableStatement.setString(3, student.getDob());
        callableStatement.setString(4, student.getPhone());
        callableStatement.setString(5, student.getSslc());
        callableStatement.setString(6, student.getHsc());
        callableStatement.setString(7, student.getCollege());
        callableStatement.registerOutParameter(8, java.sql.Types.NUMERIC);

        callableStatement.executeUpdate();

        int returnCode=callableStatement.getInt(8);
        getStudents();

        if(SOME CONDITION){
            conn.rollback(savepoint1);
        }
        getStudents();
    } catch (SQLException e) {
        e.printStackTrace();
    }
}

上記のコードでは、getStudents()メソッドは、学生テーブルの名前のリストを出力します。ロールバック前とロールバック後にこのgetStudents()メソッドを実行しています。セーブポイントセーブポイント savepoint1 = conn.setSavepoint("ROLLBACK_SP");として設定しました。このセーブポイントを使用してロールバックしています。

しかし、ロールバックは起こっていません。何か不足していますか?助けてください。

4

1 に答える 1