Advertisement
Guest User

Untitled

a guest
Mar 26th, 2016
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. package com.cadiducho.tutorialbukkit;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.SQLException;
  6.  
  7. /**
  8. * Objeto para conexiones de MySQL
  9. * @author Huskehhh base original, Cadiducho actualización
  10. */
  11. public class MySQL {
  12.  
  13. protected Connection connection;
  14.  
  15. private final String user, database, password, port, hostname;
  16.  
  17. public MySQL(String hostname, String port, String database, String username, String password) {
  18. this.hostname = hostname;
  19. this.port = port;
  20. this.database = database;
  21. this.user = username;
  22. this.password = password;
  23. }
  24.  
  25. public boolean checkConnection() throws SQLException {
  26. return connection != null && !connection.isClosed();
  27. }
  28.  
  29. public Connection getConnection() {
  30. return connection;
  31. }
  32.  
  33. public boolean closeConnection() throws SQLException {
  34. if (connection == null) {
  35. return false;
  36. }
  37. connection.close();
  38. return true;
  39. }
  40.  
  41. public Connection openConnection() throws SQLException, ClassNotFoundException {
  42. if (checkConnection()) {
  43. return connection;
  44. }
  45.  
  46. Class.forName("com.mysql.jdbc.Driver");
  47. connection = DriverManager.getConnection("jdbc:mysql://"
  48. + this.hostname + ":" + this.port + "/" + this.database, this.user, this.password);
  49. return connection;
  50. }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement