我正在使用Spring MVC控制器来处理请求。
DAO的代码
public University findbyId(int id){
try {
Connection con = JDBCConnection.getConnection();
PreparedStatement s=con.prepareStatement(UniversitySQL.FINDBYID);
ResultSet rs;
s.setInt(1, id);
rs=s.executeQuery();
while(rs.next()){
uni=new University(rs.getInt(1),rs.getString(2));
}
} catch (ClassNotFoundException ex) {
Logger.getLogger(UniversityDAO.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(UniversityDAO.class.getName()).log(Level.SEVERE, null, ex);
}
return uni ;
}
Servlet代码
@RequestMapping(value="/university/{id}",method=RequestMethod.GET)
public ResponseEntity<String> getUniversityById(@PathVariable("id") int id) {
University uni = null;
uni = university.findbyId(id);
data.setData(uni);
data.setMessage("Success");
return new ResponseEntity<String>(data.toString(), headers, HttpStatus.OK);
}
问题是如果我输入无效的id
它会返回上一次成功的结果,而不是它应该返回null。