Advertisement
Guest User

Untitled

a guest
Dec 7th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1. package com.qualitynode.customspawners.managers;
  2.  
  3. import net.aminecraftdev.utils.IExtendedRepository;
  4. import net.aminecraftdev.utils.database.AbstractDatabase;
  5. import net.aminecraftdev.utils.serialization.Serialize;
  6. import org.bukkit.Location;
  7.  
  8. import java.sql.Connection;
  9. import java.sql.PreparedStatement;
  10. import java.sql.ResultSet;
  11. import java.sql.SQLException;
  12.  
  13. /**
  14. * @author AMinecraftDev
  15. * @version 1.0.0
  16. * @since 08-Dec-17
  17. */
  18. public class SpawnerRepository implements IExtendedRepository<Location, Integer> {
  19.  
  20. private AbstractDatabase abstractDatabase;
  21. private Connection connection;
  22. private String tableName;
  23.  
  24. public SpawnerRepository(Connection connection, String tableName, AbstractDatabase abstractDatabase) {
  25. this.connection = connection;
  26. this.tableName = tableName;
  27. this.abstractDatabase = abstractDatabase;
  28. }
  29.  
  30. @Override
  31. public Connection getConnection() {
  32. try {
  33. if(this.abstractDatabase.checkConnection(this.connection)) this.abstractDatabase.openConnection(this.connection);
  34. } catch (SQLException | ClassNotFoundException e) {
  35. e.printStackTrace();
  36. }
  37.  
  38. return this.connection;
  39. }
  40.  
  41. @Override
  42. public void DeleteById(Integer integer) {
  43.  
  44. }
  45.  
  46. @Override
  47. public void Create(Location location) {
  48.  
  49. }
  50.  
  51. @Override
  52. public Location Read(Integer integer) {
  53. return null;
  54. }
  55.  
  56. @Override
  57. public void Update(Location location) {
  58.  
  59. }
  60.  
  61. @Override
  62. public void Delete(Location location) {
  63. String sql = "DELETE FROM " + this.tableName + " WHERE location=? LIMIT 1;";
  64.  
  65. try(PreparedStatement preparedStatement = getConnection().prepareStatement(sql)) {
  66. preparedStatement.setString(1, Serialize.serialize(location));
  67.  
  68. preparedStatement.execute();
  69. } catch (SQLException ex) {
  70. ex.printStackTrace();
  71. }
  72. }
  73.  
  74. @Override
  75. public Location[] ReadAll() {
  76. String sql = "SELECT * FROM " + this.tableName + ";";
  77. Location[] list = {};
  78.  
  79. try(PreparedStatement preparedStatement = getConnection().prepareStatement(sql)) {
  80. try(ResultSet resultSet = preparedStatement.executeQuery()) {
  81. while(resultSet.next()) {
  82. Location location = Serialize.deserializeLocation(resultSet.getString("location"));
  83. int nextSlot = list.length + 1;
  84.  
  85. list = new Location[nextSlot];
  86. list[nextSlot] = location;
  87. }
  88. }
  89. } catch (SQLException ex) {
  90. ex.printStackTrace();
  91. }
  92.  
  93. return list;
  94. }
  95.  
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement