Advertisement
Jycko

Is this good sql class?

Dec 12th, 2018
409
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.90 KB | None | 0 0
  1. package com.gmail.JyckoSianjaya.DonateCraft.Database;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.SQLException;
  6. import java.sql.Statement;
  7.  
  8. import org.bukkit.Bukkit;
  9. import org.bukkit.scheduler.BukkitRunnable;
  10.  
  11. import com.gmail.JyckoSianjaya.DonateCraft.Main.DonateCraft;
  12. import com.gmail.JyckoSianjaya.DonateCraft.Utils.Utility;
  13.  
  14. public class SimpleSQL {
  15.     private static SimpleSQL instance;
  16.     private Connection connection;
  17.     private String hostname = ""; // The host name
  18.     private int port = 0; // The host port
  19.     private String databasename = ""; // The name of the database
  20.     private String username = ""; // MySQL username
  21.     private String password = ""; // MySQL password
  22.     private SimpleSQL(String hostname, int port , String databasename, String username, String password) {
  23.         this.hostname = hostname;
  24.         this.port = port;
  25.         this.databasename = databasename;
  26.         this.username = username;
  27.         this.password = password;
  28.         new BukkitRunnable() {
  29.             Boolean loaded = false;
  30.             @Override
  31.             public void run() {
  32.                 Bukkit.getServer().broadcastMessage("Praya gantengus megantrophus paleojavanicus");
  33.                 if (!loaded) {
  34.                     try {
  35.                         openConnection();
  36.                         Statement state = connection.createStatement();
  37.                     } catch (SQLException e) {
  38.                         Utility.sendConsole("[DC] &7Can't open a MySQL connection, is it correct?");
  39.                     } catch (ClassNotFoundException e) {
  40.                         Utility.sendConsole("[DC] &7Can't find SQL class. Please contact the author via spigot conversation.");
  41.                     }
  42.                 }
  43.                 Boolean closed = false;
  44.                 try {
  45.                     closed = connection.isClosed();
  46.                 } catch (SQLException e) {
  47.                     Utility.sendConsole("[DC] &7Can't check if Connection closed. Is it okay?");
  48.                 }
  49.                 if (closed) {
  50.                     try {
  51.                         openConnection();
  52.                         Statement state = connection.createStatement();
  53.                     } catch (SQLException e) {
  54.                         Utility.sendConsole("[DC] &7Can't open a MySQL connection, is it correct?");
  55.                     } catch (ClassNotFoundException e) {
  56.                         Utility.sendConsole("[DC] &7Can't find SQL class. Please contact the author via spigot conversation.");
  57.                     }
  58.                 }
  59.             }
  60.         }.runTaskTimerAsynchronously(DonateCraft.getInstance(), 100L, 100L);
  61.     }
  62.     public static SimpleSQL setup(String host, int port, String database, String user, String pass) {
  63.         if (instance != null) return null;
  64.         instance = new SimpleSQL(host, port, database, user, pass);
  65.         return instance;
  66.     }
  67.     public void openConnection() throws SQLException, ClassNotFoundException {
  68.         if (connection != null && !connection.isClosed()) {
  69.             return;
  70.         }
  71.      
  72.         synchronized (this) {
  73.             if (connection != null && !connection.isClosed()) {
  74.                 return;
  75.             }
  76.             Class.forName("com.mysql.jdbc.Driver");
  77.             connection = DriverManager.getConnection("jdbc:mysql://" + this.hostname+ ":" + this.port + "/" + this.databasename, this.username, this.password);
  78.         }
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement