From 969eff4dadf4ae998ae4a4c79e42bd37c26eb6e2 Mon Sep 17 00:00:00 2001 From: Starky Date: Wed, 22 Jun 2022 02:51:56 -0400 Subject: [PATCH] Uses result of next() to return empty optional if query is invalid --- .../grubnest/game/core/databasehandler/MySQL.java | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/grubnest/game/core/databasehandler/MySQL.java b/src/main/java/com/grubnest/game/core/databasehandler/MySQL.java index 9771619..507ced0 100644 --- a/src/main/java/com/grubnest/game/core/databasehandler/MySQL.java +++ b/src/main/java/com/grubnest/game/core/databasehandler/MySQL.java @@ -91,9 +91,12 @@ public Optional getIdFromUsername(String username) { """)) { statement.setString(1, username); ResultSet queryResults = statement.executeQuery(); - queryResults.next(); - return Optional.of(UUID.fromString(queryResults.getString(1))); + if (queryResults.next()) { + return Optional.of(UUID.fromString(queryResults.getString(1))); + } else { + return Optional.empty(); + } } catch (SQLException e) { e.printStackTrace(); @@ -116,9 +119,12 @@ public Optional getUsernameFromID(UUID id) { """); statement.setString(1, id.toString()); ResultSet queryResults = statement.executeQuery(); - queryResults.next(); - return Optional.of(queryResults.getString(1)); + if (queryResults.next()) { + return Optional.of(queryResults.getString(1)); + } else { + return Optional.empty(); + } } catch (SQLException e) { e.printStackTrace();