【发布时间】:2018-12-02 01:09:29
【问题描述】:
我很好奇我应该如何使用 springs jdbctemplate 类来确定我的一个表中是否已经存在记录或行?我试过了
int count = jdbcTemplate.queryForObject("select * from MyTable
where Param = ?", new Object[] {myParam},
Integer.class);
if(count ==0)
//record does not exist
问题是虽然我不断收到EmptyResultAccessDataException 的任何一个,但当它不存在时,我将代码更新为
try{
jdbcTemplate.queryForObject("select * from MyTable
where Param = ?", new Object[] {myParam},
Integer.class);
} catch(EmptyResultAccessDataException e) {//insert the record}
如果记录确实存在,这会给我带来问题。所以我想我真正的问题是在表中搜索记录存在的最佳方法是什么,因为如果没有,我想添加所述记录,如果有,则什么也不做。
【问题讨论】:
标签: spring jdbc jdbctemplate