How to return a ResultSet ?

My code is:

 public ResultSet getUser(String nickname,String password){ ResultSet resSet = null; String sql = "SELECT * FROM dvsusers WHERE username=? AND password=?"; try (PreparedStatement prSt = getDbConnection().prepareStatement(sql)) { prSt.setString(1, nickname); prSt.setString(2, password); resSet = prSt.executeQuery(); } catch (SQLException | ClassNotFoundException e) { e.printStackTrace(); } return resSet; } 
  • one
    It is most likely "( javadoc ), so you can’t just return and return the ResultSet . - zRrr
  • How to get it back? I know that just can not. - Maxim Aleksandrovich
  • As I understand. You must first get the data from the ResultSet and then transfer it. And how to do it and did not understand. - Maxim Aleksandrovich

1 answer 1

As it was written above, you cannot just return a ResultSet, but this can be done through caching its data in CachedRowSet

 public ResultSet getUser(String nickname, String password) throws SQLException { ResultSet resSet = null; String sql = "SELECT * FROM dvsusers WHERE username=? AND password=?"; try (PreparedStatement prSt = getDbConnection().prepareStatement(sql)) { prSt.setString(1, nickname); prSt.setString(2, password); resSet = prSt.executeQuery(); } catch (SQLException | ClassNotFoundException e) { e.printStackTrace(); } RowSetFactory factory = RowSetProvider.newFactory(); CachedRowSet cachedRowSet = factory.createCachedRowSet(); cachedRowSet.populate(resSet); return cachedRowSet; } 
  • Can not do as you describe in the answer. The error is here - .newFactory (); right here - createCachedRowSet (); and here - createCachedRowSet (); . Underlines in red. - Maxim Aleksandrovich