我的问题不是如何从结果集中检索数据,而是关于结果集,它不像我们以前知道的那样
我会解释:
我的数据库位于远程服务器上,通过ssh建立连接,一切正常 但问题是我使用结果集来获取我定义的“queryString”变量的结果,该变量将查询作为前端的字符串作为请求获取,然后,此查询将使用我定义的方法执行
由于我们事先不知道从哪个表中读取数据以及要检索的列数,我不知道如何使这个方法适用于每个接收查询
下面的是我的代码段:
@Override
public SqlQuery exeuteReceivedQuery(String queryString) throws SQLException {
ConnectToDataBase();
Statement st = (Statement) conn.createStatement();
ResultSet rs = st.executeQuery(queryString);
SqlQuery result = null;
while(rs.next()) {
result = new SqlQuery();
result.getQueryResult();
}
return result;
}
// controller
@GetMapping(value = "/{queryString}",produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<SqlQuery> getResults(@PathVariable("queryString") String queryString) throws SQLException {
SqlQuery cmd = sqlService.exeuteReceivedQuery(queryString);
return new ResponseEntity<>(cmd, HttpStatus.OK);
}
你们有谁知道如何做到这一点? 在此先感谢:)
答案 0 :(得分:1)
您可以访问ResultSet的元数据
rs.getMetaData().getColumnCount();
然后
rs.getMetaData().getColumnName(i);
您可以从元数据列,名称,数据类型等中获取提取结果所需的全部内容