Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private Connection connection;
- private String url_base = "jdbc:mysql://", host, db, username, password;
- private Integer port;
- private boolean debug;
- public MySQL(String host, Integer port, String db, String username, String password, boolean debug) {
- this.host = host;
- this.port = port;
- this.db = db;
- this.username = username;
- this.password = password;
- this.debug = debug;
- }
- public Connection getConnection() {
- return connection;
- }
- public boolean isConnected() {
- try {
- if ((connection == null) || (connection.isClosed())) return false;
- else return true;
- } catch (SQLException e) {
- if(debug == true)
- e.printStackTrace();
- }
- return false;
- }
- public void connect() {
- if (!isConnected()) {
- try {
- Class.forName("com.mysql.jdbc.Driver");
- connection = DriverManager.getConnection(url_base + host + ":" + port + "/" + db, username, password);
- if (debug == true) System.out.printf("\n[DB] Connection etablished --> " + url_base + host + ":" + port + "/" + db, username, password);
- } catch (ClassNotFoundException e) {
- if (debug == true) e.printStackTrace();
- } catch (SQLException e) {
- if (debug == true) {
- System.err.printf("\n[DB] Failled to connect --> " + url_base + host + ":" + port + "/" + db, username, password);
- e.printStackTrace();
- }
- }
- }
- }
- public void disconnect() {
- if (isConnected()) {
- try {
- connection.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement