Advertisement
Guest User

Untitled

a guest
Nov 26th, 2014
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.63 KB | None | 0 0
  1. package frikoskyy.StoneGen;
  2.  
  3. import java.io.File;
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.sql.Statement;
  9. import java.util.HashMap;
  10.  
  11. import org.bukkit.Bukkit;
  12. import org.bukkit.Location;
  13. import org.bukkit.Material;
  14.  
  15.  
  16.  
  17. public class SQLManager {
  18.  
  19. private static Connection conn;
  20. private static Statement st;
  21.  
  22.  
  23. public SQLManager(){
  24. try{
  25. Class.forName("org.sqlite.JDBC");
  26. conn = DriverManager.getConnection("jdbc:sqlite:" + Main.getInstance().getDataFolder() + File.separator + "generators" + ".db");
  27. } catch (ClassNotFoundException e) {
  28. Bukkit.getConsoleSender().sendMessage(Main.fixColors("&cWystapil blad podczas nawiazywania polaczenia z baza danych ! &6Blad : &4" + e.getMessage()));
  29. } catch (SQLException e) {
  30. Bukkit.getConsoleSender().sendMessage(Main.fixColors("&cWystapil blad podczas nawiazywania polaczenia z baza danych ! &6Blad : &4" + e.getMessage()));
  31. }
  32.  
  33. try {
  34. st = conn.createStatement();
  35. } catch (SQLException e) {
  36. Bukkit.getConsoleSender().sendMessage(Main.fixColors("&cWystapil blad podczas nawiazywania polaczenia z baza danych ! &6Blad : &4" + e.getMessage()));
  37. }
  38. createTables();
  39. Data.setGenerators(getStoneCreators());
  40. }
  41.  
  42. private void createTables(){
  43. try{
  44. st = conn.createStatement();
  45. String query = "CREATE TABLE IF NOT EXISTS generators(X INT , Y INT , Z INT)";
  46. st.execute(query);
  47. } catch (SQLException e){
  48. Bukkit.getConsoleSender().sendMessage(Main.fixColors("&cWystapil blad podczas tworzenia tabel ! &6Blad : &4" + e.getMessage()));
  49. }
  50. }
  51.  
  52. private HashMap<Location, StoneGenerator> getStoneCreators(){
  53. HashMap<Location, StoneGenerator> hm = new HashMap<Location, StoneGenerator>();
  54.  
  55. try{
  56. String query = "SELECT * FROM generators;";
  57. st = conn.createStatement();
  58. ResultSet rs = st.executeQuery(query);
  59. while (rs.next()){
  60. int x = rs.getInt("X");
  61. int y = rs.getInt("Y");
  62. int z = rs.getInt("Z");
  63. Location location = new Location(Bukkit.getWorlds().get(0) , x , y , z);
  64. StoneGenerator creator = new StoneGenerator(location);
  65. location.getBlock().setType(Material.STONE);
  66. hm.put(location, creator);
  67.  
  68.  
  69. }
  70. }catch (SQLException ex){
  71. Bukkit.getConsoleSender().sendMessage("§cWystapil blad podczas wczytywania generatorow kamienia §3: §6" + ex.getMessage());
  72. ex.printStackTrace();
  73. }
  74. return hm;
  75. }
  76.  
  77.  
  78. public static void saveStoneCreateors() {
  79. try {
  80. for (StoneGenerator creator : Data.getAllGenerators().values()) {
  81. Location loc = creator.getLoc();
  82. int x = loc.getBlockX();
  83. int y = loc.getBlockY();
  84. int z = loc.getBlockZ();
  85. String query = "UPDATE generators"
  86. + " WHERE X=" + x
  87. + " AND Y=" + y
  88. + " AND Z=" + z
  89. + ";";
  90.  
  91. st = conn.createStatement();
  92. st.executeUpdate(query);
  93. }
  94.  
  95.  
  96. } catch (SQLException e){
  97. Bukkit.getConsoleSender().sendMessage(Main.fixColors("cWystapil blad podczas zapisywania generatorow kamienia §3: §6" + e.getMessage()));
  98. e.printStackTrace();
  99. }
  100.  
  101. }
  102.  
  103. public static void removeGenerator(Location location){
  104. try {
  105. int x = location.getBlockX();
  106. int y = location.getBlockY();
  107. int z = location.getBlockZ();
  108. String query = "DELETE FROM generators"
  109. + " WHERE X=" + x
  110. + " AND Y=" + y
  111. + " AND Z=" + z
  112. + ";";
  113. st = conn.createStatement();
  114. st.executeUpdate(query);
  115.  
  116. }catch (SQLException e){
  117.  
  118. }
  119. }
  120.  
  121. public static void insertGenerator(Location location){
  122. int x = location.getBlockX();
  123. int y = location.getBlockY();
  124. int z = location.getBlockZ();
  125. try {
  126. String query = "INSERT INTO generators VALUES("
  127. + x + ","
  128. + y + ","
  129. + z + ","
  130. + " )";
  131. st = conn.createStatement();
  132. st.execute(query);
  133. } catch(SQLException ex) {
  134. Bukkit.getConsoleSender().sendMessage("§cWystapil blad podczas umieszcania generatora w tabeli §3: §6" + ex.getMessage());
  135. ex.printStackTrace();
  136. }
  137. }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement