Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package pl.ttpsc.projmgmt.service;
- import org.apache.logging.log4j.LogManager;
- import org.apache.logging.log4j.Logger;
- import java.sql.*;
- public class PostgreSqlService implements SqlService {
- private static final Logger LOGGER = LogManager.getLogger("Application");
- private final String url = "jdbc:postgresql://localhost:5432/projectManagmentAppDB";
- private final String user = "postgres";
- private final String password = "postgres";
- public Statement statement = null;
- public Connection connection = null;
- @Override
- public void connect() {
- try {
- Class.forName("org.postgresql.Driver");
- connection = DriverManager.getConnection(url, user, password);
- } catch (SQLException e) {
- LOGGER.error("Connection to PostgreSQL database failed!");
- }catch (ClassNotFoundException e) {
- LOGGER.error("Cannot find PostgreSQL driver!");
- }
- }
- @Override
- public ResultSet executeQuery(String sqlStatement){
- ResultSet resultSet = null;
- try {
- statement = connection.createStatement();
- resultSet = statement.executeQuery(sqlStatement);
- } catch (SQLException e) {
- LOGGER.error("Error while executing SQL statement!");
- }
- return resultSet;
- }
- @Override
- public ResultSet executeInsert(String sqlStatement){
- ResultSet resultSet = null;
- try {
- statement = connection.createStatement();
- statement.executeUpdate(sqlStatement, Statement.RETURN_GENERATED_KEYS);
- resultSet = statement.getGeneratedKeys();
- } catch (SQLException e) {
- LOGGER.error("Error while executing SQL statement!");
- }
- return resultSet;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement