Guest User

Untitled

a guest
May 4th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 KB | None | 0 0
  1. package generic.util;
  2.  
  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. public class DBConnection {
  10.  
  11. private static final String FILE_NAME = "db";
  12. private static final String URL = "url";
  13. private static final String USER = "user";
  14. private static final String PASSWORD = "password";
  15.  
  16. private static Connection conn;
  17.  
  18. private DBConnection(){
  19.  
  20. }
  21.  
  22. public static synchronized Connection getConnection() throws SQLException, ClassNotFoundException, IOException {
  23.  
  24. if(DBConnection.conn == null || DBConnection.conn.isClosed()){
  25. Properties props = PropertiesReader.readPropertyFile(FILE_NAME);
  26.  
  27. String url = props.getProperty(URL);
  28. String user = props.getProperty(USER);
  29. String password = props.getProperty(PASSWORD);
  30.  
  31. Connection conn = DriverManager.getConnection(url, user, password);
  32.  
  33. DBConnection.conn = conn;
  34. }
  35.  
  36. return DBConnection.conn;
  37. }
  38.  
  39. }
  40.  
  41. <project xmlns="http://maven.apache.org/POM/4.0.0"
  42. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  43. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  44. <modelVersion>4.0.0</modelVersion>
  45. <groupId>groupid</groupId>
  46. <artifactId>artifactid</artifactId>
  47. <version>1.0</version>
  48. <properties>
  49. <skipTests>true</skipTests>
  50. </properties>
  51. <build>
  52. <plugins>
  53. <plugin>
  54. <groupId>org.apache.maven.plugins</groupId>
  55. <artifactId>maven-compiler-plugin</artifactId>
  56. <version>3.7.0</version>
  57. <configuration>
  58. <source>1.8</source>
  59. <target>1.8</target>
  60. </configuration>
  61. </plugin>
  62. <plugin>
  63. <groupId>org.apache.maven.plugins</groupId>
  64. <artifactId>maven-surefire-plugin</artifactId>
  65. <version>2.12.4</version>
  66. <configuration>
  67. <skipTests>${skipTests}</skipTests>
  68. </configuration>
  69. </plugin>
  70. </plugins>
  71. </build>
  72. <dependencies>
  73.  
  74. <!-- https://mvnrepository.com/artifact/org.postgresql/postgresql -->
  75. <dependency>
  76. <groupId>org.postgresql</groupId>
  77. <artifactId>postgresql</artifactId>
  78. <version>42.2.2</version>
  79. </dependency>
  80.  
  81. </dependencies>
  82. </project>
Add Comment
Please, Sign In to add comment