Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MySQL extends Database {
- Connection connection;
- private final DatabaseSettings databaseSettings = DatabaseSettings.getInstance();
- public MySQL() {
- super("ContainerData", "User_Settings");
- }
- @Override
- public Connection connect() {
- if (this.connection == null) {
- String databaseName = databaseSettings.getDatabaseName();
- String hostAdress = databaseSettings.getHostAdress();
- String port = databaseSettings.getPort();
- String user = databaseSettings.getDatabaseUser();
- String password = databaseSettings.getDatabasePassword();
- System.out.println("hostAdress" + hostAdress);
- System.out.println("password " + password);
- System.out.println("user " + user);
- try {
- Class.forName("com.mysql.jdbc.Driver");
- connection = DriverManager.getConnection("jdbc:mysql://" + hostAdress + ":" + port, user, password);// "/?user=" + user + "&password=" + password);
- PreparedStatement createdatabase = connection.prepareStatement("CREATE DATABASE IF NOT EXISTS " + databaseName);
- if (createdatabase.execute())
- close(createdatabase);
- connection = DriverManager.getConnection("jdbc:mysql://" + hostAdress + ":" + port + "/" + databaseName + "?useSSL=false&useUnicode=yes&characterEncoding=UTF-8&autoReconnect=" + true, user, password);
- } catch (SQLException | ClassNotFoundException throwables) {
- throwables.printStackTrace();
- }
- return connection;
- }
- return connection;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement