Example usage for java.sql ResultSet isBeforeFirst

List of usage examples for java.sql ResultSet isBeforeFirst

Introduction

In this page you can find the example usage for java.sql ResultSet isBeforeFirst.

Prototype

boolean isBeforeFirst() throws SQLException;

Source Link

Document

Retrieves whether the cursor is before the first row in this ResultSet object.

Usage

From source file:i5.las2peer.services.mobsos.SurveyService.java

/**
 * TODO: write documentation//ww  w . j av a  2  s .co m
 * 
 * @param full
 * @param query
 * @return
 */
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("surveys")
@ResourceListApi(description = "Manage surveys")
@Summary("search or list questionnaires.")
@Notes("query parameter matches questionnaire name, description.")
@ApiResponses(value = {
        @ApiResponse(code = 200, message = "Questionnaires data (TODO: introduce Swagger models)"), })
public HttpResponse getSurveys(@QueryParam(defaultValue = "1", name = "full") int full,
        @QueryParam(defaultValue = "", name = "q") String query) {

    String onAction = "retrieving surveys";

    try {
        JSONObject r = new JSONObject(); //result to return in HTTP response
        JSONArray qs = new JSONArray(); // variable for collecting surveys from DB

        Connection c = null;
        PreparedStatement s = null;
        ResultSet rs = null;

        // use query for survey id per default
        String sQuery = "select id from survey where name like ? or description like ? or organization like ?";

        // if query parameter full is provided greater 0, then use query for full questionnaire data set.
        if (full > 0) {
            sQuery = "select * from survey where name like ? or description like ? or organization like ? order by name";
        }

        try {
            c = dataSource.getConnection();
            s = c.prepareStatement(sQuery);
            s.setString(1, "%" + query + "%");
            s.setString(2, "%" + query + "%");
            s.setString(3, "%" + query + "%");

            rs = s.executeQuery();

            // in case result set is empty...
            if (!rs.isBeforeFirst()) {
                r.put("surveys", qs);
                HttpResponse result = new HttpResponse(r.toJSONString());
                result.setStatus(200);
                return result;
            }

            // in case result set contains entries...
            while (rs.next()) {
                if (full > 0) {
                    JSONObject survey = readSurveyFromResultSet(rs);
                    survey.put("url", epUrl + "surveys/" + survey.get("id"));
                    qs.add(survey);
                } else {
                    String id = rs.getString("id");
                    qs.add(epUrl + "surveys/" + id);
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
            return internalError(onAction);
        } finally {
            try {
                if (rs != null)
                    rs.close();
            } catch (Exception e) {
                e.printStackTrace();
                return internalError(onAction);
            }
            try {
                if (s != null)
                    s.close();
            } catch (Exception e) {
                e.printStackTrace();
                return internalError(onAction);
            }
            try {
                if (c != null)
                    c.close();
            } catch (Exception e) {
                e.printStackTrace();
                return internalError(onAction);
            }
        }

        r.put("surveys", qs);
        HttpResponse result = new HttpResponse(r.toJSONString());
        result.setStatus(200);
        return result;

    } catch (Exception e) {
        e.printStackTrace();
        return internalError(onAction);
    }
}

From source file:i5.las2peer.services.mobsos.SurveyService.java

/**
 * TODO: write documentation/*  w  w w.  j av a 2 s. co m*/
 * Retrieves a list of all questionnaires.
 * @return
 * @throws SQLException 
 */
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("questionnaires")
@ResourceListApi(description = "Manage questionnaires")
@Summary("search or list questionnaires.")
@Notes("query parameter matches questionnaire name, description.")
@ApiResponses(value = {
        @ApiResponse(code = 200, message = "Questionnaires data (TODO: introduce Swagger models)"), })
public HttpResponse getQuestionnaires(@QueryParam(name = "full", defaultValue = "1") int full,
        @QueryParam(name = "q", defaultValue = "") String query) {
    String onAction = "retrieving questionnaires";

    try {
        JSONObject r = new JSONObject(); //result to return in HTTP response
        JSONArray qs = new JSONArray(); // variable for collecting questionnaires from DB

        Connection c = null;
        PreparedStatement s = null;
        ResultSet rs = null;

        // use query for questionnaire id per default
        String sQuery = "select id from questionnaire where name like ? or description like ? or organization like ?";

        // if query param full is provided greater 0, then use query for full questionnaire data set.
        if (full > 0) {
            sQuery = "select * from questionnaire where name like ? or description like ? or organization like ? order by name";
        }

        // +++ dsi 
        try {
            c = dataSource.getConnection();
            s = c.prepareStatement(sQuery);
            s.setString(1, "%" + query + "%");
            s.setString(2, "%" + query + "%");
            s.setString(3, "%" + query + "%");

            rs = s.executeQuery();

            // in case result set is empty...
            if (!rs.isBeforeFirst()) {
                r.put("questionnaires", qs);
                HttpResponse result = new HttpResponse(r.toJSONString());
                result.setStatus(200);
                return result;
            }

            // in case result set contains entries...
            while (rs.next()) {
                if (full > 0) {
                    JSONObject questionnaire = readQuestionnaireFromResultSet(rs);
                    questionnaire.put("url", epUrl + "questionnaires/" + questionnaire.get("id"));
                    qs.add(questionnaire);
                } else {
                    String id = rs.getString("id");
                    qs.add(epUrl + "questionnaires/" + id);
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
            return internalError(onAction);
        } finally {
            try {
                if (rs != null)
                    rs.close();
            } catch (Exception e) {
                e.printStackTrace();
                return internalError(onAction);
            }
            try {
                if (s != null)
                    s.close();
            } catch (Exception e) {
                e.printStackTrace();
                return internalError(onAction);
            }
            try {
                if (c != null)
                    c.close();
            } catch (Exception e) {
                e.printStackTrace();
                return internalError(onAction);
            }
        }
        // --- dsi

        r.put("questionnaires", qs);
        HttpResponse result = new HttpResponse(r.toJSONString());
        result.setStatus(200);
        return result;
    } catch (Exception e) {
        e.printStackTrace();
        return internalError(onAction);
    }
}