Advertisement
Guest User

java

a guest
May 20th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.51 KB | None | 0 0
  1.  
  2.     private Connection connection;
  3.     private String url_base = "jdbc:mysql://", host, db, username, password;
  4.     private Integer port;
  5.     private boolean debug;
  6.  
  7.     public MySQL(String host, Integer port, String db, String username, String password, boolean debug) {
  8.         this.host = host;
  9.         this.port = port;
  10.         this.db = db;
  11.         this.username = username;
  12.         this.password = password;
  13.         this.debug = debug;
  14.     }
  15.  
  16.     public Connection getConnection() {
  17.         return connection;
  18.     }
  19.  
  20.     public boolean isConnected() {
  21.         try {
  22.             if ((connection == null) || (connection.isClosed())) return false;
  23.             else return true;
  24.         } catch (SQLException e) {
  25.             if(debug == true)
  26.             e.printStackTrace();
  27.         }
  28.         return false;
  29.     }
  30.  
  31.     public void connect() {
  32.         if (!isConnected()) {
  33.             try {
  34.                 Class.forName("com.mysql.jdbc.Driver");
  35.                 connection = DriverManager.getConnection(url_base + host + ":" + port + "/" + db, username, password);
  36.                 if (debug == true) System.out.printf("\n[DB] Connection etablished --> " + url_base + host + ":" + port + "/" + db, username, password);
  37.             } catch (ClassNotFoundException e) {
  38.                 if (debug == true) e.printStackTrace();
  39.             } catch (SQLException e) {
  40.                 if (debug == true) {
  41.                     System.err.printf("\n[DB] Failled to connect --> " + url_base + host + ":" + port + "/" + db, username, password);
  42.                     e.printStackTrace();
  43.                 }
  44.             }
  45.         }
  46.     }
  47.  
  48.     public void disconnect() {
  49.         if (isConnected()) {
  50.             try {
  51.                 connection.close();
  52.             } catch (SQLException e) {
  53.                 e.printStackTrace();
  54.             }
  55.         }
  56.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement