Advertisement
Guest User

Untitled

a guest
Apr 25th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.63 KB | None | 0 0
  1. import java.io.FileInputStream;
  2. import java.io.FileNotFoundException;
  3. import java.io.IOException;
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.SQLException;
  7. import java.util.Properties;
  8.  
  9. /**
  10. * Singleton to manage DB Connection.
  11. *
  12. * One never does a new operation on a singleton object. Getting access to the
  13. * object is via
  14. *
  15. * DbConnection dbc = DbConnection.accessDbConnection();
  16. *
  17. * if the database properties are in a file database.properties in the current
  18. * directory, or
  19. *
  20. * String myDbPropertiesFile = "..."; // filename of jdbc connection properties
  21. * DbConnection dbc = DbConnection.accessDbConnection(myDbPropertiesFile);
  22. *
  23. * If a DbConnection object exists, a reference to it is returned.
  24. *
  25. * If no DbConnection object exists, a new one is created and its reference is
  26. * returned.
  27. *
  28. * Connection conn = dbc.getConnection();
  29. * returns a jdbc Connection object
  30. * that is already connected to the database.
  31. *
  32. * Note that this implementation makes a lot of assumptions about
  33. * "only one client at a time." But it does keep one from having to use a shared
  34. * static Connection field, or passing a Connection object around via parameter.
  35. *
  36. * @author jparks
  37. *
  38. */
  39. public class DbConnection {
  40.  
  41. private static final String DB_PROPERTIES_FILE = "database.properties";
  42.  
  43. private static DbConnection me = null;
  44.  
  45. private Connection dbConn = null;
  46.  
  47. // Note the constructor is private. Code outside this class can't create a
  48. // DbConnection
  49. // object even if it wanted to.
  50. private DbConnection(String dbPropertiesFile) throws FileNotFoundException, IOException, SQLException {
  51.  
  52. Properties p = new Properties();
  53. p.load(new FileInputStream(dbPropertiesFile));
  54.  
  55. String username = "";
  56. String password = "";
  57. String url = "";
  58. String driver = "";
  59. url = p.getProperty("jdbc.url");
  60. driver = p.getProperty("jdbc.driver");
  61. // Class.forName(driver); // not needed on my system
  62.  
  63. dbConn = DriverManager.getConnection(url, username, password);
  64. }
  65.  
  66. /**
  67. * Get access to a jdbc Connection to a database. The Connection is already established.
  68. *
  69. * @return - already established jdbc connection
  70. */
  71. public Connection getConnection() {
  72. return dbConn;
  73. }
  74.  
  75. /**
  76. * Gain access to a DbConnection object that can supply a jdbc Connection to
  77. * a database. The database connection parameters are in a file
  78. * database.properties in the current directory.
  79. *
  80. * @return DbConnection object
  81. * @throws FileNotFoundException
  82. * @throws IOException
  83. * @throws SQLException
  84. */
  85. public static DbConnection accessDbConnection() throws FileNotFoundException, IOException, SQLException {
  86. return accessDbConnection(DB_PROPERTIES_FILE);
  87. }
  88.  
  89. /**
  90. * Gain access to a DbConnection object that can supply a jdbc Connection to
  91. * a database.
  92. *
  93. *
  94. * @param propertiesFilename - the name of a file containing the jdbc properties to make
  95. * the database connection.
  96. *
  97. * @return DbConnection object
  98. *
  99. * @throws FileNotFoundException
  100. * @throws IOException
  101. * @throws SQLException
  102. */
  103. public static DbConnection accessDbConnection(String propertiesFilename)
  104. throws FileNotFoundException, IOException, SQLException {
  105. if (me == null) {
  106. me = new DbConnection(propertiesFilename);
  107. }
  108. return me;
  109. }
  110.  
  111. /**
  112. * Disconnect from the database and return the DbConnection object to an
  113. * uninitialized state.
  114. * @throws SQLException
  115. */
  116. public void disconnect() throws SQLException {
  117. me = null;
  118. if (dbConn != null) {
  119. try {
  120. dbConn.close();
  121. } finally {
  122. dbConn = null;
  123. }
  124. }
  125. }
  126.  
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement