Advertisement
Guest User

Untitled

a guest
Aug 19th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.86 KB | None | 0 0
  1.  
  2. import org.bukkit.entity.Player;
  3.  
  4. import java.sql.*;
  5. import java.util.UUID;
  6.  
  7. public class Poop {
  8.  
  9.     /*
  10.         MYSQL is pretty simple to setup, there's only 3 methods you need, but I'll add 3 additional methods.
  11.  
  12.         Methods:
  13.             initializeConnection (the main/source of setting up mysql)
  14.  
  15.             closeConnection (close remote mysql host connection (usually when you're disabling your server)
  16.             closePreparedStatement(Closing the connection's prepared statement if the statement isn't in use anymore (already executed)
  17.             ifConnectionClosed (just a simple boolean method testing if the connection is closed)
  18.  
  19.         Fields:
  20.             Strings:
  21.                 host (database-host), database(database-name), username(your mysql host's username), password(your mysql host's password)
  22.             Integers:
  23.                 port (host-port usually starts with 3306 (default)
  24.  
  25.         Note: I'd recommend putting the methods in static since it's easier than a singleton (instance of the class's methods)
  26.      */
  27.  
  28.  
  29.     /*
  30.         This is an import field for mysql, it's the connection to remote server/host
  31.         Remember you've to import java.sql.Connection (the right import)
  32.      */
  33.     private static Connection connection;
  34.  
  35.     // Class Fields
  36.     private static String host, database, username, password;
  37.     private static int port;
  38.  
  39.     public static void initializeConnection() {
  40.         //just setting the values of the variables
  41.         host = "localhost";
  42.         database = "My_Database";
  43.         username = "Savage123";
  44.         password = "password";
  45.         port = 3306; // Remember, it's the default port (which is pretty much universal)
  46.  
  47.         try {
  48.             // we're just gonna synchronize the sql's thread. (If you don't know what it is, google is ur friend).
  49.             synchronized (Poop.class) {
  50.                 /*
  51.                     This is to not make the code longer. I'm not really sure what this is, but
  52.                     PHP MyAdmin is on a web browser, and it's start of the link is below.
  53.                  */
  54.                 final String URL = "jdbc:mysql://";
  55.  
  56.                 // Just registering the driver
  57.                 Class.forName("com.mysql.jdbc.Driver");
  58.  
  59.                 // 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
  60.                 connection = DriverManager.getConnection(URL + host + ":" + port + "/" + database, username, password);
  61.  
  62.                 /*
  63.                     Bukkit.getLogger().info("MYSQL connection is now established");
  64.                     For debugging purposes I recommend
  65.                  */
  66.             }
  67.             // Just exception needed towards this block of code
  68.         } catch(SQLException | ClassNotFoundException e) {
  69.             e.printStackTrace();
  70.         }
  71.     }
  72.  
  73.     // 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.
  74.     public static void createPlayer(final UUID uuid, Player player) {
  75.  
  76.         /*
  77.             This is a query witch executes in this in the mysql database
  78.             Let's break this down,
  79.  
  80.             INSERT INTO = basically it's very explanatory, make the words anything that makes more sense like, "GO INTO"
  81.             playerTable(UUID,NAME,COINS) = so, I made a table in my database called "playerTable", it has these valuesL UUID, Name, Coins.
  82.             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();
  83.          */
  84.         String query = "INSERT INTO playerTable(UUID,NAME,COINS) VALUES (?,?,?)";
  85.  
  86.         try {
  87.             // If you read the other method (playerExist - below), you'd know why I am putting this here.
  88.             if (!playerExists(uuid)) {
  89.  
  90.                 // Pretty simple just executing the query into a prepared statement (used for mysql) and putting it on the connection
  91.                 PreparedStatement statement = connection.prepareStatement(query);
  92.  
  93.             /*
  94.                 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.
  95.                 The first param is a number, and as you can see, it follows the UUID(1), Name,(2) and Coins(3), pretty simple now.
  96.  
  97.                 Do statement.(dot) for more values and other methods
  98.               */
  99.                 statement.setString(1, uuid.toString());
  100.                 statement.setString(2, player.getName());
  101.                 statement.setLong(3, 1000L);
  102.  
  103.                 // Just executing the query in the database to update the database's values
  104.  
  105.             }
  106.         } catch (SQLException e) {
  107.             e.printStackTrace();
  108.         }
  109.     }
  110.  
  111.     // This is an addition towards the createPlayer method exploring other fundamentals of how to insert values/check values too.
  112.     public static boolean playerExists(UUID uuid) {
  113.         /*
  114.             This query is basically saying "Select all from playerTable where UUID = something"
  115.  
  116.             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.
  117.  
  118.             Note: * = ALL from the database
  119.           */
  120.         String query = "SELECT * FROM playerTable WHERE UUID = ?";
  121.  
  122.         try {
  123.             // Setting up the statement
  124.             PreparedStatement statement = connection.prepareStatement(query);
  125.  
  126.             // Setting up first param in the statement to this method's param
  127.             statement.setString(1, uuid.toString());
  128.  
  129.             /*
  130.                 This might look confusing, but, it's like an enchanted for loop.
  131.                 It ALL of the database and if it matches our statement's param it will execute true, if not, it will execute false.
  132.              */
  133.  
  134.             ResultSet results = statement.executeQuery();
  135.  
  136.             // 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.
  137.             if (results.next()) {
  138.                 return true;
  139.             }
  140.         } catch (SQLException e) {
  141.             e.printStackTrace();
  142.         }
  143.         return false;
  144.     }
  145.  
  146.  
  147.     /*
  148.         Since you know basically how to do it, these other methods won't have a lot of comments on it.
  149.         Note: A lot of MYSQL methods always use an exception, such as an SQLException.
  150.      */
  151.  
  152.     // Just closing MYSQL's connection so the connection wll terminate and MYSQL won't work on your server.
  153.     public static void closeConnection() {
  154.         if (!isConnectionClosed()) {
  155.             try {
  156.                 // You can see more methods in the connection(dot)
  157.                 connection.close();
  158.             } catch (SQLException e) {
  159.                 e.printStackTrace();
  160.             }
  161.         }
  162.     }
  163.  
  164.     public static void closePreparedStatement(PreparedStatement statement) {
  165.         // Closing a connection's prepared statement if the statement isn't in use anymore (already executed)
  166.  
  167.         if (!isConnectionClosed()) {
  168.             try {
  169.                 statement.close();
  170.             } catch (SQLException e) {
  171.                 e.printStackTrace();
  172.             }
  173.         }
  174.     }
  175.  
  176.     public static boolean isConnectionClosed() {
  177.         // Checking if the connection is closed to make it easier to handle
  178.  
  179.         try {
  180.             return connection.isClosed();
  181.             // 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
  182.         } catch (SQLException | NullPointerException ignored) {
  183.         }
  184.  
  185.         return true;
  186.     }
  187.  
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement