Advertisement
Guest User

Untitled

a guest
Sep 24th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | None | 0 0
  1. package me.phil.mw;
  2.  
  3. import java.io.PrintStream;
  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.  
  11. public class MySQL
  12. {
  13. public static String host = "";
  14. public static String port = "3306";
  15. public static String database = "";
  16. public static String usrname = "";
  17. public static String passwd = "";
  18. public static Connection con;
  19.  
  20. public static void connect()
  21. {
  22. if (!connected()) {
  23. try
  24. {
  25. con = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database + "?user=" + usrname + "&password=" + passwd + "&autoReconnect=true");
  26.  
  27. System.out.println("[PSQL] MySQL Verbindung aufgebaut!");
  28. }
  29. catch (SQLException e)
  30. {
  31. System.out.println("[PSQL] " + e.getMessage());
  32. }
  33. }
  34. }
  35.  
  36.  
  37.  
  38.  
  39. public static void disconnect()
  40. {
  41. if (connected()) {
  42. try
  43. {
  44. con.close();
  45. System.out.println("[PSQL] MySQL Verbindung geschlossen!");
  46. }
  47. catch (SQLException e)
  48. {
  49. e.printStackTrace();
  50. }
  51. }
  52. }
  53.  
  54. public AsyncMySQL(Plugin owner, String host, int port, String user, String password, String database) {
  55. try {
  56. sql = new MySQL(host, port, user, password, database);
  57. executor = Executors.newCachedThreadPool();
  58. plugin = owner;
  59. } catch (Exception e) {
  60. e.printStackTrace();
  61. }
  62. }
  63.  
  64. public static boolean connected()
  65. {
  66. return con != null;
  67. }
  68.  
  69. public static void update(String qry)
  70. {
  71. try
  72. {
  73. PreparedStatement statement = con.prepareStatement(qry);
  74. statement.executeUpdate();
  75. statement.close();
  76. }
  77. catch (SQLException e)
  78. {
  79. e.printStackTrace();
  80. }
  81. }
  82. public static void Aupdate(String statement) {
  83. executor.execute(() -> sql.queryUpdate(statement));
  84. }
  85.  
  86. public static void Aquery(PreparedStatement statement, Consumer<ResultSet> consumer) {
  87. executor.execute(() -> {
  88. ResultSet result = sql.query(statement);
  89. Bukkit.getScheduler().runTask(plugin, () -> consumer.accept(result));
  90. });
  91. }
  92. public static PreparedStatement Aprepare(String query) {
  93. try {
  94. return sql.getConnection().prepareStatement(query);
  95. } catch (Exception e) {
  96. e.printStackTrace();
  97. }
  98. return null;
  99. }
  100. public static ResultSet query(String qry)
  101. {
  102. ResultSet rs = null;
  103. try
  104. {
  105. Statement st = con.createStatement();
  106. rs = st.executeQuery(qry);
  107. }
  108. catch (SQLException e)
  109. {
  110. e.printStackTrace();
  111. }
  112. return rs;
  113. }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement