How will Spring boot work with Sql Response?

25 Views Asked by At

I have a service through which I am hitting an endpoint with some Query in some remote relational database. So when I hit that remote db with Select statement then in response body I recieve data.

Now My question How should I recieve response from other write queries.

try {
            Statement stmt = createConnectionAndGetStatement();
            System.out.println("\tGot the statement object, object-id : " + stmt);
            ResultSet rs = stmt.executeQuery(selectSQL);
            System.out.println("\tGot the result set object, object-id : " + rs);

                ResultSetMetaData md = rs.getMetaData();
                int numCols = md.getColumnCount();
                List<String> colNames = IntStream.range(0, numCols).mapToObj(i -> {
                            try {
                                return md.getColumnName(i + 1);
                            } catch (SQLException e) {
                                e.printStackTrace();
                                return "?";
                            }
                        })
                        .collect(Collectors.toList());

                JSONArray result = new JSONArray();
                while (rs.next()) {
                    JSONObject row = new JSONObject();
                    colNames.forEach(cn -> {
                        try {
                            row.put(cn, rs.getObject(cn));
                        } catch (JSONException | SQLException e) {
                            e.printStackTrace();
                        }
                    });
                    result.put(row);
                }
                return result.toString();

        } catch (SQLException exp) {
            exp.printStackTrace();
        }
        return "not executed";
    }

Till now I am just checking if the status code of response is 200 or 201 then it is fine. But I want a response of that remote db.

0

There are 0 best solutions below