Returning all rows from a database using json string : so i'm trying to retrieve all the values stored in a database where it matches the sensor name. 

The code runs however, it only outputs the final value in the database.  

    public void doGet(HttpServletRequest request,
               		  HttpServletResponse response)	throws ServletException, IOException {
    
    
    	response.setStatus(HttpServletResponse.SC_OK);
    	String info = request.getParameter("getdata");
    
    	// Do we want info or to enter data on current sensor?
    	// if request for info isn't here, record the current sensor name/value from the parameters
    //	if (info == null){
    		String sensorNameStr = request.getParameter("sensorname");
    		String sensorValueStr = request.getParameter("sensorvalue");
    //		if (!(sensorNameStr==null) && !(sensorValueStr==null)) {
    //			returnMessage = updateSensorTable(sensorNameStr, sensorValueStr);
    //		}
    //		else returnMessage = "bad data";
    //
    //		PrintWriter out = response.getWriter();
    //		System.out.println("DEBUG: Return response for receiving data "+ returnMessage);
    //		out.print(returnMessage);
    //		out.close();
    		
    		String noDataAvailable = "No data for sensor name "+sensorNameStr;
    		String selectSQL = "select sensorvalue from sensorUsage where "+
    		" sensorname = '" + sensorNameStr + 
    		"' order by TimeInserted asc";
    
    		String retrievedSensorData = noDataAvailable;
    	    try {       
    	        PreparedStatement pst = conn.prepareStatement(selectSQL);
    	        ResultSet rs = pst.executeQuery();
    	        while (rs.next()) {
    	        	retrievedSensorData = rs.getString(1);
    	            System.out.println("DEBUG: Retrieved : "+ retrievedSensorData);
    	        }
    
    	    } catch (SQLException ex) {
    	            System.out.println("Error in SQL " + ex.getMessage());
    	    }
    		
    	    System.out.println("DEBUG: Final retrieved value : " + retrievedSensorData);
    	    response.setContentType("application/json");
    		
    	    String json = "{\"sensor\": {\"" + sensorNameStr + 
    	    		      "\": \"" + retrievedSensorData + "\"}}";
    
    		PrintWriter out = response.getWriter();
    		System.out.println("DEBUG: json return: "+json);
    		out.print(json);
    		out.close();
    
    	}
    		
Do i have to create a for loop so all values are returned and outputted on a webpage? as thats how am viewing the data without manually going into the server. Also, if i do this, is it possible to only receive the last few inserted sensor values? 

Help will be appreciated! 