Advertisement
broken-arrow

Untitled

Oct 17th, 2021
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 KB | None | 0 0
  1.  
  2. public class MySQL extends Database {
  3.  
  4. private Connection connection;
  5. private Connection connectin;
  6.  
  7. private boolean hasCastExeption = false;
  8. private final DatabaseSettings databaseSettings = DatabaseSettings.getInstance();
  9.  
  10. public MySQL() {
  11. super("ContainerData", "User_Settings");
  12. }
  13.  
  14.  
  15. @Override
  16. public Connection connect() {
  17.  
  18. try {
  19. if (this.connection == null || this.connection.isClosed()) {
  20. String databaseName = databaseSettings.getDatabaseName();
  21. String hostAdress = databaseSettings.getHostAdress();
  22. String port = databaseSettings.getPort();
  23. String user = databaseSettings.getDatabaseUser();
  24. String password = databaseSettings.getDatabasePassword();
  25.  
  26. Class.forName("org.mariadb.jdbc.Driver");
  27. Class.forName("com.mysql.jdbc.Driver");
  28. System.out.println(" testing database after");
  29. if (!hasCastExeption) {
  30. System.out.println("testing database");
  31. String driverConetion = "jdbc:mysql://";
  32. try {
  33. connectin = DriverManager.getConnection("jdbc:mysql://" + hostAdress + ":" + port + "/?useSSL=false", user, password);
  34. } catch (SQLRecoverableException | NoClassDefFoundError exception) {
  35. System.out.println("testing database");
  36. driverConetion = "jdbc:mariadb://";
  37. }
  38. connectin = DriverManager.getConnection(driverConetion + hostAdress + ":" + port + "/?useSSL=false", user, password);
  39. DatabaseMetaData metaData = connectin.getMetaData();
  40. //retrieving the name of the database
  41. String product_name = metaData.getDatabaseProductName();
  42. System.out.println("Name of the underlying database: " + product_name);
  43. PreparedStatement createdatabase = connectin.prepareStatement("CREATE DATABASE IF NOT EXISTS " + databaseName);
  44. createdatabase.execute();
  45. close(createdatabase);
  46. connectin.close();
  47.  
  48. connection = DriverManager.getConnection(driverConetion + hostAdress + ":" + port + "/" + databaseName + "?useSSL=false&useUnicode=yes&characterEncoding=UTF-8&autoReconnect=" + true, user, password);
  49. }
  50. }
  51. return this.connection;
  52. } catch (CommunicationsException throwables) {
  53. hasCastExeption = true;
  54. ChestStorage.getInstance().getPluginLoader().disablePlugin(ChestStorage.getInstance());
  55. ChestStorage.getInstance().getLogger().log(Level.SEVERE, "You has no connection to the database. Check your connection and the databaseSettings.yml, so you has right values.", throwables);
  56. } catch (SQLException | ClassNotFoundException throwables) {
  57. throwables.printStackTrace();
  58. //ChestStorage.getInstance().getPluginLoader().disablePlugin(ChestStorage.getInstance());
  59. }
  60. return connection;
  61. }
  62.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement