Advertisement
Guest User

Untitled

a guest
Aug 29th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.27 KB | None | 0 0
  1. package fr.vexia.api.database;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.SQLException;
  6.  
  7. public class SqlManager {
  8.    
  9.       private String host, name, user, password;
  10.       private Connection connection;
  11.      
  12.       public SqlManager(String host, String name, String user, String password){
  13.         this.host = host;
  14.         this.name = name;
  15.         this.user = user;
  16.         this.password = password;
  17.       }
  18.      
  19.       public void connect() {
  20.         if (!isConnected()) {
  21.           try {
  22.               this.connection = DriverManager.getConnection("jdbc:mysql://" + this.host + "/" + this.name,
  23.               this.user, this.password);
  24.           } catch (SQLException e) {
  25.             e.printStackTrace();
  26.           }
  27.         }
  28.       }
  29.      
  30.       public void disconnect() {
  31.         if (isConnected()) {
  32.           try {
  33.             this.connection.close();
  34.           } catch (SQLException e) {
  35.             e.printStackTrace();
  36.           }
  37.         }
  38.       }
  39.      
  40.       public void initTables(){
  41.           try {
  42.             this.connection.prepareStatement("CREATE TABLE IF NOT EXISTS `accounts` (`id` int(11) NOT NULL, `pseudo` varchar(255) DEFAULT NULL, `uuid` text, `ip` text, `grade` int(11) "
  43.                     + "NOT NULL DEFAULT '0',`coins` int(11) DEFAULT '0',`credit` int(11) NOT NULL DEFAULT '0',`firstJoin` bigint(20) NOT NULL DEFAULT '0',`lastJoin` bigint(20) "
  44.                     + "NOT NULL DEFAULT '0',`totalTime` bigint(20) NOT NULL DEFAULT '0',`friends` text,`online` tinyint(1) NOT NULL DEFAULT '0')").executeQuery();
  45.            
  46.             this.connection.prepareStatement("CREATE TABLE IF NOT EXISTS `ban` (`id` int(11) NOT NULL,`playerID` int(11) NOT NULL,`modoID` int(11) NOT NULL,"
  47.                     + "`type` enum('BAN','BANIP','MUTE','KICK') NOT NULL,`raison` text NOT NULL,`start` bigint(20) NOT NULL,`duration` bigint(20) NOT NULL,"
  48.                     + "`removeID` int(11) NOT NULL,`removeRaison` text NOT NULL)").executeQuery();
  49.         } catch (SQLException e) {
  50.             e.printStackTrace();
  51.         }
  52.       }
  53.      
  54.       public boolean isConnected() {
  55.         try {
  56.           if ((this.connection == null) || (this.connection.isClosed()) || (!this.connection.isValid(5))) {
  57.              return false;
  58.           }
  59.           return true;
  60.         } catch (SQLException e) {
  61.           e.printStackTrace();
  62.         }
  63.         return false;
  64.       }
  65.      
  66.       public Connection getConnection() {
  67.         return this.connection;
  68.       }
  69.  
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement