如何从Oracle中捕获自定义抛出的异常到Java中

huangapple 未分类评论39阅读模式
英文:

How to catch custom raised exceptions from oracle to java

问题

以下是翻译好的部分:

我有一个Oracle数据库,其中在表上有一个触发器。如果满足某些条件,它会在删除操作时引发自定义异常。在Java中,我在DAO层中使用NamedParameterJdbcTemplate。结构是经典的控制器-服务-DAO。如何在Java中捕获此异常的文本?提前感谢。

这是我的触发器。

create or replace TRIGGER "ME"."EDITABLE_SETTINGS_DELETE"
BEFORE DELETE ON "ME"."MAP_SETTINGS"
FOR EACH ROW
ENABLE
DECLARE
PRAGMA AUTONOMOUS_TRANSACTION;
CURSOR is_editable_cursor IS
select is_editable
from map_calculation mc 
inner join map_calculation_group mg on mc.id_calc = mg.id_calc
where mg.id_calc = mc.id_calc and mg.id_settings = :old.id_settings;
TYPE is_editable_table IS TABLE OF VARCHAR2(1 CHAR);
v_is_editable is_editable_table;
BEGIN
OPEN is_editable_cursor;
FETCH is_editable_cursor BULK COLLECT INTO v_is_editable;
CLOSE is_editable_cursor;
IF ('F' MEMBER OF v_is_editable) THEN
    RAISE_APPLICATION_ERROR( -20017, 'You cannot delete settings in an uneditable calculation' );
END IF;
COMMIT;
END;

这是DAO实现:

@Override
public void delete(Integer id) {
    SqlParameterSource parameters = new MapSqlParameterSource().addValue("ID_SETTINGS", id);
    try {
        namedParameterJdbcTemplate.update(DELETE_SQL, parameters);
    } catch (Exception e) {
        log.error(e.getMessage());
        throw new RuntimeException("Error during deletion: " + e.getMessage());
    }
}

这是React前端按钮:

<Button
    variant='primary'
    onClick={() => {
        globalProps
            .deleteItem()
            .then(() => {
                table.draw();
            })
            .catch((e) => {
                console.log(e);
                this.setState({ error: e.message });
                this.setState({ showModalBatch: true });
            });
        this.props.toggleShowDelete();
    }}
>
    Удалить
</Button>

我有一个显示消息的模态对话框,显示的消息只是"request failed with status code 500"。

英文:

I have an oracle db which has a trigger on a table. It raises a custom exception on delete if some conditions are met. In Java I use NamedParameterJdbcTemplate in my DAO layer. Structure is classical controller-service-dao. How do I catch the text of this exception in Java? Thanks in advance.
This is my trigger.

create or replace TRIGGER &quot;ME&quot;.&quot;EDITABLE_SETTINGS_DELETE&quot; 
BEFORE DELETE ON &quot;ME&quot;.&quot;MAP_SETTINGS&quot;
FOR EACH ROW
ENABLE
DECLARE
PRAGMA AUTONOMOUS_TRANSACTION;
CURSOR is_editable_cursor IS
select is_editable
from map_calculation mc 
inner join map_calculation_group mg on mc.id_calc = mg.id_calc
where mg.id_calc = mc.id_calc and mg.id_settings = :old.id_settings;
TYPE is_editable_table IS TABLE OF VARCHAR2(1 CHAR);
v_is_editable is_editable_table;
BEGIN
OPEN is_editable_cursor;
FETCH is_editable_cursor BULK COLLECT INTO v_is_editable;
CLOSE is_editable_cursor;
IF (&#39;F&#39; MEMBER OF v_is_editable) THEN
    RAISE_APPLICATION_ERROR( -20017, &#39;You cannot delete settings inn an uneditable calculation&#39; );
END IF;
COMMIT;
END;

This is the dao impl

@Override
	public void delete(Integer id) {
		SqlParameterSource parameters = new MapSqlParameterSource().addValue(&quot;ID_SETTINGS&quot;, id);
		try {
			namedParameterJdbcTemplate.update(DELETE_SQL, parameters);
		} catch (Exception e) {
			log.error(e.getMessage());
			throw new RuntimeException(&quot;Error during deletion: &quot; + e.getMessage());
		}
	}

This is the react front button

                        &lt;Button
                            variant=&#39;primary&#39;
                            onClick={() =&gt; {
                                globalProps
                                    .deleteItem()
                                    .then(() =&gt; {
                                        table.draw();
                                    })
                                    .catch((e) =&gt; {
                                        console.log(e);
                                        this.setState({ error: e.message });
                                        this.setState({ showModalBatch: true });
                                    });
                                this.props.toggleShowDelete();
                            }}
                        &gt;
                            Удалить
                        &lt;/Button&gt;

I have a modal which shows the message and what it shows is just "request failed with status code 500"

答案1

得分: 0

try {
    ...
} catch (DataAccessException ex) {
    if (ex.getRootCause() instanceof SQLException) {
        SQLException sqlEx = (SQLException) ex.getRootCause();
        int errorCode = sqlEx.getErrorCode();
        if (errorCode == -20017) {
            ...
        }
    }
}
英文:
try {
	...
} catch (DataAccessException ex) {
	if (ex.getRootCause() instanceof SQLException) {
		SQLException sqlEx = (SQLException) ex.getRootCause();
		int errorCode = sqlEx.getErrorCode();
		if (errorCode == -20017) {
			...
		}
	}
}

huangapple
  • 本文由 发表于 2020年7月24日 20:28:26
  • 转载请务必保留本文链接:https://java.coder-hub.com/63073580.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定