Advertisement
Guest User

Untitled

a guest
Oct 19th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.65 KB | None | 0 0
  1. package gg.southgate.slobby.utils;
  2.  
  3. import com.zaxxer.hikari.HikariConfig;
  4. import com.zaxxer.hikari.HikariDataSource;
  5. import com.zaxxer.hikari.pool.HikariPool;
  6. import gg.southgate.mrleaw.language.utils.LanguageData;
  7. import org.bukkit.Bukkit;
  8.  
  9. import java.sql.*;
  10. import java.util.UUID;
  11.  
  12. public class FirstJoinMySQL {
  13.  
  14. private static String host = "localhost", username = "test", password = "Southgate2013", database = "test", port = "3306";
  15. private static Connection con;
  16.  
  17. public static void connect() {
  18. HikariConfig config = new HikariConfig();
  19. config.setJdbcUrl(String.format("jdbc:mysql://%s:%s/%s", host, port, database));
  20. config.setUsername(username);
  21. config.setPassword(password);
  22. try {
  23. con = new HikariDataSource(config).getConnection();
  24. } catch (HikariPool.PoolInitializationException | SQLException e) {
  25. e.printStackTrace();
  26. }
  27. if (con != null)
  28. Bukkit.getConsoleSender().sendMessage(LanguageData.getPrefix() + "§afirstJoin-MySQL-Connection ready!");
  29. }
  30.  
  31.  
  32. public static void close() {
  33. if (isConnected()) {
  34. try {
  35. con.close();
  36. Bukkit.getConsoleSender().sendMessage(LanguageData.getPrefix() + "§cfirstJoin-MySQL-Connection closed!");
  37. } catch (SQLException e) {
  38. e.printStackTrace();
  39. }
  40. }
  41. }
  42.  
  43. public static boolean isConnected() {
  44. return con != null;
  45. }
  46.  
  47.  
  48. public static boolean isFirstJoin(UUID uuid) {
  49. try {
  50. PreparedStatement ps = con.prepareStatement("SELECT uuid FROM firstjoin WHERE uuid = ?");
  51. ps.setString(1, uuid.toString());
  52. ResultSet rs = ps.executeQuery();
  53. return rs.next();
  54. } catch (SQLException e) {
  55. e.printStackTrace();
  56. return false;
  57. }
  58. }
  59.  
  60. public static void createTable() {
  61. if (isConnected()) {
  62. try {
  63. con.createStatement().executeUpdate("CREATE TABLE IF NOT exists firstjoin\n" +
  64. "(\n" +
  65. " uuid VARCHAR(50) PRIMARY KEY NOT NULL\n" +
  66. ");");
  67. } catch (SQLException e) {
  68. e.printStackTrace();
  69. }
  70. }
  71. }
  72.  
  73. public static void insert(UUID uuid) {
  74. try {
  75. PreparedStatement ps = con.prepareStatement("INSERT INTO firstjoin (uuid) VALUES (?)");
  76. ps.setString(1, uuid.toString());
  77. ps.execute();
  78. } catch (SQLException e) {
  79. e.printStackTrace();
  80. }
  81. }
  82.  
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement