[英]Java JDBC how can I display results that are already in Json format
我正在使用postgres數據庫,現在我的實際查詢以Json格式返回,但我不知道如何將其顯示在屏幕上。 例如,在我這樣做之前
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);
如您所見,我將執行查詢,然后將其轉換為Json格式並在瀏覽器中顯示。 我的代碼現在是這樣的
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);
現在,我的查詢以Json格式返回,因此無需轉換為Json,如何獲取該查詢的結果以返回到瀏覽器。 我是JDBC的新手,我的另一個問題是我知道JDBC正在阻塞,但是每秒可以處理的請求數是否有限制? 假設我繼續添加更多的CPU?
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.