Advertisement
Guest User

Untitled

a guest
Jul 7th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. package sleiferhd.core.config;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7.  
  8. public class MysqlConnection {
  9.  
  10. private final String host;
  11. private final int port;
  12. private final String database;
  13. private final String username;
  14. private final String password;
  15.  
  16. private Connection conn;
  17. public final Mysql inf;
  18.  
  19. public MysqlConnection(String host, int port, String database, String username, String password)
  20. {
  21. this.host = host;
  22. this.port = port;
  23. this.database = database;
  24. this.username = username;
  25. this.password = password;
  26. this.inf = new Mysql(this);
  27. reconnect();
  28. }
  29.  
  30. public void reconnect()
  31. {
  32. try {
  33. Class.forName("com.mysql.jdbc.Driver");
  34. conn = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database + "?autoReconnect=true", username, password);
  35. } catch(Exception err) {err.printStackTrace();}
  36. }
  37.  
  38. public boolean isConnected()
  39. {
  40. try {
  41. return conn == null ? false : !conn.isClosed();
  42. } catch(Exception err) {err.printStackTrace();}
  43. return false;
  44. }
  45.  
  46. public void executeUpdate(String update)
  47. {
  48. if(!isConnected())
  49. reconnect();
  50. try {
  51. conn.createStatement().executeUpdate(update);
  52. } catch(Exception err) {err.printStackTrace();}
  53. }
  54.  
  55. public ResultSet executeQuery(String query)
  56. {
  57. if(!isConnected())
  58. reconnect();
  59. try {
  60. return conn.createStatement().executeQuery(query);
  61. } catch(Exception err) {err.printStackTrace();}
  62. return null;
  63. }
  64.  
  65. public void close()
  66. {
  67. if(isConnected())
  68. try {
  69. conn.close();
  70. conn = null;
  71. } catch(SQLException err) {err.printStackTrace();}
  72. }
  73.  
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement