Advertisement
Guest User

Untitled

a guest
Aug 11th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.77 KB | None | 0 0
  1. package server.util;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.sql.Statement;
  7.  
  8. public class SQL {
  9.     public static Connection con = null;
  10.     public static Statement stmt;
  11.     public static boolean printed = false;
  12.  
  13.     public static boolean createConnection() {
  14.         try {
  15.             Class.forName("com.mysql.jdbc.Driver").newInstance();
  16.             con = DriverManager.getConnection(
  17.                     "jdbc:mysql://box332.bluehost.com:3306/impactpk_imp", "impactpk_hs", "terr");
  18.             stmt = con.createStatement();
  19.             return true;
  20.         } catch (Exception e) {
  21.             return false;
  22.         }
  23.         //return false;
  24.     }
  25.  
  26.  
  27.     public static boolean createConnection2() {
  28.         try {
  29.             Class.forName("com.mysql.jdbc.Driver").newInstance();
  30.             con = DriverManager.getConnection(
  31.                     "jdbc:mysql://localhost/highscores", "root", "roflcakes");
  32.             stmt = con.createStatement();
  33.             return true;
  34.         } catch (Exception e) {
  35.             if(!printed) {
  36.             e.printStackTrace();
  37.             printed = true;
  38.             }
  39.             return false;
  40.         }
  41.         //return false;
  42.     }
  43.  
  44.     public static ResultSet query(String s) throws SQLException {
  45.         try {
  46.             if (s.toLowerCase().startsWith("select")) {
  47.                 ResultSet rs = stmt.executeQuery(s);
  48.                 return rs;
  49.             } else {
  50.                 stmt.executeUpdate(s);
  51.             }
  52.             return null;
  53.         } catch (Exception e) {
  54.             e.printStackTrace();
  55.             destroyConnection();
  56.             createConnection();
  57.         }
  58.         return null;
  59.     }
  60.  
  61.     public static void destroyConnection() {
  62.         try {
  63.             stmt.close();
  64.             con.close();
  65.         } catch (Exception e) {
  66.         }
  67.     }
  68. }
  69.  
  70. /*OHAIDERRRRRRRRRRRRRRRR*/
  71.     public static void processSQL(Client c) {
  72.         try {
  73.         if(!SQL.createConnection()) {
  74.             c.sendMessage("Your request could not be processed. Try again later.");
  75.             return;
  76.         }
  77.         ResultSet rs = SQL.query("SELECT * FROM donator WHERE finished=false AND name='" + c.playerName + "'");
  78.         if(rs == null) {
  79.                 c.sendMessage("You have not ordered any points!");
  80.                 return;
  81.         }      
  82.         int amount = 0;
  83.         ArrayList<Integer> rows = new ArrayList<Integer>();
  84.         while (rs.next()) {
  85.             String s = rs.getString("amount");
  86.            
  87.             try {
  88.                 int a = (int)(Double.parseDouble(s) * 100);
  89.                 amount += a;
  90.                 int row = Integer.parseInt(rs.getString("row"));
  91.                 rows.add(row);
  92.             } catch(Exception e) {
  93.                 e.printStackTrace();
  94.             }
  95.         }
  96.        
  97.         for(int x : rows) {
  98.                 SQL.query("UPDATE donator SET finished=true WHERE row=" + x);
  99.         }
  100.        
  101.         if(amount <= 0) {
  102.             c.sendMessage("You have not ordered any points!");
  103.         } else {
  104.             c.donatePoints += amount;
  105.             c.sendMessage("You have been given " + amount + " donator points.");
  106.         }
  107.         } catch(Exception e) {
  108.             e.printStackTrace();
  109.         }
  110.     }
  111. /*MORE OHAIDERRRRRRRRRRRR*/
  112. if (playerCommand.toLowerCase().startsWith("getpoints")) {
  113.                 EventManager.processSQL(c);
  114.             }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement