JdbcTemplate.queryForObject 返回值怎么判断

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;if(this.findById(vo.getAdminid()) == null){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()可以把数据查询出来,而页面也有显示,就是判断这里,出错!
2026年09月22日 23:14
有1个网友回答
网友(1):

我给你加上注释你就明白了啊
public class Main{

public static void main(String[] args) {//无返回值,不需要返回数值
int i;
while (ture){
String intString = JOptionPane.showInputDialog("enter");
i =Integer.parseInt(intString);
if (i==0)
return;//返回时没有任何值,所以是跳出循环
System.out.println("i="+i);
}
}
public ststic viod xMethod(double x,double y ) {//这个无返回值应为是void类型的
System.out.println(x+y);
return x+y;//返回肯定会报错!
}