I have a function QueryRunner
that takes a query, runs it and returns the result.
public static ResultSet QueryGetter(String query) {
Connection connection = null;
ResultSet rslt = null;
try {
Class.forName(driverName);
connection = DriverManager.getConnection(url + ":" + port + "/" + dbName,
username, password);
Statement statement = connection.createStatement();
rslt = statement.executeQuery(query);
} catch (SQLException e) {
System.out.println(e);
} catch (ClassNotFoundException ex) {
Logger.getLogger(QueriesRunner.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
connection.close();
} catch (SQLException ex) {
System.out.println(ex);
}
}
return rslt;
However, when I use it to run a query, I get "Operation not allowed after ResultSet closed". It is because I close the connection.
How can I modify the return signature of my function to make it work?
I tried using a List
, but the thing is that I have to distinguish between int
and String
by using rslt.getString()
.
I don't want to do that. I want to return the content of the ResultSet regardless of the type of the object in the SQL table (int, date, String ...). I will then process the content later, in another function.