Advertisement
Guest User

Untitled

a guest
Oct 8th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. package ua.denicon.obelisks.mysql;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.SQLException;
  6.  
  7. /**
  8. * Connects to and uses a MySQL database
  9. *
  10. * @author -_Husky_-
  11. * @author tips48
  12. */
  13. public class MySQL extends Database {
  14. private final String user;
  15. private final String database;
  16. private final String password;
  17. private final String port;
  18. private final String hostname;
  19.  
  20. /**
  21. * Creates a new MySQL instance
  22. *
  23. * @param hostname
  24. * Name of the host
  25. * @param port
  26. * Port number
  27. * @param username
  28. * Username
  29. * @param password
  30. * Password
  31. */
  32. public MySQL(String hostname, String port, String username,
  33. String password) {
  34. this(hostname, port, null, username, password);
  35. }
  36.  
  37. /**
  38. * Creates a new MySQL instance for a specific database
  39. *
  40. * @param hostname
  41. * Name of the host
  42. * @param port
  43. * Port number
  44. * @param database
  45. * Database name
  46. * @param username
  47. * Username
  48. * @param password
  49. * Password
  50. */
  51. public MySQL(String hostname, String port, String database,
  52. String username, String password) {
  53. this.hostname = hostname;
  54. this.port = port;
  55. this.database = database;
  56. this.user = username;
  57. this.password = password;
  58. }
  59.  
  60. @Override
  61. public Connection openConnection() throws SQLException,
  62. ClassNotFoundException {
  63. if (checkConnection()) {
  64. return connection;
  65. }
  66.  
  67. String connectionURL = "jdbc:mysql://"
  68. + this.hostname + ":" + this.port;
  69. if (database != null) {
  70. connectionURL = connectionURL + "/" + this.database;
  71. }
  72.  
  73. Class.forName("com.mysql.jdbc.Driver");
  74. connection = DriverManager.getConnection(connectionURL,
  75. this.user, this.password);
  76. return connection;
  77. }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement