CarlaDX
CarlaDX

Reputation: 29

retreive resultset from a custom query in java

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 :

    @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);
}

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

Upvotes: 0

Views: 36

Answers (1)

StanislavL
StanislavL

Reputation: 57381

You can access the ResultSet's metadata

rs.getMetaData().getColumnCount();

and then

rs.getMetaData().getColumnName(i);

You can obtain from the metadata columns, names, data types etc. all you need to extract the results

Upvotes: 1

Related Questions