Created
August 23, 2021 13:11
-
-
Save JuliaKrivonos/858bc9e2b85da24de6daae3a2616bd3c to your computer and use it in GitHub Desktop.
connection util
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package org.jazzteam.krivonos.fitness.jdbc.util; | |
| import lombok.extern.slf4j.Slf4j; | |
| import java.sql.Connection; | |
| import java.sql.DriverManager; | |
| import java.sql.SQLException; | |
| @Slf4j | |
| public class ConnectionUtil { | |
| private static final String URL = "datasource.url"; | |
| private static final String USER_NAME = "datasource.username"; | |
| private static final String PASSWORD = "datasource.password"; | |
| private static final String DRIVER = "datasource.driver"; | |
| public static Connection getConnection() { | |
| try { | |
| Class.forName(PropertyUtil.getProperty(DRIVER)); | |
| return DriverManager.getConnection( | |
| PropertyUtil.getProperty(URL), | |
| PropertyUtil.getProperty(USER_NAME), | |
| PropertyUtil.getProperty(PASSWORD)); | |
| } catch (ClassNotFoundException e) { | |
| log.error(e.getMessage()); | |
| throw new IllegalStateException("Cannot find class PropertyUtil.getProperty(DRIVER)"); | |
| } catch (SQLException throwables) { | |
| log.error(throwables.getMessage()); | |
| throw new IllegalStateException("SQLException in trying get connection"); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment