Advertisement
Guest User

Untitled

a guest
May 16th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.94 KB | None | 0 0
  1. public class Mysql {
  2.  
  3. private Main plugin;
  4. public static Connection con;
  5. public String host;
  6. public int port;
  7. public String database;
  8. public String username;
  9. public String password;
  10. public int MySQLSchedulerID;
  11.  
  12. public Mysql(Main main) {
  13. this.plugin = main;
  14.  
  15. host = "ip";
  16. port = 3306;
  17. database = "databasename";
  18. username = "username";
  19. password = "password";
  20.  
  21. }
  22.  
  23.  
  24. public void connect() {
  25. if(!isConnected()) {
  26. try {
  27. con = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database, username ,password);
  28. System.out.println("[MySQL] Verbunden!");
  29.  
  30. onReconnectScheduler();
  31.  
  32. } catch (SQLException e) {
  33. e.printStackTrace();
  34. }
  35. }
  36. }
  37.  
  38. public boolean isConnected() {
  39. return(con == null ? false : true);
  40. }
  41.  
  42. public void disconect() {
  43. if(con != null){
  44. try {
  45. con.close();
  46. } catch (SQLException ex) {
  47. ex.printStackTrace();
  48. }
  49. }
  50. }
  51.  
  52. public ResultSet query(String query) {
  53. if(!isConnected()){
  54. connect();
  55. }
  56. ResultSet rs = null;
  57. try {
  58. Statement stm = con.createStatement();
  59. rs = stm.executeQuery(query);
  60. } catch (SQLException ex) {
  61. connect();
  62. ex.printStackTrace();
  63. }
  64. return rs;
  65. }
  66.  
  67. public void update(String query) {
  68. if(!isConnected()){
  69. connect();
  70. }
  71. try {
  72. Statement stm = con.createStatement();
  73. System.out.println("Ausgefuehrtes Abfrage: "+query);
  74. stm.executeUpdate(query);
  75.  
  76. } catch (SQLException ex) {
  77. connect();
  78. ex.printStackTrace();
  79. }
  80. }
  81. private void onReconnectScheduler()
  82. {
  83. MySQLSchedulerID = Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable()
  84. {
  85. public void run()
  86. {
  87. onReconnect();
  88. }
  89. }, 20 * 60 * 60 *6, 20 * 60 * 60 *6);
  90. }
  91.  
  92. private void onReconnect()
  93. {
  94. if(con != null)
  95. {
  96. try
  97. {
  98. con.close();
  99. System.out.println("[MySQL] MySQL-Verbindung beendet!");
  100. }
  101.  
  102. catch (SQLException e)
  103. {
  104. System.err.println("[MySQL] MySQL-Verbindung konnte nicht getrennt werden!");
  105. e.printStackTrace();
  106. }
  107. }
  108.  
  109. Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable()
  110. {
  111. public void run()
  112. {
  113. try
  114. {
  115. con = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database, username ,password);
  116. System.out.println("[MySQL] MySQL-Verbindung hergestellt!");
  117. }
  118.  
  119. catch (SQLException e)
  120. {
  121. System.err.println("[MySQL] MySQL-Verbindung konnte nicht hergestellt werden!");
  122. e.printStackTrace();
  123. }
  124. }
  125. }, 1L);
  126. }
  127.  
  128. public boolean isInDatabase(Player target, String tabelle, String spalte) throws SQLException {
  129. String uuid = target.getUniqueId().toString();
  130.  
  131. boolean isInDatabase = false;
  132.  
  133. ResultSet rs = query("SELECT * FROM "+tabelle+" WHERE "+spalte+" = '" + uuid + "'");
  134.  
  135. if(rs.next()){
  136. isInDatabase = true;
  137. }else{
  138. isInDatabase = false;
  139.  
  140. }
  141.  
  142. return Boolean.valueOf(isInDatabase).booleanValue();
  143. }
  144.  
  145. public boolean isInDatabase(String target, String tabelle, String spalte) throws SQLException {
  146.  
  147.  
  148. boolean isInDatabase = false;
  149.  
  150. ResultSet rs = query("SELECT * FROM "+tabelle+" WHERE "+spalte+" = '" + target + "'");
  151.  
  152. if(rs.next()){
  153. isInDatabase = true;
  154. }else{
  155. isInDatabase = false;
  156. }
  157.  
  158. return Boolean.valueOf(isInDatabase).booleanValue();
  159. }
  160.  
  161. public boolean isInDatabase(int target, String tabelle, String spalte) throws SQLException {
  162.  
  163.  
  164. boolean isInDatabase = false;
  165.  
  166. ResultSet rs = query("SELECT * FROM "+tabelle+" WHERE "+spalte+" = '" + target + "'");
  167.  
  168. if(rs.next()){
  169. isInDatabase = true;
  170. }else{
  171. isInDatabase = false;
  172.  
  173. }
  174.  
  175. return Boolean.valueOf(isInDatabase).booleanValue();
  176.  
  177.  
  178. }
  179.  
  180. public ResultSet getResult(String query){
  181. if(isConnected()){
  182. try {
  183. return con.createStatement().executeQuery(query);
  184. } catch (SQLException e) {
  185. // TODO Auto-generated catch block
  186. e.printStackTrace();
  187. }
  188. }else{
  189. connect();
  190. getResult(query);
  191. }
  192.  
  193.  
  194. return null;
  195.  
  196. }
  197.  
  198. public ResultSet getPrepairedResult(String query){
  199. if(isConnected()){
  200. try {
  201. return con.prepareStatement(query).executeQuery();
  202. } catch (SQLException e) {
  203. e.printStackTrace();
  204. }
  205. }else{
  206. connect();
  207. getPrepairedResult(query);
  208. }
  209. return null;
  210. }
  211.  
  212. public void createTableIfNotExist(String query){
  213. update("CREATE TABLE IF NOT EXIST "+query);
  214. }
  215.  
  216. public void createTable(String query){
  217. update("CREATE TABLE "+query);
  218. }
  219.  
  220. public void createDatabaseEintrag(String table, String spalten, String werte){
  221. update("INSERT INTO "+table+"("+spalten+") VALUES("+werte+")");
  222. }
  223.  
  224. public void setWertInTable(String table, String spalte, String wert, String fromSpalte, String wertFromSpalte){
  225. update("UPDATE "+table+" SET "+spalte+"='"+wert+"' WHERE "+fromSpalte+"='"+wertFromSpalte+"'");
  226. }
  227.  
  228. public void setWertInTable(String table, String spalte, int wert, String fromSpalte, String wertFromSpalte){
  229. update("UPDATE "+table+" SET "+spalte+"="+wert+" WHERE "+fromSpalte+"='"+wertFromSpalte+"'");
  230. }
  231.  
  232. public void setNULLInTable(String table, String spalte, String fromSpalte, String wertFromSpalte){
  233. update("UPDATE "+table+" SET "+spalte+"="+null+" WHERE "+fromSpalte+"='"+wertFromSpalte+"'");
  234. }
  235. public void setWertInTable(String table, String spalte, double wert, String fromSpalte, String wertFromSpalte){
  236. update("UPDATE "+table+" SET "+spalte+"='"+wert+"' WHERE "+fromSpalte+"='"+wertFromSpalte+"'");
  237. }
  238.  
  239. public ResultSet getWert(String table, String spalte, String fromSpalte){
  240. return getResult("SELECT * FROM "+table+" WHERE "+spalte+ " ='"+fromSpalte+"'");
  241. }
  242.  
  243. public ResultSet getWert(String table, String spalte, int fromSpalte){
  244. return getResult("SELECT * FROM "+table+" WHERE "+spalte+ " ="+fromSpalte);
  245. }
  246.  
  247. public ResultSet getWert(String table, String spalte, double fromSpalte){
  248. return getResult("SELECT * FROM "+table+" WHERE "+spalte+ " ="+fromSpalte);
  249. }
  250. public ResultSet getWertfromNULL(String table, String spalte){
  251. return getResult("SELECT * FROM "+table+" WHERE "+spalte+ " ="+null);
  252. }
  253.  
  254. public ResultSet sortierTable(String table, String bySpalte){
  255. return getPrepairedResult("SELECT * FROM "+table+" ORDER BY "+bySpalte+" DESC");
  256. }
  257.  
  258. public ResultSet giveTop3(String table, String bySpalte){
  259. return getPrepairedResult("SELECT * FROM "+table+" ORDER BY "+bySpalte+" DESC LIMIT 3");
  260. }
  261. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement