Guest User

mysqlSetterGetter.java

a guest
Nov 12th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.58 KB | None | 0 0
  1. package com.starfluxgames.playtime;
  2.  
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.util.ArrayList;
  7. import java.util.List;
  8. import java.util.UUID;
  9.  
  10. import org.bukkit.entity.Player;
  11. import org.bukkit.event.Listener;
  12. public class mysqlSetterGetter implements Listener{
  13.  
  14.     static Main plugin = Main.getPlugin(Main.class);
  15.    
  16.     public static boolean isPlayerInDB(UUID uuid)
  17.     {
  18.         try {
  19.             PreparedStatement statement = plugin.getConnection().prepareStatement("SELECT * FROM `stats` WHERE uuid='" + uuid.toString() + "'"); //Setting command
  20.             ResultSet results = statement.executeQuery(); //Executing command
  21.             if (results.next())
  22.             {
  23.                 return true;
  24.             }
  25.             return false;
  26.         }catch (SQLException e) {
  27.             e.printStackTrace();
  28.         }
  29.        
  30.         return false;
  31.     }
  32.     public static boolean isUsernameInDB(String username)
  33.     {
  34.         try {
  35.             PreparedStatement statement = plugin.getConnection().prepareStatement("SELECT * FROM `stats` WHERE username='" + username + "'"); //Setting command
  36.             ResultSet results = statement.executeQuery(); //Executing command
  37.             if (results.next())
  38.             {
  39.                 return true;
  40.             }
  41.             return false;
  42.         }catch (SQLException e) {
  43.             e.printStackTrace();
  44.         }
  45.        
  46.         return false;
  47.     }
  48.    
  49.     public static String getUUIDfromName(String username){
  50.         try {
  51.             PreparedStatement statement = plugin.getConnection().prepareStatement("SELECT * FROM `stats` WHERE username='" + username + "'");
  52.             ResultSet results = statement.executeQuery(); //Executing command
  53.             if (results.next())
  54.             {
  55.                 return results.getString("uuid");
  56.             }
  57.             return null;
  58.         }catch (SQLException e) {
  59.             e.printStackTrace();
  60.         }
  61.        
  62.         return null;
  63.     }
  64.    
  65.     public static void setupPlayer(final UUID uuid, Player player)
  66.     {
  67.         try
  68.         {
  69.             if (!isPlayerInDB(uuid))
  70.             {
  71.                 PreparedStatement insert = plugin.getConnection().prepareStatement("INSERT INTO `stats`(`uuid`, `username`, `firstJoin`, `lastSeen`, `global`, `lobby`, `creative`) VALUES ('" + uuid + "','" + player.getName() + "'," + System.currentTimeMillis() + ",0,0,0,0)");
  72.                 insert.executeUpdate();
  73.             }
  74.         }catch (SQLException e) {
  75.             e.printStackTrace();
  76.         }
  77.     }
  78.    
  79.     public static void addPlaytime(final UUID uuid, long time, String server)
  80.     {
  81.         try
  82.         {
  83.             PreparedStatement insert = plugin.getConnection().prepareStatement("UPDATE `stats` SET `global`=`global` + '" + time + "', `" + server + "` = `" + server + "`+'" + time + "' WHERE uuid = '" + uuid + "'");
  84.             insert.executeUpdate();
  85.         }catch (SQLException e) {
  86.             e.printStackTrace();
  87.         }
  88.     }
  89.    
  90.     public static void setLastSeen(UUID uuid, long time)
  91.     {
  92.         try
  93.         {
  94.             PreparedStatement insert = plugin.getConnection().prepareStatement("UPDATE `stats` SET `lastSeen`='" + time + "' WHERE uuid='" + uuid + "'");
  95.             insert.executeUpdate();
  96.         }catch (SQLException e) {
  97.             e.printStackTrace();
  98.         }
  99.     }
  100.    
  101.     public static Long getplaytime(UUID uuid){
  102.         try {
  103.             PreparedStatement statement = plugin.getConnection().prepareStatement("SELECT * FROM `stats` WHERE uuid='" + uuid + "'");
  104.             ResultSet results = statement.executeQuery(); //Executing command
  105.             if (results.next())
  106.             {
  107.                 return results.getLong("global");
  108.             }
  109.             return null;
  110.         }catch (SQLException e) {
  111.             e.printStackTrace();
  112.         }
  113.        
  114.         return null;
  115.     }
  116.    
  117.     public static Long getLastSeen(UUID uuid){
  118.         try {
  119.             PreparedStatement statement = plugin.getConnection().prepareStatement("SELECT * FROM `stats` WHERE uuid='" + uuid + "'");
  120.             ResultSet results = statement.executeQuery(); //Executing command
  121.             if (results.next())
  122.             {
  123.                 return results.getLong("lastSeen");
  124.             }
  125.             return null;
  126.         }catch (SQLException e) {
  127.             e.printStackTrace();
  128.         }
  129.        
  130.         return null;
  131.     }
  132.    
  133.     public static Long getFirstJoin(UUID uuid)
  134.     {
  135.         try {
  136.             PreparedStatement statement = plugin.getConnection().prepareStatement("SELECT * FROM `stats` WHERE uuid='" + uuid + "'");
  137.             ResultSet results = statement.executeQuery(); //Executing command
  138.             if (results.next())
  139.             {
  140.                 return results.getLong("firstJoin");
  141.             }
  142.             return null;
  143.         }catch (SQLException e) {
  144.             e.printStackTrace();
  145.         }
  146.        
  147.         return null;
  148.     }
  149.    
  150.     public static int playerGlobalCoins(UUID uuid){
  151.         try {
  152.             PreparedStatement statement = plugin.getConnection().prepareStatement("SELECT globalCoins FROM `players` WHERE uuid='" + uuid.toString() + "'"); //Setting command
  153.             ResultSet results = statement.executeQuery(); //Executing command
  154.             if (results.next())
  155.             {
  156.                 return results.getInt("globalCoins");
  157.             }
  158.             return -1;
  159.         }catch (SQLException e) {
  160.             e.printStackTrace();
  161.         }
  162.        
  163.         return -1;
  164.     }
  165.  
  166. }
Add Comment
Please, Sign In to add comment