SHARE
TWEET

Untitled

a guest Jun 15th, 2017 58 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package server.util;
  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. import server.model.players.Client;
  10. import server.Config;
  11.  
  12. /**
  13.  * HiscoreManager Class
  14.  * @author Ian
  15.  *
  16.  */
  17.  
  18. public class HiscoreManager implements Runnable {
  19.  
  20.     /** MySQL Connection */
  21.     private static Connection conn = null;
  22.     private static Statement statement = null;
  23.     private static ResultSet results = null;
  24.     private static Thread thread = null;
  25.    
  26.     public static boolean databaseRunning = true;
  27.     public static boolean databaseConnected = false;
  28.    
  29.     public HiscoreManager() {
  30.         try {
  31.             databaseRunning = true;
  32.             databaseConnected = false;
  33.             conn = null;
  34.             thread = new Thread(this);
  35.             thread.start();
  36.         } catch(Exception e) {
  37.         }
  38.     }
  39.    
  40.     /**
  41.      * Creates a Connection to the MySQL Database
  42.      */
  43.     public static void createConnection() {
  44.         try {
  45.             if(!databaseConnected && databaseRunning) {
  46.                 Class.forName("com.mysql.jdbc.Driver").newInstance();
  47.                 conn = DriverManager.getConnection("jdbc:mysql://HOSTIP/DATABASE_NAME", "DATABASE_USER", "USER_PASSWORD");
  48.                 statement = conn.createStatement();
  49.                 if(!databaseConnected)
  50.                     System.out.println("Connected to MYSQL hiscore database...");
  51.                 databaseConnected = true;
  52.                 databaseRunning = true;
  53.             }
  54.         } catch (Exception e) {
  55.             disableDatabase();
  56.         }
  57.     }  
  58.    
  59.     public static boolean testConnect() {
  60.         boolean connected = false;
  61.         try {
  62.             Class.forName("com.mysql.jdbc.Driver").newInstance();
  63.             conn = DriverManager.getConnection("jdbc:mysql://HOSTIP/DATABASE_NAME", "DATABASE_USER", "USER_PASSWORD");
  64.             statement = conn.createStatement();
  65.             connected = true;
  66.         } catch(Exception e) {
  67.         }
  68.         if(connected) {
  69.             statement = null;
  70.             conn = null;
  71.             return true;
  72.         }
  73.         return false;
  74.     }
  75.            
  76.     private static void destroyConnection() {
  77.         try {
  78.             statement.close();
  79.             conn.close();
  80.         } catch (Exception e) {
  81.             disableDatabase();
  82.         }
  83.     }
  84.    
  85.     private static ResultSet query(String s) throws SQLException {
  86.         try {
  87.             if (s.toLowerCase().startsWith("select")) {
  88.                 ResultSet rs = statement.executeQuery(s);
  89.                 return rs;
  90.             } else {
  91.                 statement.executeUpdate(s);
  92.             }
  93.             return null;
  94.         } catch (Exception e) {
  95.             destroyConnection();
  96.         }
  97.         return null;
  98.     }
  99.    
  100.     public static void disableDatabase() {
  101.         boolean connect = databaseConnected;
  102.         boolean run = databaseRunning;
  103.         if(run) {
  104.             System.out.println("Disconnected from MYSQL hiscore database...");
  105.             databaseRunning = false;
  106.             if(connect)
  107.                 databaseConnected = false;
  108.         }
  109.     }
  110.    
  111.     public synchronized static boolean saveHiscores(Client clientToSave) {
  112.         if(databaseRunning) {
  113.             try {
  114.                 createConnection();
  115.                 query("DELETE FROM `skills` WHERE playerName = '"+clientToSave.playerName+"';");
  116.                 query("DELETE FROM `skillsoverall` WHERE playerName = '"+clientToSave.playerName+"';");
  117.                 query("INSERT INTO `skills` (`playerName`,`Attacklvl`,`Attackxp`,`Defencelvl`,`Defencexp`,`Strengthlvl`,`Strengthxp`,`Hitpointslvl`,`Hitpointsxp`,`Rangelvl`,`Rangexp`,`Prayerlvl`,`Prayerxp`,`Magiclvl`,`Magicxp`,`Cookinglvl`,`Cookingxp`,`Woodcuttinglvl`,`Woodcuttingxp`,`Fletchinglvl`,`Fletchingxp`,`Fishinglvl`,`Fishingxp`,`Firemakinglvl`,`Firemakingxp`,`Craftinglvl`,`Craftingxp`,`Smithinglvl`,`Smithingxp`,`Mininglvl`,`Miningxp`,`Herblorelvl`,`Herblorexp`,`Agilitylvl`,`Agilityxp`,`Thievinglvl`,`Thievingxp`,`Slayerlvl`,`Slayerxp`,`Farminglvl`,`Farmingxp`,`Runecraftlvl`,`Runecraftxp`) VALUES ('"+clientToSave.playerName+"',"+clientToSave.playerLevel[0]+","+clientToSave.playerXP[0]+","+clientToSave.playerLevel[1]+","+clientToSave.playerXP[1]+","+clientToSave.playerLevel[2]+","+clientToSave.playerXP[2]+","+clientToSave.playerLevel[3]+","+clientToSave.playerXP[3]+","+clientToSave.playerLevel[4]+","+clientToSave.playerXP[4]+","+clientToSave.playerLevel[5]+","+clientToSave.playerXP[5]+","+clientToSave.playerLevel[6]+","+clientToSave.playerXP[6]+","+clientToSave.playerLevel[7]+","+clientToSave.playerXP[7]+","+clientToSave.playerLevel[8]+","+clientToSave.playerXP[8]+","+clientToSave.playerLevel[9]+","+clientToSave.playerXP[9]+","+clientToSave.playerLevel[10]+","+clientToSave.playerXP[10]+","+clientToSave.playerLevel[11]+","+clientToSave.playerXP[11]+","+clientToSave.playerLevel[12]+","+clientToSave.playerXP[12]+","+clientToSave.playerLevel[13]+","+clientToSave.playerXP[13]+","+clientToSave.playerLevel[14]+","+clientToSave.playerXP[14]+","+clientToSave.playerLevel[15]+","+clientToSave.playerXP[15]+","+clientToSave.playerLevel[16]+","+clientToSave.playerXP[16]+","+clientToSave.playerLevel[17]+","+clientToSave.playerXP[17]+","+clientToSave.playerLevel[18]+","+clientToSave.playerXP[18]+","+clientToSave.playerLevel[19]+","+clientToSave.playerXP[19]+","+clientToSave.playerLevel[20]+","+clientToSave.playerXP[20]+");");
  118.                 query("INSERT INTO `skillsoverall` (`playerName`,`lvl`,`xp`) VALUES ('"+clientToSave.playerName+"',"+(clientToSave.getPA().getTotalLevel())+","+((clientToSave.getPA().getTotalXp()))+");");
  119.                 destroyConnection();
  120.                 return true;
  121.             } catch (Exception e) {
  122.                 return false;
  123.             }
  124.         }
  125.         return false;
  126.     }
  127.    
  128.     public void run() {
  129.         boolean run = Config.HISCORE_MYSQL;
  130.         while(run) {
  131.             try {
  132.                 if(conn == null && databaseRunning) {
  133.                     databaseConnected = false;
  134.                     createConnection();
  135.                 }
  136.                 if(!databaseRunning && testConnect()) {
  137.                     databaseRunning = true;
  138.                     createConnection();
  139.                 }
  140.                 Thread.sleep(10000);
  141.             } catch(Exception e) {
  142.                 e.printStackTrace();
  143.             }
  144.         }
  145.     }
  146.    
  147. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top