Advertisement
Guest User

Untitled

a guest
Jun 12th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 1.18 KB | None | 0 0
  1. package com.subin.common;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.SQLException;
  6.  
  7. public class ConnectionManager
  8. {
  9.  
  10. private String userId = "username";
  11. private String passWord = "password";
  12. private String url = "complete_url"; // e.g         jdbc:oracle:thin:@host:port:SID
  13. private String driver = "oracle.jdbc.driver.OracleDriver";
  14.  
  15. private static ConnectionManager instance;
  16.  
  17. private String getUrl()
  18. {
  19. return url;
  20. }
  21.  
  22. private String getUserId()
  23. {
  24. return userId;
  25. }
  26.  
  27. private String getPassWord()
  28. {
  29. return passWord;
  30. }
  31.  
  32. public static ConnectionManager getInstance()
  33. {
  34. if (instance == null)
  35. {
  36. instance = new ConnectionManager();
  37. }
  38. return instance;
  39. }
  40.  
  41. private ConnectionManager()
  42. {
  43. try
  44. {
  45. Class.forName(driver);
  46. }
  47. catch (Exception e)
  48. {
  49. e.printStackTrace();
  50. }
  51. }
  52.  
  53. public static Connection getConnection() throws java.sql.SQLException
  54. {
  55. return DriverManager.getConnection(getInstance().getUrl(),
  56. getInstance().getUserId(), getInstance().getPassWord());
  57. }
  58.  
  59. public static void closeConnection(Connection c)
  60. {
  61. try
  62. {
  63. if (c != null)
  64. {
  65. c.close();
  66. }
  67. }
  68. catch (SQLException e)
  69. {
  70. e.printStackTrace();
  71. }
  72. }
  73.  
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement