Advertisement
Guest User

Untitled

a guest
Jun 21st, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. public class MySQL {
  2.  
  3. public static String host;
  4. public static String password;
  5. public static String username;
  6. public static String port;
  7. public static String database;
  8. public static Connection con;
  9.  
  10.  
  11. private static ExecutorService executor;
  12.  
  13. static {
  14. executor = Executors.newCachedThreadPool();
  15. }
  16.  
  17. public static void connect() {
  18. if(!isConnected()) {
  19. try {
  20. con = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database + "?autoReconnect=true", username, password);
  21. setupNick();
  22. setupNickNames();
  23. } catch (SQLException e) {
  24. e.printStackTrace();
  25. }
  26. }
  27. }
  28.  
  29. public static void disconnect() {
  30. if(isConnected()) {
  31. try {
  32. con.close();
  33. con = null;
  34. } catch (SQLException e) {
  35. e.printStackTrace();
  36. }
  37. }
  38. }
  39.  
  40. public static boolean isConnected() {
  41. try {
  42. return con !=null && con.isValid(1);
  43. } catch (SQLException e) {
  44. e.printStackTrace();
  45. }
  46. return false;
  47. }
  48.  
  49. // Setup MySQL
  50. private static void setupNick() {
  51. if(isConnected()) {
  52. executor.execute(new Runnable() {
  53. @Override
  54. public void run() {
  55. try {
  56. String qry = "CREATE TABLE IF NOT EXISTS `Nick` (UUID TEXT, Spielername TEXT, Nickname TEXT)";
  57. PreparedStatement stmt;
  58. stmt = con.prepareStatement(qry);
  59. stmt.executeUpdate();
  60. stmt.close();
  61. } catch (SQLException e) {
  62. e.printStackTrace();
  63. }
  64. }
  65. });
  66. }
  67. }
  68.  
  69.  
  70.  
  71. private static void setupNickNames() {
  72. if(isConnected()) {
  73. executor.execute(new Runnable() {
  74. @Override
  75. public void run() {
  76. try {
  77. String qry = "CREATE TABLE IF NOT EXISTS `NickNames` (UUID TEXT, Spielername TEXT)";
  78. PreparedStatement stmt;
  79. stmt = con.prepareStatement(qry);
  80. stmt.executeUpdate();
  81. stmt.close();
  82. } catch (SQLException e) {
  83. e.printStackTrace();
  84. }
  85. }
  86. });
  87. }
  88. }
  89.  
  90.  
  91. // MySQL update
  92. public static void update(final String query) {
  93. if(isConnected()) {
  94. executor.execute(new Runnable() {
  95. @Override
  96. public void run() {
  97. try {
  98. PreparedStatement stmt;
  99. stmt = con.prepareStatement(query);
  100. stmt.executeUpdate();
  101. stmt.close();
  102. } catch (SQLException e) {
  103. e.printStackTrace();
  104. }
  105. }
  106. });
  107. }
  108. }
  109.  
  110. // MySQL result
  111. public static ResultSet getResult(String query) {
  112. if(isConnected()) {
  113. try {
  114. return con.prepareStatement(query).executeQuery();
  115. } catch (SQLException e) {
  116. e.printStackTrace();
  117. }
  118. }
  119. return null;
  120. }
  121.  
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement