Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.silver.harambeknowsu;
- import java.io.File;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.time.Instant;
- import java.util.HashSet;
- import java.util.Scanner;
- import java.util.UUID;
- public class DatabaseController {
- File configFile;
- String user, passwd, address, db;
- Connection dbConn;
- private boolean connectionStatus;
- public DatabaseController(File configFile) {
- try {
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- this.configFile = configFile;
- reloadConfig();
- if (initConnection()) {
- System.out.println("Database connection success!");
- this.connectionStatus = true;
- } else {
- System.out.println("Database connection failed!");
- }
- }
- public boolean isConnected() {
- return connectionStatus;
- }
- private boolean initConnection() {
- Connection conn = null;
- try {
- // System.out.println("Init connect to " + user + "@" + passwd + ":"
- // + address);
- conn = DriverManager.getConnection(
- "jdbc:mysql://" + address + "?user=" + user + "&autoReconnect=true&password=" + passwd);
- } catch (SQLException ex) {
- System.out.println("SQLException: " + ex.getMessage());
- System.out.println("SQLState: " + ex.getSQLState());
- System.out.println("VendorError: " + ex.getErrorCode());
- ex.printStackTrace();
- return false;
- }
- this.dbConn = conn;
- return true;
- }
- private void reloadConfig() {
- try {
- if (!configFile.exists()) {
- PrintWriter pw = new PrintWriter(configFile);
- pw.println("user: []");
- pw.println("passwd: []");
- pw.println("address: []");
- pw.println("db: []");
- pw.close();
- }
- Scanner scan = new Scanner(configFile);
- while (scan.hasNextLine()) {
- String in = scan.nextLine();
- if (in.contains(":")) {
- String front = in.split(":")[0];
- if (front.equalsIgnoreCase("user")) {
- this.user = in.split(":")[1].trim();
- } else if (front.equalsIgnoreCase("passwd")) {
- this.passwd = in.split(":")[1].trim();
- } else if (front.equalsIgnoreCase("address")) {
- this.address = in.split(":")[1].trim();
- } else if (front.equalsIgnoreCase("db")) {
- this.db = in.split(":")[1].trim();
- } else {
- System.out.println("Invalid DB Config Value \"" + in.split(":")[0] + "\"");
- }
- }
- }
- scan.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- private HashSet<String> searched = new HashSet<String>();
- private HashSet<String> results = new HashSet<String>();
- public boolean isBannedPlayer(String name) {
- UUIDFetcher uf = new UUIDFetcher();
- return this.isBannedPlayer(uf.getUUID(name));
- }
- public boolean isBannedPlayer(UUID u) {
- if (u != null) {
- System.out.println("Checking ban on " + u.toString());
- String q = "SELECT * FROM " + this.db + ".litebans_bans WHERE uuid =\"" + u.toString() + "\" AND active=1;";
- // String q = "SELECT * FROM " + this.db + ".litebans_bans WHERE
- // uuid = uuid;";
- ResultSet r = this.queryDatabase(q);
- // System.out.println(q);
- try {
- while (r != null && r.next()) {
- return true;
- }
- } catch (SQLException e) {
- System.out.println(e.getMessage());
- e.printStackTrace();
- }
- }
- return false;
- }
- private ResultSet queryDatabase(String query) {
- Statement stmt = null;
- ResultSet rs = null;
- try {
- // System.out.println(1);
- stmt = this.dbConn.createStatement();
- // System.out.println(2);
- rs = stmt.executeQuery(query);
- // System.out.println(4);
- return rs;
- } catch (SQLException ex) {
- // System.out.println(7);
- System.out.println("Query: \"" + query + "\"");
- System.out.println("SQLException: " + ex.getMessage());
- System.out.println("SQLState: " + ex.getSQLState());
- System.out.println("VendorError: " + ex.getErrorCode());
- ex.printStackTrace();
- }
- return rs;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement