Advertisement
Guest User

MySQL Manager

a guest
Dec 23rd, 2014
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.94 KB | None | 0 0
  1.  
  2. package com.bimmr.mcinfected.MySQL;
  3.  
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.sql.Statement;
  9. import java.util.ArrayList;
  10. import java.util.UUID;
  11. import java.util.logging.Level;
  12.  
  13. import org.bukkit.plugin.Plugin;
  14.  
  15. import com.bimmr.mcinfected.SettingsManager;
  16.  
  17.  
  18. public class MySQLManager {
  19.    
  20.     private Connection  connection;
  21.     private MySQL               mysql;
  22.     private Statement       statement;
  23.    
  24.     /**
  25.      * Create a mysql connection
  26.      *
  27.      * @param plugin
  28.      * @param host
  29.      * @param port
  30.      * @param database
  31.      * @param username
  32.      * @param password
  33.      */
  34.     public MySQLManager(Plugin plugin, String host, String port, String database, String username, String password)
  35.     {
  36.         this.mysql = new MySQL(plugin, host, port, database, username, password);
  37.         openConnection();
  38.     }
  39.    
  40.     /**
  41.      * Closes the connection
  42.      *
  43.      * @throws SQLException
  44.      */
  45.     public void closeConnection() {
  46.         if (SettingsManager.getDebugMode())
  47.             System.out.println("Closing MySQL Connection");
  48.         if (this.connection != null)
  49.             try
  50.             {
  51.                 this.connection.close();
  52.             }
  53.             catch (SQLException e)
  54.             {
  55.                
  56.             }
  57.         this.connection = null;
  58.     }
  59.    
  60.     /**
  61.      * Closes the statement
  62.      *
  63.      * @throws SQLException
  64.      */
  65.     public void closeStatement() {
  66.         if (SettingsManager.getDebugMode())
  67.             System.out.println("Closing MySQL Statement");
  68.         if (this.statement != null)
  69.             try
  70.             {
  71.                 this.statement.close();
  72.             }
  73.             catch (SQLException e)
  74.             {
  75.                
  76.             }
  77.         this.statement = null;
  78.     }
  79.    
  80.     /**
  81.      * Execute an update
  82.      *
  83.      * @param string
  84.      * @throws SQLException
  85.      */
  86.     public void execute(String string) throws SQLException {
  87.        
  88.         openStatement();
  89.        
  90.         this.statement.execute(string);
  91.        
  92.         closeStatement();
  93.        
  94.     }
  95.    
  96.     /**
  97.      * First columns is the player's UUID, so you don't need that For example:
  98.      * <p>
  99.      * "Kills INT(10), Deaths INT(10), Points INT(10), Score INT(10), PlayingTime INT(15), HighestKillStreak INT(10)"
  100.      * <p>
  101.      * ^ Would check if a table with all that exists, and if not it creates it
  102.      *
  103.      * @param table
  104.      * @param mySqlColoumData
  105.      * @throws SQLException
  106.      */
  107.     public void createTableIfDoesntExist(String tableName, String mySqlColoumData) throws SQLException {
  108.        
  109.         openStatement();
  110.        
  111.         this.statement.executeUpdate("CREATE TABLE IF NOT EXISTS " + tableName + " (UUID VARCHAR(40), " + mySqlColoumData + ");");
  112.        
  113.         closeStatement();
  114.        
  115.     }
  116.    
  117.     /**
  118.      * Assumes all players are in columns 1
  119.      *
  120.      * @param tableName
  121.      *          - The tables name
  122.      * @return All the players in the table
  123.      */
  124.     public ArrayList<UUID> getAllPlayers(String tableName) {
  125.        
  126.         try
  127.         {
  128.             if (SettingsManager.getDebugMode())
  129.                 System.out.println("Getting all UUIDs");
  130.             openStatement();
  131.             ResultSet set = this.statement.executeQuery("SELECT * FROM `" + tableName + "` ");
  132.             ArrayList<UUID> players = new ArrayList<UUID>();
  133.             while (true)
  134.             {
  135.                 set.next();
  136.                 players.add(UUID.fromString(set.getString("UUID")));
  137.                 if (set.isLast())
  138.                     break;
  139.             }
  140.             set.close();
  141.             closeStatement();
  142.             return players;
  143.         }
  144.         catch (SQLException e)
  145.         {
  146.             closeStatement();
  147.             ArrayList<UUID> nope = new ArrayList<UUID>();
  148.             return nope;
  149.         }
  150.        
  151.     }
  152.    
  153.     /**
  154.      * Get the value of the column
  155.      *
  156.      * @param tableName
  157.      *          - The tables name
  158.      * @param columnName
  159.      *          - The stats name
  160.      * @param uuid
  161.      * @return the players stats
  162.      * @throws SQLException
  163.      */
  164.     public int getInt(String tableName, String columnName, UUID uuid) {
  165.        
  166.         try
  167.         {
  168.             if (SettingsManager.getDebugMode())
  169.                 System.out.println("Getting value from " + tableName + " - " + uuid + "- " + columnName);
  170.            
  171.             openStatement();
  172.             ResultSet set = this.statement.executeQuery("SELECT " + columnName + " FROM " + tableName + " WHERE UUID = '" + uuid.toString() + "';");
  173.            
  174.             set.next();
  175.            
  176.             int i = set.getInt(columnName);
  177.             set.close();
  178.             closeStatement();
  179.             return i;
  180.         }
  181.         catch (SQLException e)
  182.         {
  183.             closeStatement();
  184.             return 0;
  185.         }
  186.     }
  187.    
  188.     public String getString(String tableName, String columnName, UUID uuid) {
  189.        
  190.         try
  191.         {
  192.             if (SettingsManager.getDebugMode())
  193.                 System.out.println("Getting value from " + tableName + " - " + uuid + "- " + columnName);
  194.            
  195.             openStatement();
  196.             ResultSet set = this.statement.executeQuery("SELECT " + columnName + " FROM " + tableName + " WHERE UUID = '" + uuid.toString() + "';");
  197.            
  198.             set.next();
  199.            
  200.             String s = set.getString(columnName);
  201.             set.close();
  202.             closeStatement();
  203.             return s;
  204.         }
  205.         catch (SQLException e)
  206.         {
  207.             closeStatement();
  208.             return "";
  209.         }
  210.     }
  211.    
  212.     /**
  213.      * Gets if it has a open connection
  214.      *
  215.      * @return
  216.      */
  217.     public boolean hasOpenConnection() {
  218.         return this.connection != null;
  219.     }
  220.    
  221.     /**
  222.      * Gets if it has an open statement
  223.      *
  224.      * @return
  225.      */
  226.     public boolean hasOpenStatement() {
  227.         return this.statement != null;
  228.     }
  229.    
  230.     /**
  231.      * Opens a new connection
  232.      */
  233.     public void openConnection() {
  234.         if (SettingsManager.getDebugMode())
  235.             System.out.println("Opening MySQL Connection");
  236.         this.connection = this.mysql.openConnection();
  237.     }
  238.    
  239.     /**
  240.      * Opens a new statement
  241.      *
  242.      * @throws SQLException
  243.      */
  244.     public void openStatement() {
  245.         if (SettingsManager.getDebugMode())
  246.             System.out.println("Opening MySQL Statement");
  247.         try
  248.         {
  249.             this.statement = this.connection.createStatement();
  250.         }
  251.         catch (SQLException e)
  252.         {
  253.            
  254.         }
  255.     }
  256.    
  257.     private void setInt(String tableName, String columnName, int value, UUID uuid) {
  258.        
  259.         try
  260.         {
  261.             if (SettingsManager.getDebugMode())
  262.                 System.out.println("Setting value into " + tableName + " - " + uuid + "- " + columnName + " - " + value);
  263.             openStatement();
  264.             this.statement.execute("INSERT INTO " + tableName + " (`UUID`, `" + columnName + "`) VALUES ('" + uuid + "', '" + value + "');");
  265.             closeStatement();
  266.         }
  267.         catch (SQLException e)
  268.         {
  269.             closeStatement();
  270.         }
  271.     }
  272.    
  273.     private void setString(String tableName, String columnName, String value, UUID uuid) {
  274.        
  275.         try
  276.         {
  277.             if (SettingsManager.getDebugMode())
  278.                 System.out.println("Setting value into " + tableName + " - " + uuid + "- " + columnName + " - " + value);
  279.             openStatement();
  280.             this.statement.execute("INSERT INTO " + tableName + " (`UUID`, `" + columnName + "`) VALUES ('" + uuid + "', '" + value + "');");
  281.             closeStatement();
  282.         }
  283.         catch (SQLException e)
  284.         {
  285.             closeStatement();
  286.         }
  287.     }
  288.    
  289.     public void unload() throws SQLException {
  290.         closeStatement();
  291.         closeConnection();
  292.     }
  293.    
  294.     /**
  295.      * Safely update/set the value Will set the value only if the table doesn't
  296.      * have the player already, otherwise it'll just update the players values
  297.      *
  298.      * @param tableName
  299.      * @param columnName
  300.      * @param value
  301.      * @param uuid
  302.      */
  303.     public void updateInt(String tableName, String columnName, int value, UUID uuid) {
  304.        
  305.         if (getAllPlayers(tableName).contains(uuid))
  306.         {
  307.             try
  308.             {
  309.                 if (SettingsManager.getDebugMode())
  310.                     System.out.println("Updating value into " + tableName + " - " + uuid + "- " + columnName + " - " + value);
  311.                 openStatement();
  312.                 this.statement.execute("UPDATE " + tableName + " SET " + columnName + "=" + value + " WHERE UUID ='" + uuid + "';");
  313.                 closeStatement();
  314.             }
  315.             catch (SQLException e)
  316.             {
  317.                 e.printStackTrace();
  318.                 closeStatement();
  319.                 setInt(tableName, columnName, value, uuid);
  320.             }
  321.         }
  322.         else
  323.         {
  324.             setInt(tableName, columnName, value, uuid);
  325.         }
  326.        
  327.     }
  328.    
  329.     public void updateString(String tableName, String columnName, String value, UUID uuid) {
  330.        
  331.         if (getAllPlayers(tableName).contains(uuid))
  332.         {
  333.             try
  334.             {
  335.                 if (SettingsManager.getDebugMode())
  336.                     System.out.println("Updating value into " + tableName + " - " + uuid + "- " + columnName + " - " + value);
  337.                 openStatement();
  338.                 this.statement.execute("UPDATE " + tableName + " SET " + columnName + "=" + value + " WHERE UUID ='" + uuid + "';");
  339.                 closeStatement();
  340.             }
  341.             catch (SQLException e)
  342.             {
  343.                 e.printStackTrace();
  344.                 closeStatement();
  345.                 setString(tableName, columnName, value, uuid);
  346.             }
  347.         }
  348.         else
  349.         {
  350.             setString(tableName, columnName, value, uuid);
  351.         }
  352.        
  353.     }
  354.    
  355.     public class MySQL {
  356.        
  357.         private Connection      connection;
  358.         private final String    database;
  359.         private final String    hostname;
  360.         private final String    password;
  361.         private Plugin              plugin;
  362.        
  363.         private final String    port;
  364.        
  365.         private final String    user;
  366.        
  367.         /**
  368.          * Creates a new MySQL instance
  369.          *
  370.          * @param plugin
  371.          *          Plugin instance
  372.          * @param hostname
  373.          *          Name of the host
  374.          * @param port
  375.          *          Port number
  376.          * @param database
  377.          *          Database name
  378.          * @param username
  379.          *          Username
  380.          * @param password
  381.          *          Password
  382.          */
  383.         public MySQL(Plugin plugin, String hostname, String port, String database, String username, String password)
  384.         {
  385.             this.plugin = plugin;
  386.             this.hostname = hostname;
  387.             this.port = port;
  388.             this.database = database;
  389.             this.user = username;
  390.             this.password = password;
  391.             this.connection = null;
  392.         }
  393.        
  394.         public boolean checkConnection() {
  395.             return this.connection != null;
  396.         }
  397.        
  398.         public void closeConnection() {
  399.             if (this.connection != null)
  400.                 try
  401.                 {
  402.                     this.connection.close();
  403.                 }
  404.                 catch (SQLException e)
  405.                 {
  406.                     this.plugin.getLogger().log(Level.SEVERE, "Error closing the MySQL Connection!");
  407.                     e.printStackTrace();
  408.                 }
  409.         }
  410.        
  411.         public Connection getConnection() {
  412.             return this.connection;
  413.         }
  414.        
  415.         public Connection openConnection() {
  416.             try
  417.             {
  418.                 Class.forName("com.mysql.jdbc.Driver");
  419.                 this.connection = DriverManager.getConnection("jdbc:mysql://" + this.hostname + ":" + this.port + "/" + this.database, this.user, this.password);
  420.             }
  421.             catch (SQLException e)
  422.             {
  423.                 this.plugin.getLogger().log(Level.SEVERE, "Could not connect to MySQL server! because: " + e.getMessage());
  424.             }
  425.             catch (ClassNotFoundException e)
  426.             {
  427.                 this.plugin.getLogger().log(Level.SEVERE, "JDBC Driver not found!");
  428.             }
  429.             return this.connection;
  430.         }
  431.        
  432.         public ResultSet querySQL(String query) {
  433.             Connection c = null;
  434.            
  435.             if (checkConnection())
  436.                 c = getConnection();
  437.             else
  438.                 c = openConnection();
  439.            
  440.             Statement s = null;
  441.            
  442.             try
  443.             {
  444.                 s = c.createStatement();
  445.             }
  446.             catch (SQLException e1)
  447.             {
  448.                 e1.printStackTrace();
  449.             }
  450.            
  451.             ResultSet ret = null;
  452.            
  453.             try
  454.             {
  455.                 ret = s.executeQuery(query);
  456.             }
  457.             catch (SQLException e)
  458.             {
  459.                 e.printStackTrace();
  460.             }
  461.            
  462.             closeConnection();
  463.            
  464.             return ret;
  465.         }
  466.        
  467.         public void updateSQL(String update) {
  468.            
  469.             Connection c = null;
  470.            
  471.             if (checkConnection())
  472.                 c = getConnection();
  473.             else
  474.                 c = openConnection();
  475.            
  476.             Statement s = null;
  477.            
  478.             try
  479.             {
  480.                 s = c.createStatement();
  481.                 s.executeUpdate(update);
  482.             }
  483.             catch (SQLException e1)
  484.             {
  485.                 e1.printStackTrace();
  486.             }
  487.            
  488.             closeConnection();
  489.            
  490.         }
  491.     }
  492. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement