Home » Java » retreive resultset from a custom query in java

retreive resultset from a custom query in java

Posted by: admin June 30, 2018 Leave a comment


my problem is not how to retreive data from resultset but it’s about a resultset that it’s not like we used to know

i will explain :

My database is on an remote server, connection via ssh established and everything works perfectly
but the problem is that i’m using resultset to get result from a “queryString” variable that i defined which get the query as a string from the front-end as a request and then , this query will be executed with the method i defined

and since we don’t know beforehand from which table we re going to read data and how many columns are we going to retrieve , i didn’t know how to make this method works on every recieve query

below is my code snippet :

public SqlQuery exeuteReceivedQuery(String queryString) throws SQLException {
    Statement st = (Statement) conn.createStatement();
    ResultSet rs = st.executeQuery(queryString);
    SqlQuery result = null;
    while(rs.next()) {
    result = new SqlQuery();
    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);

Does anyone of you have an idea on how to do this ?
Thanks in advance 🙂