Advertisement
broken-arrow

Untitled

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