Advertisement
Guest User

Untitled

a guest
Jun 30th, 2016
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.04 KB | None | 0 0
  1. package me.polocraft.rework.Mysql;
  2.  
  3.  
  4. import java.sql.*;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. import java.util.UUID;
  8. import java.util.logging.Level;
  9.  
  10. import me.polocraft.rework.Main;
  11. import me.polocraft.rework.Mysql.PlayerProfile;
  12.  
  13.  
  14. public class Database {
  15.  
  16. String user = "";
  17. String database = "";
  18. String password = "";
  19. String port = "";
  20. String hostname = "";
  21. Connection c = null;
  22.  
  23. public Database(String user, String password, String database, String port, String hostname) {
  24. this.user = user;
  25. this.database = database;
  26. this.password = password;
  27. if (port == null) {
  28. this.port = "3306";
  29. } else {
  30. this.port = port;
  31. }
  32. this.hostname = hostname;
  33. }
  34.  
  35.  
  36. /**
  37. * Open mysql connection
  38. * @return
  39. */
  40. public Connection open() {
  41. try {
  42. Class.forName("com.mysql.jdbc.Driver");
  43. c = DriverManager.getConnection("jdbc:mysql://" + hostname + ":" + port + "/" + database + "?autoReconnect=true", user, password);
  44. System.out.println("MySQL connection successful");
  45. Main.getInstance().getLogger().log(Level.INFO, "MySQL connection successful");
  46. return c;
  47. } catch (SQLException e) {
  48. Main.getInstance().getLogger().log(Level.SEVERE, "MySQL connection has been aborted");
  49. Main.getInstance().getLogger().log(Level.SEVERE, "Reason:" + e.getMessage());
  50. e.printStackTrace();
  51. } catch (ClassNotFoundException e) {
  52. System.out.println("JDBC Driver not found!");
  53. }
  54. return c;
  55. }
  56.  
  57. public Connection getConnection() {
  58. return this.c;
  59. }
  60.  
  61. /**
  62. * Close mysql connection
  63. */
  64. public Connection close(Connection c){
  65. try{
  66. c.close();
  67. } catch (SQLException e) {
  68. Main.getInstance().getLogger().log(Level.SEVERE, "MySQL can't close connection");
  69. e.printStackTrace();
  70. }
  71. return c;
  72. }
  73.  
  74. /**
  75. * Get all tables together and setup one by one
  76. */
  77. public void setupTables(){
  78. setupProfiles();
  79. }
  80.  
  81. /**
  82. * Setup the hub player profiles
  83. */
  84. public void setupProfiles(){
  85. try {
  86. PreparedStatement profiles = c
  87. .prepareStatement("CREATE TABLE IF NOT EXISTS `Core`(`id` int(11) NOT NULL auto_increment,"
  88. + " `UUID` varchar(255) NOT NULL,"
  89. + " `Coins` bigint(255) NOT NULL,"
  90. + " `Tokens` bigint(255) NOT NULL,"
  91. + " PRIMARY KEY(`id`));");
  92. profiles.execute();
  93. profiles.close();
  94. } catch (Exception e) {
  95. e.printStackTrace();
  96. }
  97. }
  98.  
  99. /**
  100. * Try to create a new player profile
  101. * @param uuid
  102. * @return
  103. */
  104. public boolean createPlayerProfile(UUID uuid){
  105. try
  106. {
  107. String coreStatement = "SELECT * FROM Core WHERE UUID = '" + uuid + "';";
  108. ResultSet result = this.c.createStatement().executeQuery(coreStatement);
  109. if (result.next()) {
  110. PlayerProfile.addPlayerProfile(new PlayerProfile(uuid, result.getLong("Tokens"),result.getLong("Coins")));
  111.  
  112. } else {
  113. PlayerProfile.addPlayerProfile(new PlayerProfile(uuid,0,0));
  114. }
  115. }
  116. catch (SQLException e)
  117. {
  118. System.out.println("Error while trying to create PlayerDat! Reason: " + e.getMessage());
  119. return false;
  120. }
  121. return true;
  122. }
  123.  
  124. /**
  125. * Update all data from player profile to the sql database
  126. * @param playerProfile
  127. * @return
  128. */
  129. public boolean updatePlayerProfileSQL(PlayerProfile playerProfile)
  130. {
  131. try
  132. {
  133. String coreStatement = "SELECT * FROM Core WHERE UUID = '" + playerProfile.getUUID() + "'";
  134. ResultSet result = this.c.createStatement().executeQuery(coreStatement);
  135. if (result.next())
  136. {
  137. String coreUpdate = "UPDATE Core SET Tokens = " + playerProfile.getTokens()
  138. + ", Coins = " + playerProfile.getCoins()
  139. + ", Tokens = " + playerProfile.getTokens()
  140. + " WHERE UUID = '"
  141. + playerProfile.getUUID() + "';";
  142. this.c.createStatement().executeUpdate(coreUpdate);
  143. }
  144. else
  145. {
  146. String coreUpdate = "INSERT INTO Core (UUID, Tokens, Coins) VALUES ('"
  147. + playerProfile.getUUID() + "', "
  148. + playerProfile.getTokens() + ", "
  149. + playerProfile.getCoins() + ", ";
  150. this.c.createStatement().executeUpdate(coreUpdate);
  151. }
  152. }
  153. catch (SQLException e)
  154. {
  155. System.out.println("Error while trying to update the MySQL! Reason: " + e.getMessage());
  156. return false;
  157. }
  158. return true;
  159. }
  160.  
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement