Guest User

sqltemplate

a guest
Apr 1st, 2021
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.86 KB | None | 0 0
  1. public class SQLiteHandler {
  2. private File pluginDataFolder;
  3. private String fileName;
  4. private String sqlDefaultStatement;
  5. private String table;
  6.  
  7. private Connection connection;
  8.  
  9. //We ask for the plugins folder,how the file should be named,how the table should be named(for later uses),and a default statement allowing to specify columns
  10. public SQLiteHandler(@NotNull File pluginDataFolder, @NotNull String fileName,String table,@NotNull String sqlDefaultStatement){
  11. this.pluginDataFolder=pluginDataFolder;
  12. this.fileName=fileName;
  13. this.sqlDefaultStatement = sqlDefaultStatement;
  14. this.table=table;
  15. }
  16.  
  17.  
  18. /*
  19. An example on how to use the constructor
  20.  
  21. private SQLiteHandler sqLiteHandler = new SQLiteHandler(this.getDataFolder(),"chunkHoppers","chunkHoppers",
  22. "CREATE TABLE IF NOT EXISTS chunkHoppers (" +
  23. "`uuid` INTEGER PRIMARY KEY," +
  24. "`world` varchar(50) NOT NULL," +
  25. "`x` INT NOT NULL," +
  26. "`y` INT NOT NULL," +
  27. "`z` INT NOT NULL" +
  28. ");"
  29. );
  30.  
  31.  
  32. */
  33.  
  34.  
  35. public Connection getSQLConnection() {
  36. if(!pluginDataFolder.exists()){
  37. pluginDataFolder.mkdirs();
  38. }
  39. File dataFolder = new File(pluginDataFolder, fileName+".db");
  40. if (!dataFolder.exists()){
  41. try {
  42. dataFolder.createNewFile();
  43. } catch (IOException e) {
  44. e.printStackTrace();
  45. }
  46. }
  47. try {
  48. if(connection!=null&&!connection.isClosed()){
  49. return connection;
  50. }
  51. Class.forName("org.sqlite.JDBC");
  52. connection = DriverManager.getConnection("jdbc:sqlite:" + dataFolder);
  53. return connection;
  54. } catch (SQLException ex) {
  55. ex.printStackTrace();
  56. } catch (ClassNotFoundException ex) {
  57. ex.printStackTrace();
  58. }
  59. return null;
  60. }
  61.  
  62.  
  63. ///////
  64. //Call this onEnable() "load()"
  65. /////////
  66. public void load() {
  67. connection = getSQLConnection();
  68. try {
  69. Statement s = connection.createStatement();
  70. s.executeUpdate(sqlDefaultStatement);
  71. s.close();
  72. } catch (SQLException e) {
  73. e.printStackTrace();
  74. }
  75. initialize();
  76. }
  77.  
  78. private void initialize(){
  79. connection = getSQLConnection();
  80. try{
  81. PreparedStatement ps = connection.prepareStatement("SELECT * FROM " + table + " WHERE uuid = ?");
  82. ResultSet rs = ps.executeQuery();
  83. close(ps,rs);
  84.  
  85. } catch (SQLException ex) {
  86. ex.printStackTrace();
  87. }
  88. }
  89.  
  90. private void close(PreparedStatement ps,ResultSet rs){
  91. try {
  92. if (ps != null)
  93. ps.close();
  94. if (rs != null)
  95. rs.close();
  96. } catch (SQLException ex) {
  97. ex.printStackTrace();
  98. }
  99. }
  100. private void close(PreparedStatement ps,Connection conn){
  101. try {
  102. if (ps != null)
  103. ps.close();
  104. if (conn != null)
  105. conn.close();
  106. } catch (SQLException ex) {
  107. ex.printStackTrace();
  108. }
  109. }
  110.  
  111.  
  112. //A method returning all entry's in the database,in this case used to get hoppers locations
  113. public List<Block> getAllHoppers(){
  114. List<Block> toReturn = new ArrayList<>();
  115. Connection conn = null;
  116. PreparedStatement ps = null;
  117. ResultSet rs;
  118. try{
  119. conn = getSQLConnection();
  120. ps = conn.prepareStatement("SELECT * FROM "+table);
  121. rs = ps.executeQuery();
  122. while(rs.next()){
  123. String world;
  124. int x;
  125. int y;
  126. int z;
  127. world = rs.getString("world");
  128. x = rs.getInt("x");
  129. y = rs.getInt("y");
  130. z = rs.getInt("z");
  131. Block b = new Location(Bukkit.getWorld(world),x,y,z).getBlock();
  132. if(b.getType()!= Material.HOPPER){
  133. removeHopper(b);
  134. }else {
  135. toReturn.add(b);
  136. }
  137. }
  138.  
  139. } catch (SQLException e) {
  140.  
  141. }finally {
  142. close(ps,conn);
  143. }
  144. return toReturn;
  145. }
  146.  
  147. //Add method
  148. public void addHopper(Block toAdd){
  149. Location loc = toAdd.getLocation();
  150. Connection conn = null;
  151. PreparedStatement ps = null;
  152. try {
  153. conn = getSQLConnection();
  154. ps = conn.prepareStatement("INSERT INTO " + table + " (world,x,y,z) VALUES(?,?,?,?)");
  155. ps.setString(1,toAdd.getWorld().getName());
  156. ps.setInt(2,loc.getBlockX());
  157. ps.setInt(3,loc.getBlockY());
  158. ps.setInt(4,loc.getBlockZ());
  159. ps.executeUpdate();
  160. return;
  161. } catch (SQLException ex) {
  162. ex.printStackTrace();
  163. } finally {
  164. close(ps,conn);
  165. }
  166. }
  167. //Remove method
  168. public void removeHopper(Block toRemove){
  169. Location loc = toRemove.getLocation();
  170. Connection conn = null;
  171. PreparedStatement ps = null;
  172. try{
  173. conn = getSQLConnection();
  174. ps = conn.prepareStatement("DELETE FROM "+table +" WHERE world=? AND x=? AND y=? AND z=?");
  175. ps.setString(1,loc.getWorld().getName());
  176. ps.setInt(2,loc.getBlockX());
  177. ps.setInt(3,loc.getBlockY());
  178. ps.setInt(4,loc.getBlockZ());
  179. ps.executeUpdate();
  180. return;
  181.  
  182. } catch (SQLException e) {
  183. }finally {
  184. close(ps,conn);
  185. }
  186. }
  187. }
Advertisement
Add Comment
Please, Sign In to add comment