OIM DB Connection/ Data Source connection

  1. package com.oimacademy.connection;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.SQLException;
  5. import com.oimacademy.properties.OIMProfileReader;
  6. public class DataSource {
  7. private static Connection connectionObject = null;
  8. private static OIMProfileReader properties=null;
  9. static String JDBC_DRIVER = null;
  10. static String DB_URL = null;
  11. static String USER = null;
  12. static String PASS = null;
  13. private static Connection makeConnection() {
  14. try {
  15. properties = new OIMProfileReader();
  16. JDBC_DRIVER=properties.getString("operationsDB.directDBdriver");
  17. DB_URL=properties.getString("operationsDB.jdbcUrl");
  18. USER=properties.getString("operationsDB.user");
  19. PASS=properties.getString("operationsDB.password");
  20. Class.forName(JDBC_DRIVER);
  21. System.out.println("Connecting to database...");
  22. System.out.println("DB User Name -> "+DB_URL);
  23. System.out.println("DB URL -> "+USER);
  24. connectionObject = DriverManager.getConnection(DB_URL, USER, PASS);
  25. } catch (SQLException se) {
  26. se.printStackTrace();
  27. } catch (Exception e) {
  28. e.printStackTrace();
  29. }
  30. return connectionObject;
  31. }
  32. public static synchronized Connection getConnection() {
  33. if (connectionObject == null) {
  34. connectionObject = makeConnection();
  35. }
  36. return connectionObject;
  37. }
  38. public static void closeConnection() {
  39. System.out.println("Closing to database connection ...");
  40. try {
  41. properties=null;
  42. JDBC_DRIVER=null;
  43. DB_URL=null;
  44. USER=null;
  45. PASS=null;
  46. if (connectionObject != null)
  47. connectionObject.close();
  48. } catch (SQLException se) {
  49. se.printStackTrace();
  50. }
  51. }
  52. public static void main(String args[]){
  53. getConnection();
  54. closeConnection();
  55. }
  56. }

No comments:

Post a Comment