Advertisement
Guest User

Untitled

a guest
May 29th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.10 KB | None | 0 0
  1. package DavidScape.io;
  2.  
  3. import java.sql.*;
  4. import java.security.MessageDigest;
  5. import java.util.*;
  6. import java.lang.*;
  7. import DavidScape.Server;
  8. import DavidScape.Engine;
  9. import DavidScape.io.*;
  10. import DavidScape.players.Player;
  11. import DavidScape.util.Misc;
  12. import java.util.Calendar;
  13. import java.text.SimpleDateFormat;
  14.  
  15. public class SQL {
  16.  
  17.  
  18.     public static Connection con = null;
  19.     public static Statement stmt;
  20.  
  21.     public static void createConnection() {
  22.         try {
  23.             Class.forName("com.mysql.jdbc.Driver").newInstance();
  24.         String IP="localhost";
  25.             String DB="highscores";
  26.             String User="root";
  27.             String Pass="jasper15";
  28.             con = DriverManager.getConnection("jdbc:mysql://"+IP+"/"+DB, User, Pass);
  29.             stmt = con.createStatement();
  30.         } catch (Exception e) {
  31.             e.printStackTrace();
  32.         }
  33.     }
  34.     public static ResultSet query(String s) throws SQLException {
  35.         try {
  36.             if (s.toLowerCase().startsWith("select")) {
  37.                 ResultSet rs = stmt.executeQuery(s);
  38.                 Misc.println("Success");
  39.                 return rs;
  40.             } else {
  41.                 stmt.executeUpdate(s);
  42.             }
  43.             return null;
  44.         } catch (Exception e) {
  45.             destroyConnection();
  46.             createConnection();
  47.             Misc.println("Failed");
  48.             e.printStackTrace();
  49.         }
  50.         return null;
  51.     }
  52.  
  53.     public static void destroyConnection() {
  54.         try {
  55.             stmt.close();
  56.             con.close();
  57.         } catch (Exception e) {
  58.             //e.printStackTrace();
  59.         }
  60.     }
  61.       public static String now(String dateFormat) {
  62.             Calendar cal = Calendar.getInstance();
  63.         SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
  64.             return sdf.format(cal.getTime());
  65.  
  66.     }
  67.     public static void numOnline() {
  68.         int i = 0;
  69.         for(Player p : Engine.players) {
  70.             if(p != null) {
  71.                 i++;
  72.             }
  73.         }
  74.         try {
  75.             query("TRUNCATE TABLE `numonline`");
  76.             query("TRUNCATE TABLE `currentonline`");
  77.             query("INSERT INTO `numonline` (`online`) VALUES (" + i + ");");
  78.             for(Player p: Engine.players) {
  79.                 if(p != null) {
  80.                     query("INSERT INTO `currentonline` (`username`) VALUES ('"+p.username+"');");
  81.                 }
  82.             }
  83.         } catch(Exception e) {e.printStackTrace();}
  84.     }
  85.     public static boolean saveHighScore(Player p) {
  86.         try {
  87.             String timeF = "HH:mm";
  88.             String dateF = "EEE, MMM dd, yyyy";
  89.             query("DELETE FROM `skills` WHERE playerName = '"+p.username+"';");
  90.             query("DELETE FROM `skillsoverall` WHERE playerName = '"+p.username+"';");
  91.             query("DELETE FROM `lastlogin` WHERE playerName = '"+p.username+"';");
  92.             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`,`Hunterlvl`,`Hunterxp`,`Constructionlvl`,`Constructionxp`,`Summoninglvl`,`Summoningxp`) VALUES ('"+p.username+"',"+p.skillLvl[0]+","+p.skillXP[0]+","+p.skillLvl[1]+","+p.skillXP[1]+","+p.skillLvl[2]+","+p.skillXP[2]+","+p.skillLvl[3]+","+p.skillXP[3]+","+p.skillLvl[4]+","+p.skillXP[4]+","+p.skillLvl[5]+","+p.skillXP[5]+","+p.skillLvl[6]+","+p.skillXP[6]+","+p.skillLvl[7]+","+p.skillXP[7]+","+p.skillLvl[8]+","+p.skillXP[8]+","+p.skillLvl[9]+","+p.skillXP[9]+","+p.skillLvl[10]+","+p.skillXP[10]+","+p.skillLvl[11]+","+p.skillXP[11]+","+p.skillLvl[12]+","+p.skillXP[12]+","+p.skillLvl[13]+","+p.skillXP[13]+","+p.skillLvl[14]+","+p.skillXP[14]+","+p.skillLvl[15]+","+p.skillXP[15]+","+p.skillLvl[16]+","+p.skillXP[16]+","+p.skillLvl[17]+","+p.skillXP[17]+","+p.skillLvl[18]+","+p.skillXP[18]+","+p.skillLvl[19]+","+p.skillXP[19]+","+p.skillLvl[20]+","+p.skillXP[20]+","+p.skillLvl[21]+","+p.skillXP[21]+","+p.skillLvl[22]+","+p.skillXP[22]+","+p.skillLvl[23]+","+p.skillXP[23]+");");
  93.             query("INSERT INTO `skillsoverall` (`playerName`,`lvl`,`xp`) VALUES ('"+p.username+"',"+(p.skillLvl[0] + p.skillLvl[1] + p.skillLvl[2] + p.skillLvl[3] + p.skillLvl[4] + p.skillLvl[5] + p.skillLvl[6] + p.skillLvl[7] + p.skillLvl[8] + p.skillLvl[9] + p.skillLvl[10] + p.skillLvl[11] + p.skillLvl[12] + p.skillLvl[13] + p.skillLvl[14] + p.skillLvl[15] + p.skillLvl[16] + p.skillLvl[17] + p.skillLvl[18] + p.skillLvl[19] + p.skillLvl[20] + p.skillLvl[21] + p.skillLvl[22] + p.skillLvl[23])+","+((p.skillXP[0]) + (p.skillXP[1]) + (p.skillXP[2]) + (p.skillXP[3]) + (p.skillXP[4]) + (p.skillXP[5]) + (p.skillXP[6]) + (p.skillXP[7]) + (p.skillXP[8]) + (p.skillXP[9]) + (p.skillXP[10]) + (p.skillXP[11]) + (p.skillXP[12]) + (p.skillXP[13]) + (p.skillXP[14]) + (p.skillXP[15]) + (p.skillXP[16]) + (p.skillXP[17]) + (p.skillXP[18]) + (p.skillXP[19]) + (p.skillXP[20]) + (p.skillXP[21]) + (p.skillXP[22]) + (p.skillXP[23]))+");");
  94.             query("INSERT INTO `lastlogin` (`playerName`,`time`,`date` ) VALUES ('"+p.username+"','"+now(timeF)+"','"+now(dateF)+"');");
  95.         } catch (Exception e) {
  96.             e.printStackTrace();
  97.             return false;
  98.         }
  99.         return true;
  100.     }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement