简体   繁体   English

Java JDBC如何显示Json格式的结果

[英]Java JDBC how can I display results that are already in Json format

I am using the postgres database and my actual queries are now returned in Json format and I do not know how to display that to the screen. 我正在使用postgres数据库,现在我的实际查询以Json格式返回,但我不知道如何将其显示在屏幕上。 For instance before I would do this 例如,在我这样做之前

    org.json.JSONObject jo = null;
    org.json.JSONArray ja = new org.json.JSONArray();

String myquery = "select DISTINCT ON(city,state)latitudes,longitudes,thirtylatmin,thirtylatmax,thirtylonmin,thirtylonmax,city,state from zips where city ilike 'Orl%'  order by city,state,ziptype asc limit 10";
            conn = db.getConnection();


            ps = conn.prepareStatement(myquery);
            rs = ps.executeQuery();

            while (rs.next()) {
                jo = new org.json.JSONObject();
                jo.put("city", rs.getString("city"));
                jo.put("state", rs.getString("state").trim());
                jo.put("latitudes", rs.getFloat("latitudes"));
                jo.put("longitudes", rs.getFloat("longitudes"));

                jo.put("thirtylatmin", rs.getFloat("thirtylatmin"));

                jo.put("thirtylatmax", rs.getFloat("thirtylatmax"));
                jo.put("thirtylonmin", rs.getFloat("thirtylonmin"));
                jo.put("thirtylonmax", rs.getFloat("thirtylonmax"));
                ja.put(jo);
            }
            org.json.JSONObject mainObj = new org.json.JSONObject();
            mainObj.put("location_update", ja);


            String total = mainObj.toString();

            if (ps != null) {
                ps.close();
            }
            conn.close();
            return ok(total);

As you can see I would do my query then convert it to Json format and show it on the browser. 如您所见,我将执行查询,然后将其转换为Json格式并在浏览器中显示。 My code now is like this 我的代码现在是这样的

String myquery = "select json_build_object('Profile', array_to_json(array_agg(t))) from (select DISTINCT ON(city,state)latitudes,longitudes,thirtylatmin,thirtylatmax,thirtylonmin,thirtylonmax,city,state from zips where city ilike 'Orl%' order by city,state,ziptype asc limit 10)t";

                conn = db.getConnection();


                ps = conn.prepareStatement(myquery);

                rs = ps.executeQuery();
String results=null;
                while (rs.next()) {

                }



                String total = results.toString();
                // System.err.println(total);
                return ok(total);

My query now returns in Json format so there is no need to convert to Json, how can I get the results of that query to return to the browser . 现在,我的查询以Json格式返回,因此无需转换为Json,如何获取该查询的结果以返回到浏览器。 I am new to JDBC and my other question is that I know that JDBC is blocking but is there a limit to the number of request per second that it can handle ? 我是JDBC的新手,我的另一个问题是我知道JDBC正在阻塞,但是每秒可以处理的请求数是否有限制? Assuming that I keep adding more CPU's ? 假设我继续添加更多的CPU?

JDBC does not support JSON datatype, just get it as string. JDBC不支持JSON数据类型,只能将其作为字符串获取。

rs = ps.executeQuery();
String results=null;
if (rs.next()) {
    result = rs.getString(1);
}
String total = results.toString();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM