Advertisement
Guest User

Untitled

a guest
Mar 10th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 KB | None | 0 0
  1. package com.erwan.ohana.database;
  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 Name of the host
  24. * @param port Port number
  25. * @param username Username
  26. * @param password Password
  27. */
  28. public MySQL(String hostname, String port, String username,
  29. String password) {
  30. this(hostname, port, null, username, password);
  31. }
  32.  
  33. /**
  34. * Creates a new MySQL instance for a specific database
  35. *
  36. * @param hostname Name of the host
  37. * @param port Port number
  38. * @param database Database name
  39. * @param username Username
  40. * @param password Password
  41. */
  42. public MySQL(String hostname, String port, String database,
  43. String username, String password) {
  44. this.hostname = hostname;
  45. this.port = port;
  46. this.database = database;
  47. this.user = username;
  48. this.password = password;
  49. }
  50.  
  51. @Override
  52. public Connection openConnection() throws SQLException,
  53. ClassNotFoundException {
  54. if (checkConnection()) {
  55. return connection;
  56. }
  57.  
  58. String connectionURL = "jdbc:mysql://"
  59. + this.hostname + ":" + this.port;
  60. if (database != null) {
  61. connectionURL = connectionURL + "/" + this.database;
  62. }
  63.  
  64. Class.forName("com.mysql.jdbc.Driver");
  65. connection = DriverManager.getConnection(connectionURL,
  66. this.user, this.password);
  67. return connection;
  68. }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement