Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package generic.util;
- import java.io.IOException;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- import java.util.Properties;
- public class DBConnection {
- private static final String FILE_NAME = "db";
- private static final String URL = "url";
- private static final String USER = "user";
- private static final String PASSWORD = "password";
- private static Connection conn;
- private DBConnection(){
- }
- public static synchronized Connection getConnection() throws SQLException, ClassNotFoundException, IOException {
- if(DBConnection.conn == null || DBConnection.conn.isClosed()){
- Properties props = PropertiesReader.readPropertyFile(FILE_NAME);
- String url = props.getProperty(URL);
- String user = props.getProperty(USER);
- String password = props.getProperty(PASSWORD);
- Connection conn = DriverManager.getConnection(url, user, password);
- DBConnection.conn = conn;
- }
- return DBConnection.conn;
- }
- }
- <project xmlns="http://maven.apache.org/POM/4.0.0"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>groupid</groupId>
- <artifactId>artifactid</artifactId>
- <version>1.0</version>
- <properties>
- <skipTests>true</skipTests>
- </properties>
- <build>
- <plugins>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-compiler-plugin</artifactId>
- <version>3.7.0</version>
- <configuration>
- <source>1.8</source>
- <target>1.8</target>
- </configuration>
- </plugin>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-surefire-plugin</artifactId>
- <version>2.12.4</version>
- <configuration>
- <skipTests>${skipTests}</skipTests>
- </configuration>
- </plugin>
- </plugins>
- </build>
- <dependencies>
- <!-- https://mvnrepository.com/artifact/org.postgresql/postgresql -->
- <dependency>
- <groupId>org.postgresql</groupId>
- <artifactId>postgresql</artifactId>
- <version>42.2.2</version>
- </dependency>
- </dependencies>
- </project>
Add Comment
Please, Sign In to add comment