I am using Spring MVC controller to handle requests.

code for 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 ;
    }

Code for Servlet

@RequestMapping(value="/university/{id}",method=RequestMethod.GET)
public ResponseEntity<String> getUniversityById(@PathVariable("id") int id) {
    setHeaders();
    University uni = null;
    uni = university.findbyId(id);
    data.clear();
    data.setData(uni);
    data.setMessage("Success");
    return new ResponseEntity<String>(data.toString(), headers, HttpStatus.OK);
}

The problem is if I enter an invalid id it returns the last successful result instead it should return null.

0 Answers