Advertisement
Guest User

Untitled

a guest
Jan 17th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. package pl.ttpsc.projmgmt.service;
  2.  
  3. import org.apache.logging.log4j.LogManager;
  4. import org.apache.logging.log4j.Logger;
  5.  
  6. import java.sql.*;
  7.  
  8. public class PostgreSqlService implements SqlService {
  9.  
  10. private static final Logger LOGGER = LogManager.getLogger("Application");
  11.  
  12. private final String url = "jdbc:postgresql://localhost:5432/projectManagmentAppDB";
  13. private final String user = "postgres";
  14. private final String password = "postgres";
  15. public Statement statement = null;
  16. public Connection connection = null;
  17.  
  18. @Override
  19. public void connect() {
  20. try {
  21. Class.forName("org.postgresql.Driver");
  22. connection = DriverManager.getConnection(url, user, password);
  23. } catch (SQLException e) {
  24. LOGGER.error("Connection to PostgreSQL database failed!");
  25. }catch (ClassNotFoundException e) {
  26. LOGGER.error("Cannot find PostgreSQL driver!");
  27. }
  28. }
  29.  
  30. @Override
  31. public ResultSet executeQuery(String sqlStatement){
  32. ResultSet resultSet = null;
  33. try {
  34. statement = connection.createStatement();
  35. resultSet = statement.executeQuery(sqlStatement);
  36. } catch (SQLException e) {
  37. LOGGER.error("Error while executing SQL statement!");
  38. }
  39. return resultSet;
  40. }
  41.  
  42. @Override
  43. public ResultSet executeInsert(String sqlStatement){
  44. ResultSet resultSet = null;
  45. try {
  46. statement = connection.createStatement();
  47. statement.executeUpdate(sqlStatement, Statement.RETURN_GENERATED_KEYS);
  48. resultSet = statement.getGeneratedKeys();
  49. } catch (SQLException e) {
  50. LOGGER.error("Error while executing SQL statement!");
  51. }
  52. return resultSet;
  53. }
  54.  
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement