Advertisement
Guest User

MySQL

a guest
Mar 2nd, 2017
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.38 KB | None | 0 0
  1. /*   1:    */ package io.aura.main;
  2. /*   2:    */
  3. /*   3:    */ import java.io.File;
  4. /*   4:    */ import java.io.IOException;
  5. /*   6:    */ import java.sql.Connection;
  6. /*   7:    */ import java.sql.DriverManager;
  7. /*   8:    */ import java.sql.ResultSet;
  8. /*   9:    */ import java.sql.SQLException;
  9. /*  10:    */ import java.sql.Statement;
  10. /*  11:    */ import org.bukkit.Bukkit;
  11. /*  13:    */ import org.bukkit.configuration.file.FileConfiguration;
  12. /*  15:    */ import org.bukkit.configuration.file.YamlConfiguration;
  13. /*  16:    */
  14. /*  17:    */ public class MySQL
  15. /*  18:    */ {
  16. /*  19:    */   public static String username;
  17. /*  20:    */   public static String password;
  18. /*  21:    */   public static String database;
  19. /*  22:    */   public static String host;
  20. /*  23:    */   public static String port;
  21. /*  24:    */   public static Connection con;
  22. /*  25:    */  
  23. /*  26:    */   public MySQL(String user, String pass, String host2, String dB) {}
  24. /*  27:    */  
  25. /*  28:    */   public static void connect()
  26. /*  29:    */   {
  27. /*  30: 27 */     if (!isConnected()) {
  28. /*  31:    */       try
  29. /*  32:    */       {
  30. /*  33: 30 */         con = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database + "?user=" + username + "&password=" + password + "&autoReconnect=true");
  31. /*  34: 31 */         Bukkit.getConsoleSender().sendMessage("§aMySQL MySQL connected");
  32. /*  35:    */       }
  33. /*  36:    */       catch (SQLException e)
  34. /*  37:    */       {
  35. /*  38: 35 */         e.printStackTrace();
  36. /*  39:    */       }
  37. /*  40:    */     }
  38. /*  41:    */   }
  39. /*  42:    */  
  40. /*  43:    */   public static void close()
  41. /*  44:    */   {
  42. /*  45: 42 */     if (isConnected()) {
  43. /*  46:    */       try
  44. /*  47:    */       {
  45. /*  48: 45 */         con.close();
  46. /*  49: 46 */         Bukkit.getConsoleSender().sendMessage("§aMySQL MySQL disconnected");
  47. /*  50:    */       }
  48. /*  51:    */       catch (SQLException e)
  49. /*  52:    */       {
  50. /*  53: 50 */         e.printStackTrace();
  51. /*  54:    */       }
  52. /*  55:    */     }
  53. /*  56:    */   }
  54. /*  57:    */  
  55. /*  58:    */   public static boolean isConnected()
  56. /*  59:    */   {
  57. /*  60: 57 */     return con != null;
  58. /*  61:    */   }
  59. /*  62:    */  
  60. /*  63:    */   public static void createTable()
  61. /*  64:    */   {
  62. /*  65: 62 */     if (isConnected()) {
  63. /*  66:    */       try
  64. /*  67:    */       {
  65. /*  68: 66 */         con.createStatement().executeUpdate("CREATE TABLE IF NOT EXISTS Aura (UUID VARCHAR(100), NAME VARCHAR(100), KILLS int, DEATHS int , WIN int , LOSE int , COINS int)");
  66. /*  69: 67 */         Bukkit.getConsoleSender().sendMessage("§4MySQL MySQL Table created");
  67. /*  70:    */       }
  68. /*  71:    */       catch (SQLException e)
  69. /*  72:    */       {
  70. /*  73: 71 */         e.printStackTrace();
  71. /*  74:    */       }
  72. /*  75:    */     }
  73. /*  76:    */   }
  74. /*  77:    */  
  75. /*  78:    */   public static void update(String qry)
  76. /*  79:    */   {
  77. /*  80: 78 */     if (isConnected()) {
  78. /*  81:    */       try
  79. /*  82:    */       {
  80. /*  83: 81 */         con.createStatement().executeUpdate(qry);
  81. /*  84:    */       }
  82. /*  85:    */       catch (SQLException e)
  83. /*  86:    */       {
  84. /*  87: 85 */         e.printStackTrace();
  85. /*  88:    */       }
  86. /*  89:    */     }
  87. /*  90:    */   }
  88. /*  91:    */  
  89. /*  92:    */   public static ResultSet getResult(String qry)
  90. /*  93:    */   {
  91. /*  94: 92 */     ResultSet rs = null;
  92. /*  95:    */     try
  93. /*  96:    */     {
  94. /*  97: 95 */       Statement st = con.createStatement();
  95. /*  98: 96 */       rs = st.executeQuery(qry);
  96. /*  99:    */     }
  97. /* 100:    */     catch (SQLException e)
  98. /* 101:    */     {
  99. /* 102:100 */       connect();
  100. /* 103:101 */       System.err.println(e);
  101. /* 104:    */     }
  102. /* 105:103 */     return rs;
  103. /* 106:    */   }
  104. /* 107:    */  
  105. /* 108:    */   public static File getMySQLFile()
  106. /* 109:    */   {
  107. /* 110:108 */     return new File("plugins/Aura_Ceriox", "MySQL.yml");
  108. /* 111:    */   }
  109. /* 112:    */  
  110. /* 113:    */   public static FileConfiguration getMySQLFileConfiguration()
  111. /* 114:    */   {
  112. /* 115:113 */     return YamlConfiguration.loadConfiguration(getMySQLFile());
  113. /* 116:    */   }
  114. /* 117:    */  
  115. /* 118:    */   public static void setStandardMySQL()
  116. /* 119:    */   {
  117. /* 120:118 */     FileConfiguration cfg = getMySQLFileConfiguration();
  118. /* 121:    */    
  119. /* 122:120 */     cfg.options().copyDefaults(true);
  120. /* 123:121 */     cfg.addDefault("username", "root");
  121. /* 124:122 */     cfg.addDefault("password", "password");
  122. /* 125:123 */     cfg.addDefault("database", "localhost");
  123. /* 126:124 */     cfg.addDefault("host", "localhost");
  124. /* 127:125 */     cfg.addDefault("port", "3306");
  125. /* 128:    */     try
  126. /* 129:    */     {
  127. /* 130:128 */       cfg.save(getMySQLFile());
  128. /* 131:    */     }
  129. /* 132:    */     catch (IOException e)
  130. /* 133:    */     {
  131. /* 134:132 */       e.printStackTrace();
  132. /* 135:    */     }
  133. /* 136:    */   }
  134. /* 137:    */  
  135. /* 138:    */   public static void readMySQL()
  136. /* 139:    */   {
  137. /* 140:138 */     FileConfiguration cfg = getMySQLFileConfiguration();
  138. /* 141:139 */     username = cfg.getString("username");
  139. /* 142:140 */     password = cfg.getString("password");
  140. /* 143:141 */     database = cfg.getString("database");
  141. /* 144:142 */     host = cfg.getString("host");
  142. /* 145:143 */     port = cfg.getString("port");
  143. /* 146:    */   }
  144. /* 147:    */ }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement