想把stu中的值存到list中,调试到这一步再向下调试直接跳过了list.add(stu);最后return list;的返回值也是空的
public List<Student> findStudents(String studName, Integer pageNo, Integer pageSize){
List<Student> list=new ArrayList<Student>();
try{
conn=SqlHelper.connect();
String sql="Select a.major_name,b.* from xsgl_major a,xsgl_student b where a.major_id=b.major_id and stud_name like ? ";
sql=sql+ " limit "+ (pageNo-1)* pageSize+","+pageSize;
ps=conn.prepareStatement(sql);
ps.setString(1, "%"+studName+"%");
rs=ps.executeQuery();
while(rs.next()){
Student stu=new Student();
Major major=new Major();
stu.setMajor(major);
major.setMajorName(rs.getString(1));
stu.setStudNo(rs.getString(2));
major.setMajorId(rs.getInt(3));
stu.setStudName(rs.getString(4));
stu.setStudSex(rs.getString(5));
stu.setStudBirthDate(rs.getDate(6));
stu.setStudIsMember(rs.getBoolean(7));
stu.setStudAddress(rs.getString(8));
stu.setStudResume(rs.getString(9));
stu.setStudPic(rs.getBytes(10));
list.add(stu);
}
}catch(Exception e){
}finally{
SqlHelper.closeResultSet(rs);
SqlHelper.closePreparedStatement(ps);
SqlHelper.closeConnection(conn);
}
return list;
}
错误在于 rs.getBytes(10)
这个代码,第 10 列数据库字段的类型无法转换为 Java 的 Byte 类型导致报错,从而跳出循环直接 return ,检查下这个列的数据库类型是什么,然后再改成其他方法获取值,可以先拿 SQL 在其他客户端执行确认第 10 列字段是什么。
上面的代码同时也暴露出了几个问题,题主可以加以留意。
select *
,这样可以增加可读性,同时对查询效率可能有一定提升。catch
后一定要打印异常,便于排查问题,例如上面的代码可以改成 e.printStackTrace();
major.setMajorName(rs.getString("a.major_name"));
最后如果有帮助,请帮忙点个采纳。
在catch里面打印一下e.getMessage方法,看看输出的错误信息。
先查看报错信息,然后再决定用什么方法处理
1、原因
1)没有执行到list.add(stu);这一样
2)说明在stu.setStudPic(rs.getBytes(10));,这一行报错
3)在catch(Exception e)抛出异常测试下,写成如下,
catch(Exception e){
throw e;
}