Advertisement
Guest User

Untitled

a guest
Mar 30th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1. public class MySQLConnector {
  2.  
  3. private static String user;
  4. private static String password;
  5. private static String host;
  6. private static String database;
  7. private static Integer port;
  8. private static Connection connection;
  9.  
  10. @SuppressWarnings("static-access")
  11. public MySQLConnector(String user, String password, String host, String database, Integer port)
  12. {
  13. this.user = user;
  14. this.password = password;
  15. this.host = host;
  16. this.database = database;
  17. this.port = port;
  18. }
  19.  
  20. public static void connect()
  21. {
  22. if (!isConnected()) {
  23. try
  24. {
  25. connection = DriverManager.getConnection("jdbc:mysql://" +
  26. host + ":" + port + "/" + database +
  27. "?autoReconnect=true", user, password);
  28. Bukkit.getConsoleSender().sendMessage(Main.Prefix + "§aDie Verbindung zu der MySQL Datenbank wurde erfolgreich erstellt");
  29. }
  30. catch (SQLException e)
  31. {
  32. Bukkit.getConsoleSender().sendMessage(Main.Prefix + "§cDie Verbindung zu der MySQL Datenbank konnte nicht hergerstellt werden");
  33. }
  34. }
  35. }
  36.  
  37. @SuppressWarnings("static-access")
  38. public void close()
  39. {
  40. if (isConnected()) {
  41. try
  42. {
  43. this.connection.close();
  44. }
  45. catch (SQLException localSQLException) {}
  46. }
  47. }
  48.  
  49. public static boolean isConnected()
  50. {
  51. return connection != null;
  52. }
  53.  
  54. public static void update(String qry)
  55. {
  56. try
  57. {
  58. PreparedStatement preparedStatement = connection
  59. .prepareStatement(qry);
  60. preparedStatement.executeUpdate();
  61. }
  62. catch (SQLException e)
  63. {
  64. connect();
  65. System.err.println(e);
  66. }
  67. }
  68.  
  69. public static ResultSet query(String qry)
  70. {
  71. ResultSet rs = null;
  72. try
  73. {
  74. PreparedStatement preparedStatement = connection
  75. .prepareStatement(qry);
  76. rs = preparedStatement.executeQuery();
  77. }
  78. catch (SQLException e)
  79. {
  80. e.printStackTrace();
  81. }
  82. return rs;
  83. }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement