Advertisement
Guest User

asdf

a guest
May 23rd, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.30 KB | None | 0 0
  1. public class DBconnector {
  2.  
  3.     private String host = System.getenv("OPENSHIFT_MYSQL_DB_HOST");
  4.     private String port = System.getenv("OPENSHIFT_MYSQL_DB_PORT");
  5.     private String username = System.getenv("OPENSHIFT_MYSQL_DB_USERNAME");
  6.     private String password = System.getenv("OPENSHIFT_MYSQL_DB_PASSWORD");
  7.     private String url = String.format("jdbc:mysql://%s:%s",host,port);
  8.  
  9.     private  Connection connection;
  10.     private static DBconnector dBconnector;
  11.  
  12.     private DBconnector() {
  13.  
  14.     Properties properties = new Properties();
  15.  
  16.      properties.setProperty("useUnicode", "true");
  17.      properties.setProperty("characterEncoding", "utf8");
  18.      properties.setProperty("user", username);
  19.      properties.setProperty("password", password);
  20.  
  21.       try {
  22.            Class.forName("com.mysql.jdbc.Driver");
  23.            this.connection = DriverManager.getConnection(url , properties);
  24.  
  25.             } catch (SQLException  | ClassNotFoundException d) {
  26.                 d.printStackTrace();
  27.             }
  28.  
  29.  
  30.         }
  31.  
  32.         public static DBconnector getInstance() {
  33.             if (dBconnector == null)
  34.                 dBconnector = new DBconnector();
  35.             return dBconnector;
  36.         }
  37.  
  38.         public Connection getConnection() {
  39.             return connection;
  40.         }
  41.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement