英文:
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 "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 inn an uneditable calculation' );
END IF;
COMMIT;
END;
This is the dao impl
@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());
}
}
This is the react front button
<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>
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) {
...
}
}
}
专注分享java语言的经验与见解,让所有开发者获益!
评论