package net.saltymc.eaa.util.database; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class DB_Player { public static final String tableName = "Player"; int id = -1; String uuid; public DB_Player(String uuid){ this.uuid = uuid; } private DB_Player(int id, String uuid) { this.id = id; this.uuid = uuid; } public void put() { putPlayer(this); } public DB_Player get(){ return getPlayer(this.uuid); } public static void putPlayer(DB_Player player) { try { PreparedStatement statement; if (player.id != -1) { statement = Postgre.getInstance().connection.prepareStatement("UPDATE " + tableName + " SET id = ?, UUID = ? WHERE id = ?;"); statement.setInt(1, player.id); statement.setString(2, player.uuid); statement.setInt(3, player.id); } else { statement = Postgre.getInstance().connection.prepareStatement("INSERT INTO " + tableName + " (UUID) " + "VALUES(?);"); statement.setString(1, player.uuid); } statement.executeQuery(); } catch (SQLException e){ throw new RuntimeException("Could not safe Player", e); } } public static DB_Player getPlayer(int id){ try { PreparedStatement statement = Postgre.getInstance().connection.prepareStatement("SELECT * FROM " + tableName + " WHERE id = ?"); statement.setInt(1, id); ResultSet rs = statement.executeQuery(); rs.next(); return new DB_Player(rs.getInt("id"), rs.getString("UUID")); } catch (SQLException e){ throw new RuntimeException("Could not put Player into Database!", e); } } public static DB_Player getPlayer(String uuid){ try { PreparedStatement statement = Postgre.getInstance().connection.prepareStatement("SELECT * FROM Player WHERE UUID = ?"); statement.setString(1, uuid); ResultSet rs = statement.executeQuery(); if (rs.next()){ return new DB_Player(rs.getInt("id"), rs.getString("UUID")); } else { //unknown Player - create new DB_Player db_player = new DB_Player(uuid); db_player.put(); return getPlayer(uuid); } } catch (SQLException e){ throw new RuntimeException("Could not get Player from Database!", e); } } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUuid() { return uuid; } public void setUuid(String uuid) { this.uuid = uuid; } }