Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package us.shoc.mlsdb;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class DBConnection {
- private Connection conn;
- private URL url;
- private Credentials creds;
- static public class Credentials {
- private String user;
- private String pass;
- public Credentials(String user, String pass) {
- this.user = user;
- this.pass = pass;
- }
- public Credentials(Credentials creds) {
- this(creds.getUser(), creds.getPass());
- }
- public String getUser() {
- return this.user;
- }
- public void setUser(String user) {
- this.user = user;
- }
- public String getPass() {
- return this.pass;
- }
- public void setPass() {
- this.pass = pass;
- }
- }
- static public class URL {
- private String protocol;
- private String host;
- private int port;
- private String db;
- public URL(String protocol, String host, int port, String db) {
- this.protocol = protocol;
- this.host = host;
- this.port = port;
- this.db = db;
- }
- public URL(String url) {
- final String urlRegex = "^(.*:*)://([A-Za-z0-9\\-\\.]+):([0-9]+)?/(.*)$";
- Matcher urlMatcher = Pattern.compile(urlRegex).matcher(url);
- urlMatcher.matches();
- try {
- this.protocol = (urlMatcher.group(1) != null) ? urlMatcher.group(1) : "";
- this.host = (urlMatcher.group(2) != null) ? urlMatcher.group(2) : "";
- this.port = (urlMatcher.group(3) != null) ? Integer.parseInt(urlMatcher.group(3)) : 0;
- this.db = (urlMatcher.group(4) != null) ? urlMatcher.group(4) : "";
- }
- catch(Exception e) {
- throw new IllegalArgumentException("Invalid database connection URL provided");
- }
- }
- public URL(URL url) {
- this(url.getProtocol(), url.getHost(), url.getPort(), url.getDb());
- }
- public String getProtocol() {
- return this.protocol;
- }
- public void setProtocol(String protocol) {
- this.protocol = protocol;
- }
- public String getHost() {
- return this.host;
- }
- public void setHost(String host) {
- this.host = host;
- }
- public int getPort() {
- return this.port;
- }
- public void setPort(int port) {
- this.port = port;
- }
- public String getDb() {
- return this.db;
- }
- public void setDb(String db) {
- this.db = db;
- }
- @Override
- public String toString() {
- return String.format("%s://%s:%d/%s", this.protocol, this.host, this.port, this.db);
- }
- }
- public DBConnection(URL url, Credentials creds) {
- this.url = new URL(url);
- this.creds = new Credentials(creds);
- }
- public DBConnection(DBConnection conn) {
- this(conn.getUrl(), conn.getCreds());
- }
- public URL getUrl() {
- return this.url;
- }
- public Credentials getCreds() {
- return this.creds;
- }
- public boolean updateDB(String update_statement) {
- boolean update_status = false;
- Statement statement;
- try {
- statement = this.conn.createStatement();
- statement.executeUpdate(update_statement);
- update_status = true;
- }
- catch(SQLException e) {
- System.out.println(e.getMessage());
- }
- return update_status;
- }
- public boolean connect() {
- boolean conn_status = false;
- try {
- this.conn = DriverManager.getConnection(this.url.toString(), this.creds.getUser(), this.creds.getPass());
- conn_status = true;
- }
- catch(SQLException e) {
- System.out.println(e.getMessage());
- this.conn = null;
- }
- return conn_status;
- }
- public boolean disconnect() {
- boolean dconn_status = false;
- try {
- this.conn.close();
- dconn_status = true;
- }
- catch(SQLException e) {
- System.out.println(e.getMessage());
- }
- return dconn_status;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement