Advertisement
xCraftRayX

Untitled

Oct 8th, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.64 KB | None | 0 0
  1. package pl.mines.xcraftrayx.crafttabs;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8.  
  9. public class MySQL
  10. {
  11.     private static Connection conn;
  12.     private static Statement stat;
  13.    
  14.     public static String DB_URL;
  15.    
  16.     private static String DRIVER = "com.mysql.jdbc.Driver";
  17.  
  18.    
  19.     public static void connect()
  20.     {
  21.         try
  22.         {
  23.             Class.forName(DRIVER);
  24.         }
  25.         catch (ClassNotFoundException e)
  26.         {
  27.             System.err.println("Problem with MySQL Driver...");
  28.             e.printStackTrace();
  29.         }
  30.        
  31.         try
  32.         {
  33.                 DB_URL = "jdbc:mysql://" + Config.host + ":" + Config.port + "/" + Config.base + "?autoReconnect=true&user=" + Config.user + "&password=" + Config.pass;
  34.    
  35.             if(conn == null)
  36.             {
  37.                 conn = DriverManager.getConnection(DB_URL);
  38.                 stat = conn.createStatement();
  39.             }
  40.         }
  41.         catch(SQLException e)
  42.         {
  43.             System.err.println("Problem with opening connection with Database...");
  44.             e.printStackTrace();
  45.         }
  46.     }
  47.    
  48.     public static void closeConnection()
  49.     {
  50.         try
  51.         {
  52.             if(conn != null)
  53.             {
  54.                 conn.close();
  55.             }
  56.         }
  57.         catch (SQLException e)
  58.         {
  59.          System.err.println("Problem with closing connection with Database...");
  60.             e.printStackTrace();
  61.         }
  62.     }
  63.    
  64.     public static String[][] getTopFactions()
  65.     {
  66.         String[][] lista = new String[15][2];
  67.        
  68.         try
  69.         {
  70.             ResultSet result = stat.executeQuery("SELECT name, wins FROM `factionWars` ORDER BY wins, kills DESC LIMIT 15;");
  71.            
  72.             int i = 0;
  73.             while(result.next())
  74.             {
  75.                 String name = String.valueOf(result.getString("name"));
  76.                 String wins = String.valueOf(result.getInt("wins"));
  77.                    
  78.                 lista[i][0] = name;
  79.                 lista[i][1] = wins;
  80.                
  81.                 i++;
  82.             }
  83.         }
  84.         catch (SQLException e)
  85.         {
  86.             System.err.println("Problem with getting top factions stats!");
  87.             e.printStackTrace();
  88.             return null;
  89.         }
  90.         return lista;
  91.     }
  92.    
  93.     public static String[][] getTopPlayers()
  94.     {
  95.         String[][] lista = new String[15][3];
  96.        
  97.         try
  98.         {
  99.             ResultSet result = stat.executeQuery("SELECT name, kills, elo FROM pvpstats ORDER BY elo DESC LIMIT 15;");
  100.            
  101.             int i = 0;
  102.             while(result.next())
  103.             {
  104.                 String name = String.valueOf(result.getString("name"));
  105.                 String kills = String.valueOf(result.getInt("kills"));
  106.                 String elo = String.valueOf(result.getInt("elo"));
  107.                    
  108.                 lista[i][0] = name;
  109.                 lista[i][1] = kills;
  110.                 lista[i][2] = elo;
  111.                
  112.                 i++;
  113.             }
  114.         }
  115.         catch (SQLException e)
  116.         {
  117.             System.err.println("Problem with getting top users!");
  118.             e.printStackTrace();
  119.             return null;
  120.         }
  121.         return lista;
  122.     }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement