0

In servlet I am trying to put a list in json object, but i cant able to find what is the error. I am calling servlet from ajax call, Here is servlet code,

Latlng latlng=new Latlng();
List<Latlng> vehicleList = new ArrayList<Latlng>();

sql ="SELECT a.vehicleno,a.lat,a.lng,a.status,a.rdate,a.rtime from latlng a,vehicle_details b where a.vehicleno=b.vehicleno and b.clientid="+clientid +"  and b.groupid in(select groupid from group_details where groupname='"+gname+"' and clientid='"+clientid+"')";
              resultSet = statement.executeQuery(sql);
              while(resultSet.next()){
                  String s=resultSet.getString("vehicleno");
                  latlng.setVehicleno(resultSet.getString("vehicleno"));
                  latlng.setLat(resultSet.getString("lat"));
                  latlng.setLat(resultSet.getString("lng"));
                  latlng.setLat(resultSet.getString("status"));
                  latlng.setLat(resultSet.getString("rdate"));
                  latlng.setLat(resultSet.getString("rtime"));
                  vehicleList.add(latlng);
                  System.out.println(vehicleList);
                  String json = new Gson().toJson(vehicleList);
                  response.setContentType("application/json"); 
                  response.setCharacterEncoding("UTF-8"); 
                  response.getWriter().write(json);
              }
     } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

is there anything wrong in it. It is not even displaying exception also.

3
  • Have you tried setting a breakpoint in this code and stepping through it to see what happens? Currently it will never display an exception because you're basically swallowing any ClassNotFoundExceptions or SQLExceptions that occur (writing the exceptions to the console which isn't any help for servlet code). Also, you should probably put the last four lines in your while-loop just after it instead. Commented Aug 21, 2014 at 12:03
  • I tried with break points it execute the stmt and will not go into the while loop just comes to catch but not displaying execption Commented Aug 21, 2014 at 12:07
  • The exception will get printed to your IDE's console/output window. Try looking there, or put a breakpoint right on e.printStackTrace() and then inspect the e variable. Commented Aug 21, 2014 at 12:12

1 Answer 1

1

Try like this.

   while (resultSet.next()) {
        latlng=new Latlng();
        String s = resultSet.getString("vehicleno");
        latlng.setVehicleno(resultSet.getString("vehicleno"));
        latlng.setLat(resultSet.getString("lat"));
        latlng.setLat(resultSet.getString("lng"));
        latlng.setLat(resultSet.getString("status"));
        latlng.setLat(resultSet.getString("rdate"));
        latlng.setLat(resultSet.getString("rtime"));
        vehicleList.add(latlng);
    }
    System.out.println(vehicleList);
    String json = new Gson().toJson(vehicleList);
    response.setContentType("application/json");
    response.setCharacterEncoding("UTF-8");
    response.getWriter().write(json);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.