Guest User

Untitled

a guest
Aug 13th, 2022
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.13 KB | None | 0 0
  1. package net.danh.zzonecore.Database;
  2.  
  3. import net.danh.zzonecore.ZZoneCore;
  4. import net.danh.zzonecore.mZZone.PlayerData;
  5. import org.bukkit.plugin.java.JavaPlugin;
  6.  
  7. import java.sql.Connection;
  8. import java.sql.PreparedStatement;
  9. import java.sql.ResultSet;
  10. import java.sql.SQLException;
  11. import java.util.UUID;
  12. import java.util.logging.Level;
  13.  
  14. public abstract class Database {
  15. // The name of the table we created back in SQLite class.
  16. public String table = "playerdata";
  17. JavaPlugin plugin;
  18. Connection connection;
  19.  
  20. public Database(JavaPlugin instance) {
  21. plugin = instance;
  22. }
  23.  
  24. public abstract Connection getSQLConnection();
  25.  
  26. public abstract void load();
  27.  
  28. public void initialize() {
  29. connection = getSQLConnection();
  30. try {
  31. PreparedStatement ps = connection.prepareStatement("SELECT * FROM " + table + " WHERE player = ?");
  32. ResultSet rs = ps.executeQuery();
  33. close(ps, rs);
  34.  
  35. } catch (SQLException ex) {
  36. ZZoneCore.getZZ().getLogger().log(Level.SEVERE, "Unable to retrieve connection", ex);
  37. }
  38. }
  39.  
  40. public PlayerData getPlayerData(UUID uuid) {
  41. Connection conn = null;
  42. PreparedStatement ps = null;
  43. ResultSet rs;
  44. PlayerData playerData;
  45. try {
  46. conn = getSQLConnection();
  47. ps = conn.prepareStatement("SELECT * FROM " + table + " WHERE player = '" + uuid + "';");
  48. rs = ps.executeQuery();
  49. if (rs.next()) {
  50. playerData = new PlayerData(UUID.fromString(rs.getString("player")), rs.getInt("xp"), rs.getInt("level"), rs.getInt("mana"), rs.getInt("max_mana"), rs.getInt("stamina"), rs.getInt("max_stamina")); // Return the players amount of kills. If you wanted to get total (just a random number for an example for you guys) You would change this to total!
  51. return playerData;
  52. } else {
  53. return null;
  54. }
  55. } catch (SQLException ex) {
  56. ZZoneCore.getZZ().getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex);
  57. } finally {
  58. try {
  59. if (ps != null)
  60. ps.close();
  61. if (conn != null)
  62. conn.close();
  63. } catch (SQLException ex) {
  64. ZZoneCore.getZZ().getLogger().log(Level.SEVERE, Errors.sqlConnectionClose(), ex);
  65. }
  66. }
  67. return null;
  68. }
  69.  
  70. public void createTable(PlayerData playerData) {
  71. Connection conn = null;
  72. PreparedStatement ps = null;
  73. try {
  74. conn = getSQLConnection();
  75. ps = conn.prepareStatement("INSERT INTO " + table + " (player,xp,level,mana,max_mana,stamina,max_stamina) VALUES(?,?,?,?,?,?,?)"); // IMPORTANT. In SQLite class, We made 3 colums. player, Kills, Total.
  76. ps.setString(1, playerData.getPlayer().toString());
  77. ps.setInt(2, playerData.getXP());
  78. ps.setInt(3, playerData.getLevel());
  79. ps.setInt(4, playerData.getMana());
  80. ps.setInt(5, playerData.getMaxMana());
  81. ps.setInt(6, playerData.getStamina());
  82. ps.setInt(7, playerData.getMaxMana());
  83. ps.executeUpdate();
  84. } catch (SQLException ex) {
  85. plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex);
  86. } finally {
  87. try {
  88. if (ps != null)
  89. ps.close();
  90. if (conn != null)
  91. conn.close();
  92. } catch (SQLException ex) {
  93. plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionClose(), ex);
  94. }
  95. }
  96. }
  97.  
  98. public void updateTable(PlayerData playerData) {
  99. Connection conn = null;
  100. PreparedStatement ps = null;
  101. try {
  102. conn = getSQLConnection();
  103. ps = conn.prepareStatement("UPDATE " + table + " SET player = ?, xp = ?, level = ?, mana = ?, max_mana = ?, stamina = ?, max_stamina = ?");
  104. ps.setString(1, playerData.getPlayer().toString());
  105. ps.setInt(2, playerData.getXP());
  106. ps.setInt(3, playerData.getLevel());
  107. ps.setInt(4, playerData.getMana());
  108. ps.setInt(5, playerData.getMaxMana());
  109. ps.setInt(6, playerData.getStamina());
  110. ps.setInt(7, playerData.getMaxMana());
  111. ps.executeUpdate();
  112. } catch (SQLException ex) {
  113. plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex);
  114. } finally {
  115. try {
  116. if (ps != null)
  117. ps.close();
  118. if (conn != null)
  119. conn.close();
  120. } catch (SQLException ex) {
  121. plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionClose(), ex);
  122. }
  123. }
  124. }
  125.  
  126. public void close(PreparedStatement ps, ResultSet rs) {
  127. try {
  128. if (ps != null)
  129. ps.close();
  130. if (rs != null)
  131. rs.close();
  132. } catch (SQLException ex) {
  133. Error.close(ex);
  134. }
  135. }
  136. }
  137.  
Advertisement
Add Comment
Please, Sign In to add comment