Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public abstract class Database {
- public abstract Connection getConnection();
- public abstract void connect();
- public abstract void disconnect();
- public abstract boolean isConnected();
- }
- public class MySQL extends Database {
- private final String ip;
- private final int port;
- private final String database;
- private final String username;
- private final String password;
- public MySQL(String ip, int port, String database, String username, String password) {
- this.ip = ip;
- this.port = port;
- this.database = database;
- this.username = username;
- this.password = password;
- }
- private Connection connection;
- @Override
- public Connection getConnection() {
- return connection;
- }
- @Override
- public void connect() {
- if (isConnected()) {
- return;
- }
- try {
- connection = DriverManager.getConnection("jdbc:mysql://" + ip + ":" + port + "/" + database, username, password);
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- @Override
- public void disconnect() {
- if (isConnected()) {
- try {
- connection.close();
- connection = null;
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- @Override
- public boolean isConnected() {
- if(connection == null) {
- return false;
- }
- try {
- return connection.isClosed();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement