Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.example.connection;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- import java.util.Properties;
- public class ConnectionMariaDB {
- private static Connection instance;
- private ConnectionMariaDB() {
- }
- public static Connection getConnection() {
- if (instance == null) {
- instance = getInstance();
- }
- return instance;
- }
- private static Connection getInstance() {
- Properties props = new Properties();
- FileInputStream fis;
- try {
- fis = new FileInputStream("src/main/resources/db.properties");
- props.load(fis);
- } catch (IOException e) {
- e.printStackTrace();
- }
- String drivers = props.getProperty("db.driver");
- if (drivers != null) {
- System.setProperty("db.driver", drivers);
- }
- String url = props.getProperty("db.url");
- String username = props.getProperty("db.user");
- String password = props.getProperty("db.password");
- Connection connection = null;
- try {
- connection = DriverManager.getConnection(url, username, password);
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return connection;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement