1、查询表中一条数据。
public Admin findById(String id) throws Exception {
String sql = "select adminid,note,adminflag from admin where adminid=?";
Admin admin = (Admin) this.jdbc.queryForObject(sql,
new Object[] { id }, new int[] { Types.VARCHAR },
new RowMapper(){
public Object mapRow(ResultSet rs,int index)throws SQLException{
Admin a = new Admin();
a.setAdminid(rs.getString("adminid"));
a.setNote(rs.getString("note"));
a.setAdminflag(rs.getInt("adminflag"));
return a;
}
});
return admin;
2、在插入一条新数据时,判断这条数据是否存在。
public boolean doCreate(Admin vo) throws Exception {
boolean flag = false;
[color=red]if(this.findById(vo.getAdminid()) == null){[/color]
String sql = "insert into admin(adminid,password,note,adminflag)values(?,?,?,?)";
if (this.jdbc.update(sql, new Object[] { vo.getAdminid(),
vo.getPassword(), vo.getNote(), vo.getAdminflag() },
new int[] { Types.VARCHAR, Types.VARCHAR, Types.VARCHAR,
Types.INTEGER }) > 0) {
flag = true;
}
}
return flag;
}
异常:
org.springframework.dao.EmptyResultDataAccessException: Incorrect result size: expected 1, actual 0
我是这么判断的,结果老报错,findById()可以把数据查询出来,而页面也有显示,就是判断这里,出错!