Advertisement
Guest User

Untitled

a guest
Nov 10th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.93 KB | None | 0 0
  1. package de.agentslim.main;
  2.  
  3. import org.bukkit.configuration.file.YamlConfiguration;
  4. import org.bukkit.plugin.java.JavaPlugin;
  5.  
  6. import java.io.File;
  7. import java.io.IOException;
  8. import java.sql.*;
  9.  
  10. public class MySQL
  11. {
  12. private JavaPlugin plugin;
  13.  
  14. private Connection connection = null;
  15. private String host = "";
  16. private int port = 0;
  17. private String username = "";
  18. private String password = "";
  19. private String database = "";
  20.  
  21. public MySQL(JavaPlugin plugin)
  22. {
  23. this.plugin = plugin;
  24.  
  25. File file = new File(this.plugin.getDataFolder(), "db.yml");
  26.  
  27. YamlConfiguration configuration = YamlConfiguration.loadConfiguration(file);
  28.  
  29. if (!file.exists())
  30. {
  31. configuration.set("MySQL.host", "localhost");
  32. configuration.set("MySQL.port", 3306);
  33. configuration.set("MySQL.user", "root");
  34. configuration.set("MySQL.host", "root");
  35. configuration.set("MySQL.host", "minecraft_plugins");
  36.  
  37. try
  38. {
  39. configuration.save(file);
  40. }
  41. catch (IOException e)
  42. {
  43. e.printStackTrace();
  44. }
  45. }
  46.  
  47. this.host = configuration.getString("MySQL.host");
  48. this.port = configuration.getInt("MySQL.port");
  49. this.username = configuration.getString("MySQL.user");
  50. this.password = configuration.getString("MySQL.password");
  51. this.database = configuration.getString("MySQL.database");
  52. }
  53.  
  54. public boolean connect()
  55. {
  56. try
  57. {
  58. this.connection = DriverManager.getConnection("jdbc:mysql://" + this.host + ":" + this.port + "/" + this.database + "?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8", this.username, this.password);
  59. plugin.getLogger().info("Database connection established!");
  60. }
  61. catch (SQLException e)
  62. {
  63. this.plugin.getLogger().warning("Fehler beim verbinden zur Datenbank!");
  64. }
  65. return this.connection != null;
  66. }
  67.  
  68. public void disconnect()
  69. {
  70. if (isConnected())
  71. {
  72. try
  73. {
  74. this.connection.close();
  75. }
  76. catch (SQLException ignored)
  77. {
  78. }
  79. finally
  80. {
  81. this.connection = null;
  82. }
  83. }
  84. }
  85.  
  86. public boolean isConnected()
  87. {
  88. return this.connection != null;
  89. }
  90.  
  91. public void Update(String qry)
  92. {
  93. Statement stmt = null;
  94. try
  95. {
  96. stmt = this.connection.createStatement();
  97. }
  98. catch (SQLException e)
  99. {
  100. this.plugin.getLogger().severe("MySQL failed to create Statement [Update]!" + e.getMessage());
  101. }
  102. try
  103. {
  104. stmt.executeUpdate(qry);
  105. }
  106. catch (SQLException e)
  107. {
  108. this.plugin.getLogger().severe("MySQL failed to execute Update [Update]!" + e.getMessage());
  109. }
  110. try
  111. {
  112. stmt.close();
  113. }
  114. catch (SQLException e)
  115. {
  116. this.plugin.getLogger().severe("MySQL failed to close Statement [Update]!" + e.getMessage());
  117. }
  118. }
  119.  
  120. public ResultSet Query(String qry)
  121. {
  122. ResultSet rs = null;
  123. Statement stmt = null;
  124. try
  125. {
  126. stmt = this.connection.createStatement();
  127. }
  128. catch (SQLException e)
  129. {
  130. this.plugin.getLogger().severe("MySQL failed to create Statement [Query]! " + e.getMessage());
  131. }
  132. try
  133. {
  134. rs = stmt.executeQuery(qry);
  135. }
  136. catch (SQLException e)
  137. {
  138. this.plugin.getLogger().severe("MySQL failed to execute Query [Query]! " + e.getMessage());
  139. }
  140. return rs;
  141. }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement