Advertisement
eschersenigma

DBConnection Class

Jul 21st, 2018
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.75 KB | None | 0 0
  1. package us.shoc.mlsdb;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.SQLException;
  6. import java.sql.Statement;
  7. import java.util.regex.Matcher;
  8. import java.util.regex.Pattern;
  9.  
  10. public class DBConnection {
  11.    
  12.     private Connection conn;
  13.     private URL url;
  14.     private Credentials creds;
  15.  
  16.     static public class Credentials {
  17.  
  18.         private String user;
  19.         private String pass;
  20.  
  21.         public Credentials(String user, String pass) {
  22.             this.user = user;
  23.             this.pass = pass;
  24.         }
  25.  
  26.         public Credentials(Credentials creds) {
  27.             this(creds.getUser(), creds.getPass());
  28.         }
  29.  
  30.         public String getUser() {
  31.             return this.user;
  32.         }
  33.  
  34.         public void setUser(String user) {
  35.             this.user = user;
  36.         }
  37.  
  38.         public String getPass() {
  39.             return this.pass;
  40.         }
  41.  
  42.         public void setPass() {
  43.             this.pass = pass;
  44.         }
  45.     }
  46.  
  47.     static public class URL {
  48.  
  49.         private String protocol;
  50.         private String host;
  51.         private int port;
  52.         private String db;
  53.  
  54.         public URL(String protocol, String host, int port, String db) {
  55.             this.protocol = protocol;
  56.             this.host = host;
  57.             this.port = port;
  58.             this.db = db;
  59.         }
  60.  
  61.         public URL(String url) {
  62.             final String urlRegex = "^(.*:*)://([A-Za-z0-9\\-\\.]+):([0-9]+)?/(.*)$";
  63.            
  64.             Matcher urlMatcher = Pattern.compile(urlRegex).matcher(url);
  65.             urlMatcher.matches();
  66.  
  67.             try {
  68.                 this.protocol = (urlMatcher.group(1) != null) ? urlMatcher.group(1) : "";
  69.                 this.host = (urlMatcher.group(2) != null) ? urlMatcher.group(2) : "";
  70.                 this.port = (urlMatcher.group(3) != null) ? Integer.parseInt(urlMatcher.group(3)) : 0;
  71.                 this.db = (urlMatcher.group(4) != null) ? urlMatcher.group(4) : "";
  72.             }
  73.             catch(Exception e) {
  74.                 throw new IllegalArgumentException("Invalid database connection URL provided");
  75.             }
  76.         }
  77.  
  78.         public URL(URL url) {
  79.             this(url.getProtocol(), url.getHost(), url.getPort(), url.getDb());
  80.         }
  81.  
  82.         public String getProtocol() {
  83.             return this.protocol;
  84.         }
  85.  
  86.         public void setProtocol(String protocol) {
  87.             this.protocol = protocol;
  88.         }
  89.  
  90.         public String getHost() {
  91.             return this.host;
  92.         }
  93.  
  94.         public void setHost(String host) {
  95.             this.host = host;
  96.         }
  97.  
  98.         public int getPort() {
  99.             return this.port;
  100.         }
  101.  
  102.         public void setPort(int port) {
  103.             this.port = port;
  104.         }
  105.  
  106.         public String getDb() {
  107.             return this.db;
  108.         }
  109.  
  110.         public void setDb(String db) {
  111.             this.db = db;
  112.         }
  113.  
  114.         @Override
  115.         public String toString() {
  116.             return String.format("%s://%s:%d/%s", this.protocol, this.host, this.port, this.db);
  117.         }
  118.     }
  119.  
  120.     public DBConnection(URL url, Credentials creds) {
  121.         this.url = new URL(url);
  122.         this.creds = new Credentials(creds);
  123.     }
  124.  
  125.     public DBConnection(DBConnection conn) {
  126.         this(conn.getUrl(), conn.getCreds());
  127.     }
  128.  
  129.     public URL getUrl() {
  130.         return this.url;
  131.     }
  132.  
  133.     public Credentials getCreds() {
  134.         return this.creds;
  135.     }
  136.  
  137.     public boolean updateDB(String update_statement) {
  138.         boolean update_status = false;
  139.         Statement statement;
  140.  
  141.         try {
  142.             statement = this.conn.createStatement();
  143.             statement.executeUpdate(update_statement);
  144.             update_status = true;
  145.         }
  146.         catch(SQLException e) {
  147.             System.out.println(e.getMessage());
  148.         }
  149.  
  150.         return update_status;
  151.     }
  152.  
  153.     public boolean connect() {
  154.             boolean conn_status = false;
  155.        
  156.         try {
  157.             this.conn = DriverManager.getConnection(this.url.toString(), this.creds.getUser(), this.creds.getPass());
  158.             conn_status = true;
  159.             }
  160.         catch(SQLException e) {
  161.                     System.out.println(e.getMessage());
  162.             this.conn = null;
  163.             }
  164.  
  165.         return conn_status;
  166.     }
  167.  
  168.     public boolean disconnect() {
  169.         boolean dconn_status = false;
  170.  
  171.         try {
  172.             this.conn.close();
  173.             dconn_status = true;
  174.         }
  175.         catch(SQLException e) {
  176.             System.out.println(e.getMessage());
  177.         }
  178.  
  179.         return dconn_status;
  180.     }
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement