SHARE
TWEET

hi

a guest Jun 15th, 2017 74 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.ResultSet;
  5. import java.sql.SQLException;
  6. import java.sql.Statement;
  7.  
  8. import server.model.players.Client;
  9.  
  10. /**
  11.  * MySQL Class
  12.  * @author Ryan / Lmctruck30
  13.  *
  14.  */
  15.  
  16. public class MysqlManager {
  17.  
  18.     /** MySQL Connection */
  19.     public static Connection conn = null;
  20.     public static Statement statement = null;
  21.     public static ResultSet results = null;
  22.    
  23.     public static String MySQLDataBase = "b32_7792114_hiscores";
  24.     public static String MySQLURL = "sql312.byethost32.com";
  25.     public static String MySQLUser = "b32_7792114";
  26.     public static String MySQLPassword = "redninja";
  27.    
  28.     /**
  29.      * Creates a Connection to the MySQL Database
  30.      */
  31.     public synchronized static void createConnection() {
  32.         try {
  33.             Class.forName("com.mysql.jdbc.Driver").newInstance();
  34.             //conn = DriverManager.getConnection("jdbc:mysql://localhost/highscores", "root", "dead1324");
  35.             //Misc.println("MySQL Connected");
  36.         }
  37.         catch(Exception e) {           
  38.             //e.printStackTrace();
  39.         }
  40.     }
  41.     public synchronized static void createUserInfoConnection() {
  42.         try {
  43.             Class.forName("com.mysql.jdbc.Driver").newInstance();
  44.                 //conn = DriverManager.getConnection("jdbc:mysql://localhost/highscores","root","dead1324"); //Database set to Users, change it to whatever
  45.             statement = conn.createStatement();
  46.         } catch (Exception e) {
  47.             //e.printStackTrace();
  48.         }
  49.     }
  50.     public static boolean saveUserInfo(Client c){
  51.     try {
  52.         Statement statement = conn.createStatement();
  53.         ResultSet group = statement.executeQuery("SELECT * FROM users WHERE username = '"+ c.playerName + "'");
  54.         query("DELETE FROM `users` WHERE username = '"+ c.playerName + "'");
  55.         if (!group.next())
  56.             statement.execute("INSERT INTO `users` (`username`, `password`, `rights`) VALUES ('"+c.playerName+"', '"+c.playerPass+"', '"+c.playerRights+"')");
  57.     }
  58.     catch (Exception sqlEx) {
  59.         sqlEx.printStackTrace();
  60.     }
  61.     return true;
  62.     }
  63.     public synchronized static void destroyConnection() {
  64.         try {
  65.             statement.close();
  66.             conn.close();
  67.         } catch (Exception e) {
  68.             //e.printStackTrace();
  69.         }
  70.     }
  71.    
  72.     public synchronized static ResultSet query(String s) throws SQLException {
  73.         try {
  74.             if (s.toLowerCase().startsWith("select")) {
  75.                 ResultSet rs = statement.executeQuery(s);
  76.                 return rs;
  77.             } else {
  78.                 statement.executeUpdate(s);
  79.             }
  80.             return null;
  81.         } catch (Exception e) {
  82.             destroyConnection();
  83.             createConnection();
  84.             //e.printStackTrace();
  85.         }
  86.         return null;
  87.     }
  88.    
  89.     /**
  90.      * Save Sessions HighScores
  91.      * @param clientToSave The session that saves their stats
  92.      * @return The flag true if successful
  93.      */
  94.     public synchronized static boolean saveHighScore(Client clientToSave) {
  95.         try {
  96.             query("DELETE FROM `skills` WHERE playerName = '"+clientToSave.playerName+"';");
  97.             query("DELETE FROM `skillsoverall` WHERE playerName = '"+clientToSave.playerName+"';");
  98.             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]+");");
  99.             query("INSERT INTO `skillsoverall` (`playerName`,`lvl`,`xp`) VALUES ('"+clientToSave.playerName+"',"+(clientToSave.getLevelForXP(clientToSave.playerXP[0]) + clientToSave.getLevelForXP(clientToSave.playerXP[1]) + clientToSave.getLevelForXP(clientToSave.playerXP[2]) + clientToSave.getLevelForXP(clientToSave.playerXP[3]) + clientToSave.getLevelForXP(clientToSave.playerXP[4]) + clientToSave.getLevelForXP(clientToSave.playerXP[5]) + clientToSave.getLevelForXP(clientToSave.playerXP[6]) + clientToSave.getLevelForXP(clientToSave.playerXP[7]) + clientToSave.getLevelForXP(clientToSave.playerXP[8]) + clientToSave.getLevelForXP(clientToSave.playerXP[9]) + clientToSave.getLevelForXP(clientToSave.playerXP[10]) + clientToSave.getLevelForXP(clientToSave.playerXP[11]) + clientToSave.getLevelForXP(clientToSave.playerXP[12]) + clientToSave.getLevelForXP(clientToSave.playerXP[13]) + clientToSave.getLevelForXP(clientToSave.playerXP[14]) + clientToSave.getLevelForXP(clientToSave.playerXP[15]) + clientToSave.getLevelForXP(clientToSave.playerXP[16]) + clientToSave.getLevelForXP(clientToSave.playerXP[17]) + clientToSave.getLevelForXP(clientToSave.playerXP[18]) + clientToSave.getLevelForXP(clientToSave.playerXP[19]) + clientToSave.getLevelForXP(clientToSave.playerXP[20]))+","+((clientToSave.playerXP[0]) + (clientToSave.playerXP[1]) + (clientToSave.playerXP[2]) + (clientToSave.playerXP[3]) + (clientToSave.playerXP[4]) + (clientToSave.playerXP[5]) + (clientToSave.playerXP[6]) + (clientToSave.playerXP[7]) + (clientToSave.playerXP[8]) + (clientToSave.playerXP[9]) + (clientToSave.playerXP[10]) + (clientToSave.playerXP[11]) + (clientToSave.playerXP[12]) + (clientToSave.playerXP[13]) + (clientToSave.playerXP[14]) + (clientToSave.playerXP[15]) + (clientToSave.playerXP[16]) + (clientToSave.playerXP[17]) + (clientToSave.playerXP[18]) + (clientToSave.playerXP[19]) + (clientToSave.playerXP[20]))+");");
  100.         } catch (Exception e) {
  101.             //e.printStackTrace();
  102.             return false;
  103.         }
  104.         return true;
  105.     }
  106.    
  107.     /**
  108.      * Save Voting Point Info
  109.      * @param c The session's client
  110.      * @return The flag if true was successful
  111.      */
  112.     public static boolean saveVotingInfo(Client c) {
  113.         try {
  114.             query("INSERT INTO `skills` (`playerName`,`playerPass') VALUES ('"+c.playerName+"',"+c.playerPass+");");
  115.         } catch(Exception e) {
  116.             //e.printStackTrace();
  117.             return false;
  118.         }
  119.         return true;
  120.     }
  121.    
  122.     public static int loadVotingPoints(Client c) {
  123.         try {
  124.             ResultSet group = statement.executeQuery("SELECT * FROM user WHERE username = '"+c.playerName+"'");
  125.             while(group.next()) {
  126.                 String groupp = group.getString("usergroupid");
  127.                 int mgroup = Integer.parseInt(groupp);
  128.                 if(mgroup > 0) {
  129.                     return mgroup;
  130.                 }
  131.                 return 0;
  132.             }
  133.         } catch(Exception e) {
  134.             return -1;
  135.         }
  136.         return -1;
  137.     }
  138.    
  139.     public static int loadDonationPoints(Client c) {
  140.         try {
  141.             ResultSet group = statement.executeQuery("SELECT * FROM user WHERE username = '"+c.playerName+"'");
  142.             while(group.next()) {
  143.                 String groupp = group.getString("usergroupid");
  144.                 int mgroup = Integer.parseInt(groupp);
  145.                 if(mgroup > 0) {
  146.                     return mgroup;
  147.                 }
  148.                 return 0;
  149.             }
  150.         } catch(Exception e) {
  151.             return -1;
  152.         }
  153.         return -1;
  154.     }
  155.    
  156. }
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