Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. package me.hotribs.voidshooter.managers;
  2.  
  3. import me.hotribs.voidshooter.VoidShooter;
  4. import org.bukkit.Bukkit;
  5.  
  6. import java.sql.Connection;
  7. import java.sql.DriverManager;
  8. import java.sql.SQLException;
  9.  
  10. import static me.hotribs.voidshooter.utils.ChatUtils.format;
  11.  
  12. public class SQLManager {
  13.  
  14. private static SQLManager instance = new SQLManager();
  15. private VoidShooter plugin = VoidShooter.getPlugin();
  16.  
  17. private Connection connection;
  18. private String host, database, username, password;
  19. private int port;
  20.  
  21. public static SQLManager getInstance() {
  22. return instance;
  23. }
  24.  
  25. public void setup() {
  26. host = plugin.getConfig().getString("database.host");
  27. port = plugin.getConfig().getInt("database.port");
  28. database = plugin.getConfig().getString("database.database");
  29. username = plugin.getConfig().getString("database.username");
  30. password = plugin.getConfig().getString("database.password");
  31.  
  32. try {
  33. synchronized (this) {
  34. if (getConnection() != null && !getConnection().isClosed()) {
  35. return;
  36. }
  37.  
  38. Class.forName("com.mysql.jdbc.Driver");
  39. setConnection(DriverManager.getConnection("jdbc:mysql://"
  40. + this.host + ":" + this.port + "/" + this.database, this.username, this.password));
  41.  
  42. Bukkit.getConsoleSender().sendMessage(format("&aSuccesfully connected with database!"));
  43. }
  44. } catch (SQLException e) {
  45. e.printStackTrace();
  46. } catch (ClassNotFoundException e) {
  47. e.printStackTrace();
  48. }
  49. }
  50.  
  51. private Connection getConnection() {
  52. return connection;
  53. }
  54.  
  55. private void setConnection(Connection connection) {
  56. this.connection = connection;
  57. }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement