Skip to content

Instantly share code, notes, and snippets.

@SpiroMarshes
Created June 4, 2017 17:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SpiroMarshes/fb9cdb5092bcf47811fac54b460063ce to your computer and use it in GitHub Desktop.
Save SpiroMarshes/fb9cdb5092bcf47811fac54b460063ce to your computer and use it in GitHub Desktop.
Find out if a UUID exists in the database
/**
* Checks to see if the database has an entry for a specific UUID
*
* @param uuid The player's UUID to look for
* @return Boolean value, true if the UUID is found, false if not.
*/
public synchronized static boolean playerDataContainsUUID(UUID uuid)
{
//Create connection instance
Connection connection = null;
String select = "SELECT `uuid` FROM `player_data` WHERE uuid=?;";
PreparedStatement sql = null;
ResultSet results = null;
try
{
//Initialise hikari connection, by getting the hikari connect if established
connection = Database.getHikari().getConnection();
//Preparing statement - SELECT FROM...
sql = connection.prepareStatement(select);
//Setting parameters in MySQL query: i.e the question marks(?), where the first one has the index of 1.
sql.setString(1, uuid.toString());
//Executes the statement
results = sql.executeQuery();
// will be true if the Database contains an entry for the player's UUID
boolean containsPlayer = results.next();
sql.close();
results.close();
return containsPlayer;
} catch (SQLException e)
{
//Print out any exception while trying to prepare statement
e.printStackTrace();
} finally
{
//After catching the statement, close connection if connection is established
if (connection != null)
{
try
{
connection.close();
} catch (SQLException e)
{
e.printStackTrace();
}
}
// If connection is established, close connection after query
if (sql != null)
{
try
{
sql.close();
} catch (SQLException e)
{
e.printStackTrace();
}
}
// If results is open, close it
if (results != null)
{
try
{
results.close();
} catch (SQLException e)
{
e.printStackTrace();
}
}
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment