Advertisement
Guest User

Untitled

a guest
Jul 18th, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.93 KB | None | 0 0
  1. package de.steppvp.kitpvp.mysql;
  2.  
  3. import de.steppvp.kitpvp.main.Main;
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.PreparedStatement;
  7. import java.sql.ResultSet;
  8. import java.sql.SQLException;
  9. import java.sql.Statement;
  10. import org.bukkit.Bukkit;
  11.  
  12. public class MySQL {
  13.  
  14. public static Connection con;
  15. public static MySQL mysql;
  16. public static String user = "root";
  17. public static String pass = "kikidiki777";
  18. public static String host = "127.0.0.1";
  19. public static String db = "minecraft";
  20.  
  21. public static void connect() {
  22. try {
  23. con = DriverManager.getConnection("jdbc:mysql://" + host + ":3306/" + db + "?autoReconnect=true", user, pass);
  24. Bukkit.getConsoleSender().sendMessage("Die MySQL-Verbindung wurde erfolgreich hergestellt.");
  25. update("CREATE TABLE IF NOT EXISTS Kopfgeld (UUID varchar(64), Kopfgeld int)");
  26. update("CREATE TABLE IF NOT EXISTS Money (UUID varchar(64), Money int)");
  27. update("CREATE TABLE IF NOT EXISTS Stats (UUID varchar(64), KILLS int, DEATHS int)");
  28. } catch (SQLException e) {
  29. Bukkit.getConsoleSender().sendMessage("Die MySQL-Verbindung ist fehlgeschlagen!");
  30. }
  31. }
  32.  
  33. public static void close() {
  34. try {
  35. if (con != null) {
  36. con.close();
  37. con = null;
  38. Bukkit.getConsoleSender().sendMessage("Die MySQL-Verbindung wurde getrennt.");
  39. }
  40. } catch (Exception e) {
  41. System.err.println(e);
  42. }
  43. }
  44.  
  45. public static void update(String qry) {
  46. try {
  47. Statement stmt = con.createStatement();
  48. stmt.executeUpdate(qry);
  49. } catch (Exception e) {
  50. System.err.println(e);
  51. }
  52. }
  53.  
  54. public static ResultSet Query(String query) {
  55. ResultSet rs = null;
  56. try {
  57. Statement stmt = con.createStatement();
  58. stmt.executeQuery(query);
  59. return stmt.getResultSet();
  60. } catch (Exception e) {
  61. System.err.println(e);
  62. }
  63. return rs;
  64. }
  65.  
  66. public static String getFirstString(ResultSet rs, int l, String re, int t) {
  67. try {
  68. while (rs.next()) {
  69. if (rs.getString(l).equalsIgnoreCase(re)) {
  70. return rs.getString(t);
  71. }
  72. }
  73. } catch (Exception localException) {}
  74. return null;
  75. }
  76.  
  77. public static int getFirstInt(ResultSet rs, int l, String re, int t) {
  78. try {
  79. while (rs.next()) {
  80. if (rs.getString(l).equalsIgnoreCase(re)) {
  81. return rs.getInt(t);
  82. }
  83. }
  84. } catch (Exception localException) {}
  85. return 0;
  86. }
  87.  
  88. public static Connection getConnection() {
  89. return con;
  90. }
  91.  
  92. public static boolean isConnected() {
  93. return con != null;
  94. }
  95.  
  96. public static ResultSet getResult(String qry) {
  97. if (isConnected()) {
  98. try {
  99. return con.createStatement().executeQuery(qry);
  100. } catch (SQLException e) {
  101. e.printStackTrace();
  102. }
  103. }
  104. return null;
  105. }
  106.  
  107. public static void closeRessources(ResultSet rs, PreparedStatement st) {
  108. if (rs != null) {
  109. try {
  110. rs.close();
  111. } catch (SQLException localSQLException) {}
  112. }
  113. if (st != null) {
  114. try {
  115. st.close();
  116. } catch (SQLException localSQLException1) {}
  117. }
  118. }
  119.  
  120. public static void close(PreparedStatement st, ResultSet rs) {
  121. try {
  122. if (st != null) {
  123. st.close();
  124. }
  125. if (rs != null) {
  126. rs.close();
  127. }
  128. } catch (Exception localException) {}
  129. }
  130.  
  131. public static void executeUpdate(String statement) {
  132. try {
  133. PreparedStatement st = con.prepareStatement(statement);
  134. st.executeUpdate();
  135. close(st, null);
  136. } catch (Exception e) {
  137. Main.getInstance().getLogger().warning("executeUpdate konnte nicht ausgefuehrt werden: " + e.getMessage());
  138. e.printStackTrace();
  139. }
  140. }
  141.  
  142. public static void executeUpdate(PreparedStatement statement) {
  143. try {
  144. statement.executeUpdate();
  145. close(statement, null);
  146. } catch (Exception e) {
  147. Main.getInstance().getLogger().warning("executeUpdate konnte nicht ausgefuehrt werden: " + e.getMessage());
  148. e.printStackTrace();
  149. }
  150. }
  151.  
  152. public static ResultSet executeQuery(String statement) {
  153. try {
  154. PreparedStatement st = con.prepareStatement(statement);
  155. return st.executeQuery();
  156. } catch (Exception e) {
  157. Main.getInstance().getLogger().warning("executeQuery konnte nicht ausgefuehrt werden: " + e.getMessage());
  158. }
  159. return null;
  160. }
  161.  
  162. public static ResultSet executeQuery(PreparedStatement statement) {
  163. try {
  164. return statement.executeQuery();
  165. } catch (Exception e) {
  166. Main.getInstance().getLogger().warning("executeQuery konnte nicht ausgefuehrt werden: " + e.getMessage());
  167. }
  168. return null;
  169. }
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement