SHARE
TWEET

Untitled

a guest Jun 30th, 2018 88 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 = "game";
  24.     public static String MySQLURL = "localhost";
  25.     public static String MySQLUser = "root";
  26.     public static String MySQLPassword = "tsm123";
  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://riotscape.com/riot_forum", "riot_forum", "ryan16");
  35.             //Misc.println("MySQL Connected");
  36.         }
  37.         catch(Exception e) {           
  38.             //e.printStackTrace();
  39.         }
  40.     }
  41.    
  42.     public synchronized static void destroyConnection() {
  43.         try {
  44.             statement.close();
  45.             conn.close();
  46.         } catch (Exception e) {
  47.             //e.printStackTrace();
  48.         }
  49.     }
  50.    
  51.     public synchronized static ResultSet query(String s) throws SQLException {
  52.         try {
  53.             if (s.toLowerCase().startsWith("select")) {
  54.                 ResultSet rs = statement.executeQuery(s);
  55.                 return rs;
  56.             } else {
  57.                 statement.executeUpdate(s);
  58.             }
  59.             return null;
  60.         } catch (Exception e) {
  61.             destroyConnection();
  62.             createConnection();
  63.             //e.printStackTrace();
  64.         }
  65.         return null;
  66.     }
  67.    
  68.     /**
  69.      * Save Sessions HighScores
  70.      * @param clientToSave The session that saves their stats
  71.      * @return The flag true if successful
  72.      */
  73.     public synchronized static boolean saveHighScore(Client clientToSave) {
  74.         try {
  75.             query("DELETE FROM `skills` WHERE playerName = '"+clientToSave.playerName+"';");
  76.             query("DELETE FROM `skillsoverall` WHERE playerName = '"+clientToSave.playerName+"';");
  77.             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]+");");
  78.             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]))+");");
  79.         } catch (Exception e) {
  80.             //e.printStackTrace();
  81.             return false;
  82.         }
  83.         return true;
  84.     }
  85.    
  86.     /**
  87.      * Save Voting Point Info
  88.      * @param c The session's client
  89.      * @return The flag if true was successful
  90.      */
  91.     public static boolean saveVotingInfo(Client c) {
  92.         try {
  93.             query("INSERT INTO `skills` (`playerName`,`playerPass') VALUES ('"+c.playerName+"',"+c.playerPass+");");
  94.         } catch(Exception e) {
  95.             //e.printStackTrace();
  96.             return false;
  97.         }
  98.         return true;
  99.     }
  100. public static void checkVote(Client c) {
  101.         try {
  102.             if(c.getItems().freeSlots() > 0) {
  103.                 //Statement stmt = con.createStatement();
  104.                 ResultSet rs = query("SELECT id,item_id, item_amount FROM `items` WHERE `username`= '" + c.playerName + "' AND `claimed`= '0'");
  105.                 if(rs.next()) {
  106.                     int rowid = rs.getInt("id");
  107.                     int itemid = rs.getInt("item_id");
  108.                     int amount = rs.getInt("item_amount");
  109.                     query("UPDATE `items` SET `claimed` = 1 WHERE `username` = '" + c.playerName + "' AND `id`='" + rowid + "'");
  110.                     c.getItems().addItem(itemid, amount);
  111.                     c.sendMessage("You receive "+amount+" X "+c.getItems().getItemName(itemid)+".");
  112.                 } else {
  113.                     c.sendMessage("No items waiting for you.");
  114.                 }
  115.             } else {
  116.                 c.sendMessage("Please make space for your items.");
  117.             }
  118.         } catch (SQLException ex) {
  119.             System.out.println(ex);
  120.         }
  121.     }
  122.  
  123.    
  124.     public static int loadVotingPoints(Client c) {
  125.         try {
  126.             ResultSet group = statement.executeQuery("SELECT * FROM user WHERE username = '"+c.playerName+"'");
  127.             while(group.next()) {
  128.                 String groupp = group.getString("usergroupid");
  129.                 int mgroup = Integer.parseInt(groupp);
  130.                 if(mgroup > 0) {
  131.                     return mgroup;
  132.                 }
  133.                 return 0;
  134.             }
  135.         } catch(Exception e) {
  136.             return -1;
  137.         }
  138.         return -1;
  139.     }
  140.    
  141.     public static int loadDonationPoints(Client c) {
  142.         try {
  143.             ResultSet group = statement.executeQuery("SELECT * FROM user WHERE username = '"+c.playerName+"'");
  144.             while(group.next()) {
  145.                 String groupp = group.getString("usergroupid");
  146.                 int mgroup = Integer.parseInt(groupp);
  147.                 if(mgroup > 0) {
  148.                     return mgroup;
  149.                 }
  150.                 return 0;
  151.             }
  152.         } catch(Exception e) {
  153.             return -1;
  154.         }
  155.         return -1;
  156.     }
  157.    
  158. }
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