Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.qualitynode.customspawners.managers;
- import net.aminecraftdev.utils.IExtendedRepository;
- import net.aminecraftdev.utils.database.AbstractDatabase;
- import net.aminecraftdev.utils.serialization.Serialize;
- import org.bukkit.Location;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- /**
- * @author AMinecraftDev
- * @version 1.0.0
- * @since 08-Dec-17
- */
- public class SpawnerRepository implements IExtendedRepository<Location, Integer> {
- private AbstractDatabase abstractDatabase;
- private Connection connection;
- private String tableName;
- public SpawnerRepository(Connection connection, String tableName, AbstractDatabase abstractDatabase) {
- this.connection = connection;
- this.tableName = tableName;
- this.abstractDatabase = abstractDatabase;
- }
- @Override
- public Connection getConnection() {
- try {
- if(this.abstractDatabase.checkConnection(this.connection)) this.abstractDatabase.openConnection(this.connection);
- } catch (SQLException | ClassNotFoundException e) {
- e.printStackTrace();
- }
- return this.connection;
- }
- @Override
- public void DeleteById(Integer integer) {
- }
- @Override
- public void Create(Location location) {
- }
- @Override
- public Location Read(Integer integer) {
- return null;
- }
- @Override
- public void Update(Location location) {
- }
- @Override
- public void Delete(Location location) {
- String sql = "DELETE FROM " + this.tableName + " WHERE location=? LIMIT 1;";
- try(PreparedStatement preparedStatement = getConnection().prepareStatement(sql)) {
- preparedStatement.setString(1, Serialize.serialize(location));
- preparedStatement.execute();
- } catch (SQLException ex) {
- ex.printStackTrace();
- }
- }
- @Override
- public Location[] ReadAll() {
- String sql = "SELECT * FROM " + this.tableName + ";";
- Location[] list = {};
- try(PreparedStatement preparedStatement = getConnection().prepareStatement(sql)) {
- try(ResultSet resultSet = preparedStatement.executeQuery()) {
- while(resultSet.next()) {
- Location location = Serialize.deserializeLocation(resultSet.getString("location"));
- int nextSlot = list.length + 1;
- list = new Location[nextSlot];
- list[nextSlot] = location;
- }
- }
- } catch (SQLException ex) {
- ex.printStackTrace();
- }
- return list;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement