Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package frikoskyy.StoneGen;
- import java.io.File;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.HashMap;
- import org.bukkit.Bukkit;
- import org.bukkit.Location;
- import org.bukkit.Material;
- public class SQLManager {
- private static Connection conn;
- private static Statement st;
- public SQLManager(){
- try{
- Class.forName("org.sqlite.JDBC");
- conn = DriverManager.getConnection("jdbc:sqlite:" + Main.getInstance().getDataFolder() + File.separator + "generators" + ".db");
- } catch (ClassNotFoundException e) {
- Bukkit.getConsoleSender().sendMessage(Main.fixColors("&cWystapil blad podczas nawiazywania polaczenia z baza danych ! &6Blad : &4" + e.getMessage()));
- } catch (SQLException e) {
- Bukkit.getConsoleSender().sendMessage(Main.fixColors("&cWystapil blad podczas nawiazywania polaczenia z baza danych ! &6Blad : &4" + e.getMessage()));
- }
- try {
- st = conn.createStatement();
- } catch (SQLException e) {
- Bukkit.getConsoleSender().sendMessage(Main.fixColors("&cWystapil blad podczas nawiazywania polaczenia z baza danych ! &6Blad : &4" + e.getMessage()));
- }
- createTables();
- Data.setGenerators(getStoneCreators());
- }
- private void createTables(){
- try{
- st = conn.createStatement();
- String query = "CREATE TABLE IF NOT EXISTS generators(X INT , Y INT , Z INT)";
- st.execute(query);
- } catch (SQLException e){
- Bukkit.getConsoleSender().sendMessage(Main.fixColors("&cWystapil blad podczas tworzenia tabel ! &6Blad : &4" + e.getMessage()));
- }
- }
- private HashMap<Location, StoneGenerator> getStoneCreators(){
- HashMap<Location, StoneGenerator> hm = new HashMap<Location, StoneGenerator>();
- try{
- String query = "SELECT * FROM generators;";
- st = conn.createStatement();
- ResultSet rs = st.executeQuery(query);
- while (rs.next()){
- int x = rs.getInt("X");
- int y = rs.getInt("Y");
- int z = rs.getInt("Z");
- Location location = new Location(Bukkit.getWorlds().get(0) , x , y , z);
- StoneGenerator creator = new StoneGenerator(location);
- location.getBlock().setType(Material.STONE);
- hm.put(location, creator);
- }
- }catch (SQLException ex){
- Bukkit.getConsoleSender().sendMessage("§cWystapil blad podczas wczytywania generatorow kamienia §3: §6" + ex.getMessage());
- ex.printStackTrace();
- }
- return hm;
- }
- public static void saveStoneCreateors() {
- try {
- for (StoneGenerator creator : Data.getAllGenerators().values()) {
- Location loc = creator.getLoc();
- int x = loc.getBlockX();
- int y = loc.getBlockY();
- int z = loc.getBlockZ();
- String query = "UPDATE generators"
- + " WHERE X=" + x
- + " AND Y=" + y
- + " AND Z=" + z
- + ";";
- st = conn.createStatement();
- st.executeUpdate(query);
- }
- } catch (SQLException e){
- Bukkit.getConsoleSender().sendMessage(Main.fixColors("cWystapil blad podczas zapisywania generatorow kamienia §3: §6" + e.getMessage()));
- e.printStackTrace();
- }
- }
- public static void removeGenerator(Location location){
- try {
- int x = location.getBlockX();
- int y = location.getBlockY();
- int z = location.getBlockZ();
- String query = "DELETE FROM generators"
- + " WHERE X=" + x
- + " AND Y=" + y
- + " AND Z=" + z
- + ";";
- st = conn.createStatement();
- st.executeUpdate(query);
- }catch (SQLException e){
- }
- }
- public static void insertGenerator(Location location){
- int x = location.getBlockX();
- int y = location.getBlockY();
- int z = location.getBlockZ();
- try {
- String query = "INSERT INTO generators VALUES("
- + x + ","
- + y + ","
- + z + ","
- + " )";
- st = conn.createStatement();
- st.execute(query);
- } catch(SQLException ex) {
- Bukkit.getConsoleSender().sendMessage("§cWystapil blad podczas umieszcania generatora w tabeli §3: §6" + ex.getMessage());
- ex.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement