I am trying to create a centralized class that connects and returns the ResultSet
of a SQL query so that I don't always have to create a new connection every time I am trying to get a query.
I am using the try-with-resources
, however, I am getting a compile-time error whenever I use the try-with-resources
and I don't know why?
public class JDBC {
// logger declaration is omitted
private static final String dbURL = "jdbc:oracle:";
private static final String userName = "blah";
private static final String password = "12345";
public ResultSet retrieveSQLQuery(String sqlQuery) {
Connection conn = null;
Statement statement = null;
ResultSet rs = null;
try (conn = DriverManager.getConnection(dbUrl, user, password);
statement = conn.createStatement();
rs = statement.executeQuery(sqlQuery)) {
} catch (SQLException e) {
logger.info(e.getMessage());
}
return rs;
}
}