Advertisement
Guest User

Untitled

a guest
Feb 9th, 2019
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.59 KB | None | 0 0
  1. package fr.spacetube.dailya.spigot.dailyawars.sql;
  2.  
  3. import fr.spacetube.dailya.spigot.dailyawars.Main;
  4.  
  5. import java.sql.Connection;
  6. import java.sql.DriverManager;
  7. import java.sql.SQLException;
  8.  
  9. import static fr.spacetube.dailya.spigot.dailyawars.Main.*;
  10.  
  11. public class SQLCredentials {
  12.  
  13.     private String host,database,user,password;
  14.     private int port;
  15.     private Connection connection;
  16.  
  17.     private int i = 5;
  18.  
  19.     public void tryConnection() {
  20.  
  21.         if (!hasConnection()) {
  22.  
  23.             while (i != 0) {
  24.  
  25.                 if (host.isEmpty() || database.isEmpty() || user.isEmpty() || password.isEmpty()) {
  26.  
  27.                     host = getInstance().getConfig().getString("config.sql.host");
  28.                     database = getInstance().getConfig().getString("config.sql.database");
  29.                     user = getInstance().getConfig().getString("config.sql.user");
  30.                     password = getInstance().getConfig().getString("config.sql.password");
  31.  
  32.                     port = getInstance().getConfig().getInt("config.sql.port");
  33.  
  34.                 } else {
  35.  
  36.                     try {
  37.  
  38.                         connection = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database, user, password);
  39.  
  40.                         Main.getInstance().getServer().getConsoleSender().sendMessage(Main.getInstance().getClassManager().getMessageSQL().sqlSucessfulConnection);
  41.                     } catch (SQLException e) {Main.getInstance().getServer().getConsoleSender().sendMessage(Main.getInstance().getClassManager().getMessageSQL().sqlMissingConnection);}
  42.                 }
  43.             }
  44.  
  45.             if (i == 0) {
  46.  
  47.                 getInstance().getServer().shutdown();
  48.             }
  49.         } else {
  50.  
  51.             Main.getInstance().getServer().getConsoleSender().sendMessage(Main.getInstance().getClassManager().getMessageSQL().sqlAllreadyExist);
  52.         }
  53.     }
  54.  
  55.     public void destroyConnection(){
  56.  
  57.         if(hasConnection()){
  58.  
  59.             try {
  60.                 connection.close();
  61.             } catch (SQLException e) {Main.getInstance().getServer().getConsoleSender().sendMessage(Main.getInstance().getClassManager().getMessageSQL().sqlSucessfulDisconnection);}
  62.         }
  63.     }
  64.  
  65.     public String getHost() {return host;}
  66.  
  67.     public String getDatabase() {return database;}
  68.  
  69.     public String getUser() {return user;}
  70.  
  71.     public String getPassword() {return password;}
  72.  
  73.     public int getPort() {return port;}
  74.  
  75.     private boolean hasConnection(){return connection != null;}
  76.  
  77.     public Connection getConnection() { return connection;}
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement