Advertisement
Guest User

Untitled

a guest
Aug 27th, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.04 KB | None | 0 0
  1. package de.julian.holostats.mysql;
  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.  
  10. import org.bukkit.Bukkit;
  11.  
  12. import de.julian.holostats.main.Main;
  13.  
  14. public class Mysql {
  15.     public String password = "";
  16.     public String database = "";
  17.     public String user = "";
  18.     public String host = "";
  19.     public static Connection con;
  20.  
  21. //  public Mysql(String password, String database, String user, String host)
  22. //  {
  23. //      this.password = password;
  24. //      this.database = database;
  25. //      this.user = user;
  26. //      this.host = host;
  27. //      connect();
  28. //  }
  29.    
  30.     public Mysql(String password, String database, String user, String host)
  31.     {
  32.         this.password = "";
  33.         this.database = "minecraft_knockffa";
  34.         this.user = "Julian";
  35.         this.host = "localhost";
  36.         connect();
  37.     }
  38.  
  39.     public void connect()
  40.     {
  41.         if (!isConnected()) {
  42.             try{
  43.                 con = DriverManager.getConnection("jdbc:mysql://" + this.host + ":3360/" + this.database, this.user, this.password);
  44.                 Bukkit.getConsoleSender().sendMessage(Main.getPrefix() + "§aErfolgreich aufgebaut");
  45.             }catch (SQLException ex){
  46.                 ex.printStackTrace();
  47.             }
  48.         }
  49.     }
  50.  
  51.     public void disconnect()
  52.     {
  53.         if (isConnected()) {
  54.             try
  55.             {
  56.                 con.close();
  57.                 System.out.println("[MYSQL] Disonnected!");
  58.             }
  59.             catch (SQLException e)
  60.             {
  61.                 e.printStackTrace();
  62.             }
  63.         }
  64.     }
  65.  
  66.     public boolean isConnected()
  67.     {
  68.         return con != null;
  69.     }
  70.  
  71.     public void update(String qry)
  72.     {
  73.         try
  74.         {
  75.             PreparedStatement ps = con.prepareStatement(qry);
  76.             ps.executeUpdate();
  77.         }
  78.         catch (SQLException e)
  79.         {
  80.             e.printStackTrace();
  81.         }
  82.     }
  83.  
  84.     public ResultSet query(String qry)
  85.     {
  86.         ResultSet rs = null;
  87.         try
  88.         {
  89.             Statement st = con.createStatement();
  90.             rs = st.executeQuery(qry);
  91.         }
  92.         catch (SQLException e)
  93.         {
  94.             connect();
  95.             System.err.println(e);
  96.         }
  97.         return rs;
  98.     }
  99.  
  100.     public ResultSet getResult(String qry)
  101.     {
  102.         try
  103.         {
  104.             PreparedStatement ps = con.prepareStatement(qry);
  105.             return ps.executeQuery();
  106.         }
  107.         catch (SQLException e)
  108.         {
  109.             e.printStackTrace();
  110.         }
  111.         return null;
  112.     }
  113.  
  114.     public Connection getConnection()
  115.     {
  116.         return con;
  117.     }
  118.  
  119.     public void reconnect()
  120.     {
  121.         if (con == null) {
  122.             connect();
  123.         }
  124.     }
  125.    
  126.     public static boolean playerExists(String uuid)
  127.     {
  128.         try
  129.         {
  130.             ResultSet rs = Main.mysql.query("SELECT * FROM knockout WHERE UUID= '" + uuid + "'");
  131.             if (rs.next()) {
  132.                 return rs.getString("UUID") != null;
  133.             }
  134.             return false;
  135.         }
  136.         catch (SQLException e)
  137.         {
  138.             e.printStackTrace();
  139.         }
  140.         return false;
  141.     }
  142.  
  143.     public static Integer getKills(String uuid)
  144.     {
  145.         Integer i = Integer.valueOf(0);
  146.         if (playerExists(uuid))
  147.         {
  148.             try
  149.             {
  150.                 ResultSet rs = Main.mysql.query("SELECT * FROM knockout WHERE UUID= '" + uuid + "'");
  151.                 if (rs.next()) {
  152.                     Integer.valueOf(rs.getInt("KILLS"));
  153.                 }
  154.                 i = Integer.valueOf(rs.getInt("KILLS"));
  155.             }
  156.             catch (SQLException e)
  157.             {
  158.                 e.printStackTrace();
  159.             }
  160.         }
  161.         else
  162.         {
  163.             getKills(uuid);
  164.         }
  165.         return i;
  166.     }
  167.  
  168.  
  169.     public static Integer getDeaths(String uuid)
  170.     {
  171.         Integer i = Integer.valueOf(0);
  172.         if (playerExists(uuid))
  173.         {
  174.             try
  175.             {
  176.                 ResultSet rs = Main.mysql.query("SELECT * FROM knockout WHERE UUID= '" + uuid + "'");
  177.                 if (rs.next()) {
  178.                     Integer.valueOf(rs.getInt("DEATHS"));
  179.                 }
  180.                 i = Integer.valueOf(rs.getInt("DEATHS"));
  181.             }
  182.             catch (SQLException e)
  183.             {
  184.                 e.printStackTrace();
  185.             }
  186.         }
  187.         else
  188.         {
  189.             getDeaths(uuid);
  190.         }
  191.         return i;
  192.     }
  193.  
  194.  
  195.     public static int getRank(String uuid)
  196.     {
  197.         int rank = 0;
  198.         try
  199.         {
  200.             PreparedStatement ps = con.prepareStatement("SELECT * FROM knockout ORDER BY KILLS DESC");
  201.             ResultSet result = ps.executeQuery();
  202.             while (result.next())
  203.             {
  204.                 rank++;
  205.                 String uuid2 = result.getString("UUID");
  206.                 if (uuid2.equalsIgnoreCase(uuid)) {
  207.                     return rank;
  208.                 }
  209.             }
  210.             result.close();
  211.             ps.close();
  212.         }
  213.         catch (Exception ex)
  214.         {
  215.             ex.printStackTrace();
  216.         }
  217.         return rank;
  218.     }
  219.    
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement