【发布时间】:2016-11-21 16:33:20
【问题描述】:
我正在尝试为 Web 服务实现 java 方法,该方法应从表中检索数据并以 json 格式返回结果。为此,我使用 json_agg() 函数将查询结果转换为我想要显示的 json,但到目前为止它以错误的格式显示。这是方法本身:
public String GetRowsFromTable() throws SQLException {
Connection connection = null;
String result = "";
try {
connection = Connection.getConnection();
PreparedStatement prepStmt = conn.prepareStatement("SELECT json_agg(table1) FROM table");
ResultSet rs = prepStmt.executeQuery();
result += rs;
System.out.println(rs);
} catch (SQLException sqlex) {
sqlex.printStackTrace();
} finally {
connection.close();
}
return result;
}
控制台显示如下结果:
org.postgresql.jdbc.PgResultSet@5179f609
任何帮助将不胜感激。
【问题讨论】:
-
但目前显示的格式错误这完全没有意义。
-
这也是
Object上.toString()的默认输出,而不是正确调用ResultSet,只需查看ResultSet的源代码,它只是执行默认操作在Object。我认为您需要阅读有关您正在尝试做什么的文档,或者如果您已经阅读过,请再次阅读。
标签: java json postgresql jdbc resultset