Advertisement
Guest User

Untitled

a guest
Jul 26th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.04 KB | None | 0 0
  1. package com.silver.harambeknowsu;
  2.  
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.io.PrintWriter;
  6. import java.sql.Connection;
  7. import java.sql.DriverManager;
  8. import java.sql.ResultSet;
  9. import java.sql.SQLException;
  10. import java.sql.Statement;
  11. import java.time.Instant;
  12. import java.util.HashSet;
  13. import java.util.Scanner;
  14. import java.util.UUID;
  15.  
  16. public class DatabaseController {
  17.     File configFile;
  18.     String user, passwd, address, db;
  19.     Connection dbConn;
  20.     private boolean connectionStatus;
  21.  
  22.     public DatabaseController(File configFile) {
  23.         try {
  24.             Class.forName("com.mysql.jdbc.Driver").newInstance();
  25.         } catch (Exception ex) {
  26.             ex.printStackTrace();
  27.         }
  28.         this.configFile = configFile;
  29.         reloadConfig();
  30.         if (initConnection()) {
  31.             System.out.println("Database connection success!");
  32.             this.connectionStatus = true;
  33.         } else {
  34.             System.out.println("Database connection failed!");
  35.         }
  36.  
  37.     }
  38.  
  39.     public boolean isConnected() {
  40.         return connectionStatus;
  41.     }
  42.  
  43.     private boolean initConnection() {
  44.         Connection conn = null;
  45.         try {
  46.             // System.out.println("Init connect to " + user + "@" + passwd + ":"
  47.             // + address);
  48.             conn = DriverManager.getConnection(
  49.                     "jdbc:mysql://" + address + "?user=" + user + "&autoReconnect=true&password=" + passwd);
  50.         } catch (SQLException ex) {
  51.             System.out.println("SQLException: " + ex.getMessage());
  52.             System.out.println("SQLState: " + ex.getSQLState());
  53.             System.out.println("VendorError: " + ex.getErrorCode());
  54.             ex.printStackTrace();
  55.             return false;
  56.         }
  57.         this.dbConn = conn;
  58.         return true;
  59.     }
  60.  
  61.     private void reloadConfig() {
  62.         try {
  63.             if (!configFile.exists()) {
  64.                 PrintWriter pw = new PrintWriter(configFile);
  65.                 pw.println("user: []");
  66.                 pw.println("passwd: []");
  67.                 pw.println("address: []");
  68.                 pw.println("db: []");
  69.                 pw.close();
  70.             }
  71.             Scanner scan = new Scanner(configFile);
  72.             while (scan.hasNextLine()) {
  73.                 String in = scan.nextLine();
  74.                 if (in.contains(":")) {
  75.                     String front = in.split(":")[0];
  76.                     if (front.equalsIgnoreCase("user")) {
  77.                         this.user = in.split(":")[1].trim();
  78.                     } else if (front.equalsIgnoreCase("passwd")) {
  79.                         this.passwd = in.split(":")[1].trim();
  80.                     } else if (front.equalsIgnoreCase("address")) {
  81.                         this.address = in.split(":")[1].trim();
  82.                     } else if (front.equalsIgnoreCase("db")) {
  83.                         this.db = in.split(":")[1].trim();
  84.                     } else {
  85.                         System.out.println("Invalid DB Config Value \"" + in.split(":")[0] + "\"");
  86.                     }
  87.                 }
  88.             }
  89.             scan.close();
  90.         } catch (IOException e) {
  91.             e.printStackTrace();
  92.         }
  93.     }
  94.  
  95.     private HashSet<String> searched = new HashSet<String>();
  96.     private HashSet<String> results = new HashSet<String>();
  97.  
  98.     public boolean isBannedPlayer(String name) {
  99.         UUIDFetcher uf = new UUIDFetcher();
  100.         return this.isBannedPlayer(uf.getUUID(name));
  101.     }
  102.  
  103.     public boolean isBannedPlayer(UUID u) {
  104.         if (u != null) {
  105.             System.out.println("Checking ban on " + u.toString());
  106.             String q = "SELECT * FROM " + this.db + ".litebans_bans WHERE uuid =\"" + u.toString() + "\" AND active=1;";
  107.             // String q = "SELECT * FROM " + this.db + ".litebans_bans WHERE
  108.             // uuid = uuid;";
  109.             ResultSet r = this.queryDatabase(q);
  110.             // System.out.println(q);
  111.             try {
  112.                 while (r != null && r.next()) {
  113.                     return true;
  114.                 }
  115.             } catch (SQLException e) {
  116.                 System.out.println(e.getMessage());
  117.                 e.printStackTrace();
  118.             }
  119.         }
  120.         return false;
  121.  
  122.     }
  123.  
  124.     private ResultSet queryDatabase(String query) {
  125.  
  126.         Statement stmt = null;
  127.         ResultSet rs = null;
  128.  
  129.         try {
  130.             // System.out.println(1);
  131.             stmt = this.dbConn.createStatement();
  132.             // System.out.println(2);
  133.             rs = stmt.executeQuery(query);
  134.             // System.out.println(4);
  135.             return rs;
  136.         } catch (SQLException ex) {
  137.             // System.out.println(7);
  138.             System.out.println("Query: \"" + query + "\"");
  139.             System.out.println("SQLException: " + ex.getMessage());
  140.             System.out.println("SQLState: " + ex.getSQLState());
  141.             System.out.println("VendorError: " + ex.getErrorCode());
  142.             ex.printStackTrace();
  143.         }
  144.         return rs;
  145.     }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement