Advertisement
Guest User

Untitled

a guest
Feb 15th, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. package com.lobbysystem.mysql;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8.  
  9. import com.lobbysystem.LobbySystem;
  10.  
  11. public class MySQLConnection {
  12.  
  13. /* Created by: Suders
  14. * Date: 15.12.2017
  15. * Time: 18:41:11
  16. * Location: BungeeBanSystem
  17. */
  18.  
  19. private static final String port = LobbySystem.getMySQLConfigManager().getPort();
  20. private static final String ip = LobbySystem.getMySQLConfigManager().getHost();
  21. private static final String database = LobbySystem.getMySQLConfigManager().getDatabase();
  22. private static final String host = "jdbc:mysql://" + ip + ":" + port + "/" + database + "?autoReconnect";
  23. private static final String username = LobbySystem.getMySQLConfigManager().getUsername();
  24. private static final String password = LobbySystem.getMySQLConfigManager().getPassword();
  25. public static Connection connection;
  26.  
  27. public static void connect() {
  28. try {
  29. Class.forName("com.mysql.jdbc.Driver");
  30. connection = DriverManager.getConnection(host, username, password);
  31. } catch (Exception e) {
  32. e.printStackTrace();
  33. }
  34. }
  35.  
  36. public static boolean isConnected() {
  37. try {
  38. if(connection != null && !connection.isClosed()) {
  39. return true;
  40. }
  41. } catch (SQLException e) {
  42. e.printStackTrace();
  43. }
  44. return false;
  45. }
  46.  
  47. public static void close() {
  48. if(isConnected()) {
  49. try {
  50. connection.close();
  51. } catch (SQLException e) {}
  52. }
  53. }
  54.  
  55. public static void update(String qry) {
  56. try {
  57. Statement st = connection.createStatement();
  58. st.executeUpdate(qry);
  59. st.close();
  60. } catch (SQLException e) {
  61. e.printStackTrace();
  62. }
  63. }
  64.  
  65. public static ResultSet query(String qry) {
  66. ResultSet rs = null;
  67. try {
  68. Statement st = connection.createStatement();
  69. rs = st.executeQuery(qry);
  70. } catch (SQLException e) {
  71. e.printStackTrace();
  72. }
  73. return rs;
  74. }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement