The SaveSystem class has methods for saving and loading player positions in a database.
MySQL database.
The JDBC URL for connecting to the MySQL database.
private static final String JDBC_URL = "//localhost:xxxx/game_database";
The username for accessing the MySQL database.
private static final String DB_USERNAME = "username";
The password for accessing the MySQL database.
private static final String DB_PASSWORD = "password";
Saves the player's position to the database.
@param username The username of the player.
@param x The x-coordinate of the player's position.
@param y The y-coordinate of the player's position.
Loads the player's position from the database.
@param username The username of the player.
@return An array containing the x and y coordinates of the player's position, or null if the position could not be loaded.
public static int[] loadPlayerPosition(String username) {
try (Connection connection = DriverManager.getConnection(JDBC_URL, DB_USERNAME, DB_PASSWORD);
PreparedStatement statement = connection.prepareStatement("SELECT x, y FROM player_positions WHERE username = ?")) {
statement.setString(1, username);
ResultSet resultSet = statement.executeQuery();
if (resultSet.next()) {
int[] playerPosition = new int[2];
playerPosition[0] = resultSet.getInt("x");
playerPosition[1] = resultSet.getInt("y");
return playerPosition;
} else {
System.out.println("Save file not found for user: " + username);
return null;
}
} catch (SQLException e) {
System.err.println("Error loading player position: " + e.getMessage());
return null;
}
}
The main method serves as an example usage of the SaveSystem class.
It saves and loads the player's position to and from the database.
@param args The command-line arguments (not used).
public static void main(String[] args) {
// Example usage
String username = "exampleUser";
int playerX = 10;
int playerY = 20;
// Save player position
savePlayerPosition(username, playerX, playerY);
// Load player position
int[] loadedPosition = loadPlayerPosition(username);
if (loadedPosition != null) {
System.out.println("Player position loaded: (" + loadedPosition[0] + ", " + loadedPosition[1] + ")");
}
}
Saving player spot via x and y coordinates
The SaveSystem class has methods for saving and loading player positions in a database. MySQL database.
private static final String JDBC_URL = "//localhost:xxxx/game_database";
The username for accessing the MySQL database. private static final String DB_USERNAME = "username";
The password for accessing the MySQL database. private static final String DB_PASSWORD = "password";
Saves the player's position to the database.
Loads the player's position from the database. @param username The username of the player. @return An array containing the x and y coordinates of the player's position, or null if the position could not be loaded.
The main method serves as an example usage of the SaveSystem class. It saves and loads the player's position to and from the database.
@param args The command-line arguments (not used).
}