Advertisement
Guest User

Untitled

a guest
May 3rd, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.04 KB | None | 0 0
  1. package net.ulmc.UnleashedTokensAPI.util;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.sql.Statement;
  9. import java.util.UUID;
  10.  
  11. import net.ulmc.UnleashedTokensAPI.Main;
  12.  
  13. public class SQLUtil {
  14.     private static Connection connection;
  15.    
  16.     public static void openConnection() {
  17.         try {
  18.             connection = DriverManager.getConnection("jdbc:mysql://" + Main.getPlugin().getConfig().getString("mysql.address") + ":" + Integer.toString(Main.getPlugin().getConfig().getInt("port")) + "/" + Main.getPlugin().getConfig().getString("mysql.database"), Main.getPlugin().getConfig().getString("mysql.username"), Main.getPlugin().getConfig().getString("mysql.password"));
  19.         } catch (Exception e) {
  20.             e.printStackTrace();
  21.         }
  22.     }
  23.    
  24.     public static void closeConnection() {
  25.         try {
  26.             if (!connection.isClosed()) {
  27.                 connection.close();
  28.             }
  29.         } catch (SQLException e) {
  30.             e.printStackTrace();
  31.         }
  32.     }
  33.    
  34.     public static void checkTables() {
  35.         try {
  36.             if (connection.isClosed()) {
  37.                 openConnection();
  38.             }
  39.            
  40.             Statement s = connection.createStatement();
  41.            
  42.             s.execute("CREATE TABLE IF NOT EXISTS tokens (id VARCHAR(32) PRIMARY KEY, tokens INTEGER NOT NULL);");
  43.            
  44.             s.close();
  45.         } catch (SQLException e) {
  46.             e.printStackTrace();
  47.         }
  48.     }
  49.    
  50.     public static void insertPlayer(UUID uuid) {
  51.         try {
  52.             if (connection.isClosed()) {
  53.                 openConnection();
  54.             }
  55.            
  56.             Statement s = connection.createStatement();
  57.            
  58.             s.executeUpdate("INSERT INTO tokens (id,tokens) VALUES ('" + uuid.toString() + "', 0);");
  59.            
  60.             s.close();
  61.         } catch (Exception e) {
  62.             e.printStackTrace();
  63.         }
  64.     }
  65.    
  66.     public static boolean containsPlayer(UUID uuid) {
  67.         try {
  68.             if (connection.isClosed()) {
  69.                 openConnection();
  70.             }
  71.            
  72.             PreparedStatement ps = connection.prepareStatement("SELECT * FROM tokens WHERE uuid=?;");
  73.             ps.setString(1, uuid.toString());
  74.             ResultSet rs = ps.executeQuery();
  75.             boolean cp = rs.next();
  76.            
  77.             ps.close();
  78.             rs.close();
  79.            
  80.             return cp;
  81.         } catch (Exception e) {
  82.             e.printStackTrace();
  83.             return false;
  84.         }
  85.     }
  86.    
  87.     public static int getTokens(UUID uuid) {
  88.         try {
  89.             if (connection.isClosed()) {
  90.                 openConnection();
  91.             }
  92.            
  93.             int t;
  94.            
  95.             if (containsPlayer(uuid)) {
  96.                 Statement s = connection.createStatement();
  97.                 ResultSet rs = s.executeQuery("SELECT * FROM tokens WHERE id=" + uuid.toString());
  98.                
  99.                 t = rs.getInt("tokens");
  100.                
  101.                 rs.close();
  102.                 s.close();
  103.                
  104.                 return t;
  105.             }
  106.         } catch (Exception e) {
  107.             e.printStackTrace();
  108.         }
  109.        
  110.         return -1;
  111.     }
  112.    
  113.     public static void setTokens(UUID uuid, int tokens) {
  114.         try {
  115.             if (connection.isClosed()) {
  116.                 openConnection();
  117.             }
  118.            
  119.             if (containsPlayer(uuid)) {
  120.                 Statement s = connection.createStatement();
  121.                
  122.                 s.executeUpdate("UPDATE tokens SET tokens=" + tokens + " WHERE id='" + uuid.toString() + "';");
  123.                
  124.                 s.close();
  125.             }
  126.         } catch (Exception e) {
  127.             e.printStackTrace();
  128.         }
  129.     }
  130.    
  131.     public static void addTokens(UUID uuid, int tokens) {
  132.         try {
  133.             if (connection.isClosed()) {
  134.                 openConnection();
  135.             }
  136.            
  137.             if (containsPlayer(uuid)) {
  138.                 Statement s = connection.createStatement();
  139.                
  140.                 s.executeUpdate("UPDATE tokens SET tokens=tokens+" + tokens + " WHERE id='" + uuid.toString() + "';");
  141.                
  142.                 s.close();
  143.             }
  144.         } catch (Exception e) {
  145.             e.printStackTrace();
  146.         }
  147.     }
  148.    
  149.     public static void deductTokens(UUID uuid, int tokens) {
  150.         try {
  151.             if (connection.isClosed()) {
  152.                 openConnection();
  153.             }
  154.            
  155.             if (containsPlayer(uuid)) {
  156.                 Statement s = connection.createStatement();
  157.                
  158.                 s.executeUpdate("UPDATE tokens SET tokens=tokens-" + tokens + " WHERE id='" + uuid.toString() + "';");
  159.                
  160.                 s.close();
  161.             }
  162.         } catch (Exception e) {
  163.             e.printStackTrace();
  164.         }
  165.     }
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement