Guest User

ProfileDAOImpl.java

a guest
Oct 5th, 2017
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.85 KB | None | 0 0
  1. package com.romanidze.jizzyshop.dao.implementations;
  2.  
  3. import com.romanidze.jizzyshop.dao.interfaces.ProfileDAOInterface;
  4. import com.romanidze.jizzyshop.models.Profile;
  5. import com.romanidze.jizzyshop.models.User;
  6.  
  7. import java.sql.Connection;
  8. import java.sql.PreparedStatement;
  9. import java.sql.ResultSet;
  10. import java.sql.SQLException;
  11. import java.util.ArrayList;
  12. import java.util.List;
  13.  
  14. public class ProfileDAOImpl implements ProfileDAOInterface{
  15.  
  16.     private Connection conn;
  17.  
  18.     private static final String INSERT_QUERY = "INSERT INTO profile (country, gender, subscription, user_id)" +
  19.                                                 " VALUES (?, ?, ?, ?)";
  20.     private static final String FIND_QUERY = "SELECT FROM profile WHERE profile.id = ?";
  21.     private static final String FIND_ALL_QUERY = "SELECT * FROM profile";
  22.     private static final String DELETE_QUERY = "DELETE FROM profile WHERE profile.id = ?";
  23.     private static final String UPDATE_QUERY =
  24.             "UPDATE profile SET (country, gender, subscription, user_id) = (?, ?, ?, ?) " +
  25.                     "WHERE profile.id = ?";
  26.     private static final String FIND_BY_USER_QUERY = "SELECT FROM profile WHERE profile.user_id = ?";
  27.     private static final String FIND_BY_COUNTRY_QUERY = "SELECT FROM profile WHERE profile.country = ?";
  28.  
  29.     private ProfileDAOImpl(){}
  30.  
  31.     public ProfileDAOImpl(Connection conn){
  32.  
  33.         this.conn = conn;
  34.  
  35.         try {
  36.             Class.forName("org.postgresql.Driver");
  37.         } catch (ClassNotFoundException e) {
  38.             e.printStackTrace();
  39.         }
  40.  
  41.     }
  42.  
  43.     @Override
  44.     public List<Profile> findAll() {
  45.  
  46.         List<Profile> resultList = new ArrayList<>();
  47.  
  48.         try(PreparedStatement ps = this.conn.prepareStatement(FIND_ALL_QUERY);
  49.             ResultSet rs = ps.executeQuery())
  50.         {
  51.  
  52.            while(rs.next()){
  53.  
  54.                Profile profile = Profile.builder()
  55.                                         .id(rs.getLong("id"))
  56.                                         .country(rs.getString("country"))
  57.                                         .gender(rs.getString("gender"))
  58.                                         .subscription(rs.getString("subscription"))
  59.                                         .userId(rs.getLong("user_id"))
  60.                                         .build();
  61.  
  62.                resultList.add(profile);
  63.  
  64.            }
  65.  
  66.         }catch(SQLException e){
  67.  
  68.             e.printStackTrace();
  69.  
  70.         }
  71.  
  72.         return resultList;
  73.  
  74.     }
  75.  
  76.     @Override
  77.     public void save(Profile model) {
  78.  
  79.         try(PreparedStatement ps = this.conn.prepareStatement(INSERT_QUERY, new String[]{"id"})){
  80.  
  81.             ps.setString(1, model.getCountry());
  82.             ps.setString(2, model.getGender());
  83.             ps.setString(3, model.getSubscription());
  84.             ps.setLong(4, model.getUserId());
  85.  
  86.             ps.executeUpdate();
  87.  
  88.             try(ResultSet rs = ps.getGeneratedKeys()){
  89.  
  90.                 if(rs.next()){
  91.  
  92.                     Long id = rs.getLong(1);
  93.                     model.setId(id);
  94.  
  95.                 }
  96.  
  97.             }
  98.  
  99.         }catch (SQLException e) {
  100.             e.printStackTrace();
  101.         }
  102.  
  103.     }
  104.  
  105.     @Override
  106.     public Profile find(Long id) {
  107.  
  108.         Profile profile = null;
  109.  
  110.         try(PreparedStatement ps = this.conn.prepareStatement(FIND_QUERY)){
  111.  
  112.             ps.setLong(1, id);
  113.  
  114.             try(ResultSet rs = ps.executeQuery()){
  115.  
  116.                 if(rs.next()){
  117.  
  118.                     profile = Profile.builder()
  119.                                      .id(rs.getLong("id"))
  120.                                      .country(rs.getString("country"))
  121.                                      .gender(rs.getString("gender"))
  122.                                      .subscription(rs.getString("subscription"))
  123.                                      .userId(rs.getLong("user_id"))
  124.                                      .build();
  125.  
  126.                 }else{
  127.                     throw new IllegalArgumentException("Profile not found");
  128.                 }
  129.  
  130.             }
  131.  
  132.         }catch (SQLException e) {
  133.             e.printStackTrace();
  134.         }
  135.  
  136.         return profile;
  137.  
  138.     }
  139.  
  140.     @Override
  141.     public void delete(Long id) {
  142.  
  143.         try(PreparedStatement ps = this.conn.prepareStatement(DELETE_QUERY)){
  144.  
  145.             ps.setLong(1, id);
  146.             ps.executeUpdate();
  147.  
  148.         }catch (SQLException e) {
  149.             e.printStackTrace();
  150.         }
  151.  
  152.     }
  153.  
  154.     @Override
  155.     public void update(Profile model) {
  156.  
  157.         try(PreparedStatement ps = this.conn.prepareStatement(UPDATE_QUERY)){
  158.  
  159.             ps.setString(1, model.getCountry());
  160.             ps.setString(2, model.getGender());
  161.             ps.setString(3, model.getSubscription());
  162.             ps.setLong(4, model.getUserId());
  163.  
  164.             ps.executeUpdate();
  165.  
  166.         }catch (SQLException e) {
  167.             e.printStackTrace();
  168.         }
  169.  
  170.     }
  171.  
  172.     @Override
  173.     public Profile findByUser(User user) {
  174.  
  175.         Profile profile = null;
  176.  
  177.         try(PreparedStatement ps = this.conn.prepareStatement(FIND_BY_USER_QUERY)){
  178.  
  179.             ps.setLong(1, user.getId());
  180.  
  181.             try(ResultSet rs = ps.executeQuery()){
  182.  
  183.                 if(rs.next()){
  184.  
  185.                     profile = Profile.builder()
  186.                                      .id(rs.getLong("id"))
  187.                                      .country(rs.getString("country"))
  188.                                      .gender(rs.getString("gender"))
  189.                                      .subscription(rs.getString("subscription"))
  190.                                      .userId(rs.getLong("user_id"))
  191.                                      .build();
  192.  
  193.                 }else{
  194.                     throw new IllegalArgumentException("Profile not found");
  195.                 }
  196.  
  197.             }
  198.  
  199.         }catch (SQLException e) {
  200.             e.printStackTrace();
  201.         }
  202.  
  203.         return profile;
  204.  
  205.     }
  206.  
  207.     @Override
  208.     public Profile findByCountry(String country) {
  209.  
  210.         Profile profile = null;
  211.  
  212.         try(PreparedStatement ps = this.conn.prepareStatement(FIND_BY_COUNTRY_QUERY)){
  213.  
  214.             ps.setString(1, country);
  215.  
  216.             try(ResultSet rs = ps.executeQuery()){
  217.  
  218.                 if(rs.next()){
  219.  
  220.                     profile = Profile.builder()
  221.                             .id(rs.getLong("id"))
  222.                             .country(rs.getString("country"))
  223.                             .gender(rs.getString("gender"))
  224.                             .subscription(rs.getString("subscription"))
  225.                             .userId(rs.getLong("user_id"))
  226.                             .build();
  227.  
  228.                 }else{
  229.                     throw new IllegalArgumentException("Profile not found");
  230.                 }
  231.  
  232.             }
  233.  
  234.         }catch (SQLException e) {
  235.             e.printStackTrace();
  236.         }
  237.  
  238.         return profile;
  239.  
  240.     }
  241. }
Advertisement
Add Comment
Please, Sign In to add comment