0

I'm trying to execute method which should create a new object with fields from database, and everytime i run this code im getting SQLException: ResultSet closed.

 public DatabasedClient getDatabaseClient(int clientDatabaseid){
    if(DatabaseClientUtil.isInDatabase(clientDatabaseid)){
        return DatabaseClientUtil.getDBClient(clientDatabaseid);
    }else{
        try{
            System.out.println("Trying to find user in db");
            ResultSet rs = fbot.getStorage().query("select * from database_name where clientDBId = " + clientDatabaseid);
            System.out.println("deb " + rs.getString("nick"));
            while (rs.next()) {
                DatabasedClient databasedClient = new DatabasedClient(clientDatabaseid);
                databasedClient.setUid(rs.getString("uid"));
                databasedClient.setNick(rs.getString("nick"));
                databasedClient.setLastConnect(rs.getLong("lastConnected"));
                databasedClient.setLastDisconnect(rs.getLong("lastDisconnect"));
                databasedClient.setTimeSpent(rs.getLong("timeSpent"));
                databasedClient.setLongestConnection(rs.getLong("longestConnection"));
                return databasedClient;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    return null;
}

}

Im using hikari, here are methods from AbstractStorage class

@Override
public void execute(String query) throws SQLException {
    try (Connection connection = getConnection()){
        connection.prepareStatement(query).executeUpdate();
    }
}

@Override
public ResultSet query(String query) throws SQLException {
    try (Connection connection = getConnection()) {
        return  connection.prepareStatement(query).executeQuery();
    }
}

Screenshot from error enter image description here

I hope someone will help me with this.

smyslov
  • 1,279
  • 1
  • 8
  • 29
xdev
  • 3
  • 4

1 Answers1

2

I think the exact error you are seeing is being caused by the following line of code:

System.out.println("deb " + rs.getString("nick"));

You are trying to access the result set before you advance the cursor to the first record. Also, your method getDatabaseClient is returning a single object which conceptually maps to a single expected record from the query. Hence, iterating once over the result set would seem to make sense. Taking all this into consideration, we can try the following:

try {
    System.out.println("Trying to find user in db");
    ResultSet rs = fbot.getStorage().query("select * from database_name where clientDBId = " + clientDatabaseid);
    // do not access the result set here

    if (rs.next()) {
        DatabasedClient databasedClient = new DatabasedClient(clientDatabaseid);
        databasedClient.setUid(rs.getString("uid"));
        databasedClient.setNick(rs.getString("nick"));
        databasedClient.setLastConnect(rs.getLong("lastConnected"));             
        databasedClient.setLastDisconnect(rs.getLong("lastDisconnect"));
        databasedClient.setTimeSpent(rs.getLong("timeSpent"));
        databasedClient.setLongestConnection(rs.getLong("longestConnection"));
        return databasedClient;
    }
} catch (SQLException e) {
     e.printStackTrace();
}
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • Now its okay, no errors but.. I'm trying to get a clientDBId 4028 from a database and the method still returns me all the time null, even if the user exists in database – xdev Nov 17 '17 at 08:50
  • I don't know your particular API, so I won't be of much more help here. – Tim Biegeleisen Nov 17 '17 at 08:59