SHARE
TWEET

Untitled

a guest Jul 21st, 2017 63 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.  
  11. public class SQL {
  12.  
  13.     public static Connection con = null;
  14.     public static Statement stmt;
  15.     public static boolean connectionMade;
  16.     public static void createConnection() {
  17.         try {
  18.             Class.forName("com.mysql.jdbc.Driver").newInstance();
  19.             con = DriverManager.getConnection("jdbc:mysql://67.228.181.129:3306/vainfort_highscores", "vainfort_Nite", "password");
  20.             stmt = con.createStatement();
  21.         } catch (Exception e) {
  22.             e.printStackTrace();
  23.         }
  24.     }
  25.     public static ResultSet query(String s) throws SQLException {
  26.         try {
  27.             if (s.toLowerCase().startsWith("select")) {
  28.                 ResultSet rs = stmt.executeQuery(s);
  29.                 return rs;
  30.             } else {
  31.                 stmt.executeUpdate(s);
  32.             }
  33.             return null;
  34.         } catch (Exception e) {
  35.             destroyConnection();
  36.             createConnection();
  37.             e.printStackTrace();
  38.         }
  39.         return null;
  40.     }
  41.  
  42.     public static void destroyConnection() {
  43.         try {
  44.             stmt.close();
  45.             con.close();
  46.             connectionMade = false;
  47.         } catch (Exception e) {
  48.             e.printStackTrace();
  49.         }
  50.     }
  51.  
  52.     public static void needsItems(Client c) {
  53.         try {
  54.             ResultSet results = query("SELECT * FROM `items` WHERE `username`='" + c.playerName + "'");
  55.             while (results.next()) {
  56.                 if (results.getInt("given") == 0) {
  57.                     String items = results.getString("items");
  58.                     int id = results.getInt("id");
  59.                     int quantity = results.getInt("quantity");
  60.                     if (c.getItems().freeSlots() < quantity) {
  61.                         c.sendMessage("@red@You do not have enough free slots to receive your purchased reward!");
  62.                         return;
  63.                     }
  64.                     String[] args = items.split(",");
  65.                     if (args.length > 0) {
  66.                         for (String a : args) {
  67.                             int item = Integer.parseInt(a);
  68.                             for (int b = 0; b < quantity;) {
  69.                                 c.getItems().addItem(item, 1);
  70.                                 b++;
  71.                             }
  72.                         }
  73.                         c.sendMessage("@red@Thank you for your payment. Your items are now in your inventory!");
  74.                     } else {
  75.                         int item = Integer.parseInt(args[0]);
  76.                         for (int b = 0; b < quantity;) {
  77.                             c.getItems().addItem(item, 1);
  78.                             b++;
  79.                         }
  80.                         c.sendMessage("@red@Thank you for your payment. Your item is now in your inventory!");
  81.                     }
  82.  
  83.                     query("UPDATE `items` SET `given`='1' WHERE `id`='" + id + "'");
  84.                 }
  85.             }
  86.         } catch (Exception e) {
  87.             e.printStackTrace();
  88.         }
  89.     }  
  90.  
  91.    
  92.     public static boolean saveHighScore(Client clientToSave) {
  93.         try {
  94.             query("DELETE FROM `skills` WHERE playerName = '" + clientToSave.playerName + "';");
  95.             query("DELETE FROM `skillsoverall` WHERE playerName = '" + clientToSave.playerName + "';");
  96.             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] + ","
  97.                     + 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]
  98.                     + "," + clientToSave.playerLevel[19] + "," + clientToSave.playerXP[19] + "," + clientToSave.playerLevel[20] + "," + clientToSave.playerXP[20] + ");");
  99.             query("INSERT INTO `skillsoverall` (`playerName`,`lvl`,`xp`) VALUES ('"
  100.                     + clientToSave.playerName
  101.                     + "',"
  102.                     + (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])
  103.                             + 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])) + ");");
  104.         } catch (Exception e) {
  105.             e.printStackTrace();
  106.             return false;
  107.         }
  108.         return true;
  109.     }
  110. }
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