Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package tequilaxbr.dixeelban.utils;
- import java.io.File;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.concurrent.TimeUnit;
- public class SQL {
- private String user, host, database, password;
- private Connection connection;
- private Statement statement;
- private SQLType sqlType;
- private File db;
- public SQL(String user, String password, String host, String database, SQLType sqlType) {
- this.user = user;
- this.password = password;
- this.host = host;
- this.database = database;
- this.sqlType = sqlType;
- }
- public SQL(String database, File folder, SQLType sqlType){
- this.db = folder;
- this.database = database;
- this.sqlType = sqlType;
- }
- public void startConnection(){
- if(getType()){
- try {
- Class.forName("com.mysql.jdbc.Driver");
- this.connection = DriverManager.getConnection("jdbc:mysql://" + host + "/" + database + "", user, password);
- this.statement = this.connection.createStatement();
- this.statement.execute("CREATE TABLE IF NOT EXISTS bans (nickname VARCHAR(255), tempo LONG NOT NULL, banid INT NOT NULL)");
- } catch (SQLException | ClassNotFoundException e) {
- e.printStackTrace();
- }
- }else{
- try {
- Class.forName("org.sqlite.JDBC");
- this.connection = DriverManager.getConnection("jdbc:sqlite:" + this.db.getAbsolutePath() + File.separator + database+".db");
- this.statement = this.connection.createStatement();
- this.statement.execute("CREATE TABLE IF NOT EXISTS bans (nickname VARCHAR(255), tempo LONG NOT NULL, banid INT NOT NULL)");
- } catch (ClassNotFoundException | SQLException e) {
- e.printStackTrace();
- }
- }
- }
- public void closeConnection(){
- try {
- this.statement.close();
- this.connection.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public void addBanPlayer(String id, int minutes, int banid){
- if(getType()){
- if(!hasBanPlayer(id)){
- try {
- Class.forName("com.mysql.jdbc.Driver");
- long timerAll = System.currentTimeMillis() + TimeUnit.MINUTES.toMillis(minutes);
- this.statement.executeUpdate("INSERT INTO bans (nickname, tempo, banid) VALUES ('"+ id + "','" + timerAll + "','" + banid + "')");
- } catch (ClassNotFoundException | SQLException e) {
- e.printStackTrace();
- }
- }
- }else{
- if(!hasBanPlayer(id)){
- try {
- Class.forName("org.sqlite.JDBC");
- long timerAll = System.currentTimeMillis() + TimeUnit.MINUTES.toMillis(minutes);
- this.statement.executeUpdate("INSERT INTO bans (nickname, tempo, banid) VALUES ('"+ id + "','" + timerAll + "','" + banid + "')");
- } catch (ClassNotFoundException | SQLException e) {
- e.printStackTrace();
- }
- }
- }
- }
- public long getBanTimer(String id){
- if(getType()){
- try {
- Class.forName("com.mysql.jdbc.Driver");
- ResultSet rs = this.statement.executeQuery("SELECT tempo FROM bans WHERE nickname='" + id + "'");
- while(rs.next()){
- return rs.getLong("tempo");
- }
- } catch (SQLException | ClassNotFoundException e) {
- e.printStackTrace();
- }
- }else{
- try {
- Class.forName("org.sqlite.JDBC");
- ResultSet rs = this.statement.executeQuery("SELECT tempo FROM bans WHERE nickname='" + id + "'");
- while(rs.next()){
- return rs.getLong("tempo");
- }
- } catch (ClassNotFoundException | SQLException e) {
- e.printStackTrace();
- }
- }
- return 0;
- }
- public boolean hasBanPlayer(String id){
- if(getType()){
- try {
- Class.forName("com.mysql.jdbc.Driver");
- ResultSet rs = this.statement.executeQuery("SELECT * FROM bans WHERE nickname='"+id+"'");
- while(rs.next()){
- return true;
- }
- return false;
- } catch (SQLException | ClassNotFoundException e) {
- }
- }else{
- try {
- Class.forName("org.sqlite.JDBC");
- ResultSet rs = this.statement.executeQuery("SELECT * FROM bans WHERE nickname='"+id+"'");
- while(rs.next()){
- return true;
- }
- return false;
- } catch (SQLException | ClassNotFoundException e) {
- e.printStackTrace();
- }
- }
- return false;
- }
- public boolean getType(){
- if(sqlType == SQLType.MySQL){
- return true;
- }else{
- return false;
- }
- }
- public enum SQLType{
- MySQL, SQLite;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement