Advertisement
Guest User

Untitled

a guest
Feb 19th, 2020
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.27 KB | None | 0 0
  1. package me.spongedev.hypexspawners.Connection;
  2.  
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6.  
  7. import org.bukkit.Location;
  8. import org.bukkit.entity.EntityType;
  9.  
  10. import me.spongedev.hypexspawners.Main;
  11. import me.spongedev.hypexspawners.Models.Spawner;
  12. import me.spongedev.hypexspawners.Models.TipoSpawners;
  13.  
  14. public class SpawnerDAO extends DBConnector {
  15.  
  16.  
  17. /*try (Statement stm = connection.createStatement()) {
  18. stm.executeUpdate(String.format("create table %s (%s %s)", "my_table", "id", "varchar(32)");
  19. } catch (Error | Exception e) {
  20. e.printStackTrace();
  21. }*/
  22.  
  23. public static void loadSpawners() {
  24.  
  25. try {
  26. PreparedStatement statement = connection.prepareStatement("SELECT * FROM HypexSpawners");
  27. ResultSet results = statement.executeQuery();
  28. while (results.next()) {
  29. String loc = results.getString("Loc");
  30. String dono = results.getString("Dono");
  31. String tipo = results.getString("Tipo");
  32. int stack = results.getInt("Stack");
  33.  
  34. Location loc2 = Spawner.desarializeLoc(loc);
  35. EntityType tipo2 = TipoSpawners.getEntityType(tipo);
  36.  
  37. Spawner spawn = new Spawner(loc2, dono, tipo2, stack);
  38. Main.plugin.spawnerCache.add(spawn);
  39.  
  40. }
  41. statement.close();
  42. results.close();
  43. }
  44. catch (SQLException ev) {
  45. ev.printStackTrace();
  46. }
  47. }
  48.  
  49. public static void addSpawner(String loc, String dono, String tipo, int stack) {
  50. try {
  51.  
  52. //Vai verificar se ja existe
  53. PreparedStatement statement = connection.prepareStatement("SELECT Loc FROM HypexSpawners WHERE Loc=?");
  54. statement.setString(1, loc);
  55. ResultSet results = statement.executeQuery();
  56. //se nao existir vai adiconar na base de dados
  57. if (!results.next()) {
  58.  
  59. PreparedStatement statement2 = connection.prepareStatement("INSERT INTO HypexSpawners (Loc, Dono, Tipo, Stack) VALUES (?,?,?,?)");
  60. statement2.setString(1, loc);
  61. statement2.setString(2, dono);
  62. statement2.setString(3, tipo);
  63. statement2.setInt(4, stack);
  64. statement2.executeUpdate();
  65. statement2.close();
  66. }
  67. //Se existir vai atualizar o valor do stack caso necessario
  68. else {
  69. PreparedStatement statement3 = connection.prepareStatement("UPDATE HypexSpawners SET Stack=? WHERE Loc=?");
  70. statement3.setInt(1, stack);
  71. statement3.setString(2, loc);
  72. statement3.executeUpdate();
  73. statement3.close();
  74. }
  75. statement.close();
  76. results.close();
  77. }
  78. catch (SQLException e) {
  79. e.printStackTrace();
  80. }
  81. }
  82.  
  83.  
  84. public static void deleteSpawner(String loc) {
  85.  
  86. try {
  87. PreparedStatement statement = connection.prepareStatement("DELETE FROM HypexSpawners WHERE Loc=?");
  88. statement.setString(1, loc);
  89. statement.executeUpdate();
  90. statement.close();
  91. }
  92. catch (SQLException ev) {
  93. ev.printStackTrace();
  94. }
  95. }
  96.  
  97. public static void saveSpawners() {
  98.  
  99. if (!Main.plugin.spawnerCache.isEmpty()) {
  100. for (Spawner spawner : Main.plugin.spawnerCache) {
  101.  
  102. String loc = Spawner.serializeLoc(spawner.getLoc());
  103. String tipo = TipoSpawners.getTipoString(spawner.getTipo());
  104. addSpawner(loc, spawner.getDono(), tipo, spawner.getStackados());
  105. }
  106. DBConnector.closeConnection();
  107. }
  108. else {
  109. DBConnector.closeConnection();
  110. }
  111. }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement