Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import org.bukkit.entity.Player;
- import java.sql.*;
- import java.util.UUID;
- public class Poop {
- /*
- MYSQL is pretty simple to setup, there's only 3 methods you need, but I'll add 3 additional methods.
- Methods:
- initializeConnection (the main/source of setting up mysql)
- closeConnection (close remote mysql host connection (usually when you're disabling your server)
- closePreparedStatement(Closing the connection's prepared statement if the statement isn't in use anymore (already executed)
- ifConnectionClosed (just a simple boolean method testing if the connection is closed)
- Fields:
- Strings:
- host (database-host), database(database-name), username(your mysql host's username), password(your mysql host's password)
- Integers:
- port (host-port usually starts with 3306 (default)
- Note: I'd recommend putting the methods in static since it's easier than a singleton (instance of the class's methods)
- */
- /*
- This is an import field for mysql, it's the connection to remote server/host
- Remember you've to import java.sql.Connection (the right import)
- */
- private static Connection connection;
- // Class Fields
- private static String host, database, username, password;
- private static int port;
- public static void initializeConnection() {
- //just setting the values of the variables
- host = "localhost";
- database = "My_Database";
- username = "Savage123";
- password = "password";
- port = 3306; // Remember, it's the default port (which is pretty much universal)
- try {
- // we're just gonna synchronize the sql's thread. (If you don't know what it is, google is ur friend).
- synchronized (Poop.class) {
- /*
- This is to not make the code longer. I'm not really sure what this is, but
- PHP MyAdmin is on a web browser, and it's start of the link is below.
- */
- final String URL = "jdbc:mysql://";
- // Just registering the driver
- Class.forName("com.mysql.jdbc.Driver");
- // NOW, this is a very important part. It's setting the MYSQL connection to obtain our values, when the plugin starts up it's gonna run it's gonna connection to our host and database
- connection = DriverManager.getConnection(URL + host + ":" + port + "/" + database, username, password);
- /*
- Bukkit.getLogger().info("MYSQL connection is now established");
- For debugging purposes I recommend
- */
- }
- // Just exception needed towards this block of code
- } catch(SQLException | ClassNotFoundException e) {
- e.printStackTrace();
- }
- }
- // This might be hard at first, but in time if you keep doing it, you'll remember every fundamentals on how to insert values in a database.
- public static void createPlayer(final UUID uuid, Player player) {
- /*
- This is a query witch executes in this in the mysql database
- Let's break this down,
- INSERT INTO = basically it's very explanatory, make the words anything that makes more sense like, "GO INTO"
- playerTable(UUID,NAME,COINS) = so, I made a table in my database called "playerTable", it has these valuesL UUID, Name, Coins.
- VALUES(?,?,?) = question mark values are basically unset values. I've 3 values that I want to put it into, UUID, Name, and Coins, if you're still confused just go down and see statement.set<Data-Type();
- */
- String query = "INSERT INTO playerTable(UUID,NAME,COINS) VALUES (?,?,?)";
- try {
- // If you read the other method (playerExist - below), you'd know why I am putting this here.
- if (!playerExists(uuid)) {
- // Pretty simple just executing the query into a prepared statement (used for mysql) and putting it on the connection
- PreparedStatement statement = connection.prepareStatement(query);
- /*
- AS you see, it's more simpler than it is, if you look at the parameters in this method (createPlayer), you can see where I get these methods.
- The first param is a number, and as you can see, it follows the UUID(1), Name,(2) and Coins(3), pretty simple now.
- Do statement.(dot) for more values and other methods
- */
- statement.setString(1, uuid.toString());
- statement.setString(2, player.getName());
- statement.setLong(3, 1000L);
- // Just executing the query in the database to update the database's values
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- // This is an addition towards the createPlayer method exploring other fundamentals of how to insert values/check values too.
- public static boolean playerExists(UUID uuid) {
- /*
- This query is basically saying "Select all from playerTable where UUID = something"
- It's pretty simple on what's it's saying, if you read the other method you'd know why what's the question mark for.
- Note: * = ALL from the database
- */
- String query = "SELECT * FROM playerTable WHERE UUID = ?";
- try {
- // Setting up the statement
- PreparedStatement statement = connection.prepareStatement(query);
- // Setting up first param in the statement to this method's param
- statement.setString(1, uuid.toString());
- /*
- This might look confusing, but, it's like an enchanted for loop.
- It ALL of the database and if it matches our statement's param it will execute true, if not, it will execute false.
- */
- ResultSet results = statement.executeQuery();
- // If you're still confused, you'll get it later on if you practice or just trying to add this to plugins, you'll see how it supposed to work.
- if (results.next()) {
- return true;
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return false;
- }
- /*
- Since you know basically how to do it, these other methods won't have a lot of comments on it.
- Note: A lot of MYSQL methods always use an exception, such as an SQLException.
- */
- // Just closing MYSQL's connection so the connection wll terminate and MYSQL won't work on your server.
- public static void closeConnection() {
- if (!isConnectionClosed()) {
- try {
- // You can see more methods in the connection(dot)
- connection.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- public static void closePreparedStatement(PreparedStatement statement) {
- // Closing a connection's prepared statement if the statement isn't in use anymore (already executed)
- if (!isConnectionClosed()) {
- try {
- statement.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- public static boolean isConnectionClosed() {
- // Checking if the connection is closed to make it easier to handle
- try {
- return connection.isClosed();
- // These exceptions are basically ignored because this is just a method for booleans, not to output it to the server's console for error message xD
- } catch (SQLException | NullPointerException ignored) {
- }
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement