Advertisement
MiklePovolotski

DataBase

Nov 20th, 2017
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.10 KB | None | 0 0
  1. package pro.aloginov.revoluttest;
  2.  
  3. import javax.inject.Inject;
  4. import javax.inject.Named;
  5. import javax.inject.Singleton;
  6. import javax.ws.rs.Path;
  7. import javax.ws.rs.Produces;
  8. import javax.ws.rs.core.MediaType;
  9. import java.sql.*;
  10. import java.util.ArrayList;
  11. import java.util.List;
  12. import java.util.Optional;
  13. import java.util.concurrent.CompletableFuture;
  14. import java.util.concurrent.ExecutorService;
  15.  
  16. @Singleton
  17. @Path("transfer")
  18. @Produces(MediaType.APPLICATION_JSON)
  19.  
  20.  
  21. public class DatabaseService {
  22.  
  23.     Connection connection;
  24.  
  25.     private final ExecutorService ex;
  26.  
  27.     @Inject
  28.     public DatabaseService(@Named("ex") ExecutorService ex) {
  29.         this.ex = ex;
  30.     }
  31.  
  32.  
  33.     public Connection creAteConnection() {
  34.  
  35.         try {
  36.             connection = DriverManager.getConnection("jdbc:postgresql://localhost:5432/postgres", "postgres",
  37.                     "1988");
  38.  
  39.         } catch (SQLException e) {
  40.             throw new RuntimeException(e);
  41.         }
  42.  
  43.         return connection;
  44.     }
  45.  
  46.     private static void mapParams(PreparedStatement ps, Object... args) throws SQLException {
  47.         int i = 1;
  48.  
  49.         for (Object arg : args) {
  50.  
  51.             if (arg instanceof Date) {
  52.                 ps.setTimestamp(i++, new Timestamp(((Date) arg).getTime()));
  53.             } else if (arg instanceof Integer) {
  54.                 System.out.print(i);
  55.                 ps.setInt(i++, (Integer) arg);
  56.                 System.out.print(i);
  57.             } else if (arg instanceof Long) {
  58.                 ps.setLong(i++, (Long) arg);
  59.             } else if (arg instanceof Double) {
  60.                 ps.setDouble(i++, (Double) arg);
  61.             } else if (arg instanceof Float) {
  62.                 ps.setFloat(i++, (Float) arg);
  63.             } else {
  64.                 ps.setString(i++, (String) arg);
  65.             }
  66.         }
  67.     }
  68.  
  69.     public <T> CompletableFuture<List<T>> loadAsync(String sql, RowMapper<T> rowMapper, Object... params) {
  70.         return CompletableFuture.supplyAsync(() -> loadMany(sql, rowMapper, params), ex);
  71.     }
  72.  
  73.     public <T> CompletableFuture<T> loadsingle(String sql, RowMapper<T> rowMapper, Object... params) {
  74.         return CompletableFuture.supplyAsync(() -> loadOne(sql, rowMapper, params), ex);
  75.     }
  76.  
  77.     public <T> T loadOne(String sql, RowMapper<T> rowMapper, Object... params) {
  78.         try {
  79.             PreparedStatement ps = connection.prepareStatement(sql);
  80.             mapParams(ps, params);
  81.             return loadOptional(ps, rowMapper);
  82.         } catch (Exception e) {
  83.             System.out.print(sql);
  84.             throw new RuntimeException(e);
  85.         }
  86.     }
  87.  
  88.  
  89.     public <T> List<T> loadMany(String sql, RowMapper<T> rowMapper, Object... params) {
  90.         try {
  91.             PreparedStatement ps = connection.prepareStatement(sql);
  92.  
  93.             mapParams(ps, params);
  94.  
  95.             return load(ps, rowMapper);
  96.         } catch (Exception e) {
  97.             throw new RuntimeException(e);
  98.         }
  99.     }
  100.  
  101.  
  102.     private <T> List<T> load(PreparedStatement ps, RowMapper<T> rowMapper) throws SQLException {
  103.         ResultSet rs = ps.executeQuery();
  104.         List<T> result = new ArrayList<>();
  105.         while (rs.next()) {
  106.             T element = rowMapper.get(rs);
  107.             result.add(element);
  108.         }
  109.         return result;
  110.     }
  111.  
  112.     public interface RowMapper<T> {
  113.         T get(ResultSet rs) throws SQLException;
  114.  
  115.  
  116.     }
  117.  
  118.     private <T> T loadOptional(PreparedStatement ps, RowMapper<T> rowMapper) throws SQLException {
  119.         ResultSet rs = ps.executeQuery();
  120.         Optional<T> element = null;
  121.  
  122.  
  123.         while (rs.next()) {
  124.  
  125.             element = Optional.of(rowMapper.get(rs));
  126.  
  127.  
  128.         }
  129.  
  130.  
  131.         return element.get();
  132.     }
  133. }
  134.  
  135.   /*  @Inject
  136.     public DatabaseService() {
  137.         try {
  138.             connection = DriverManager.getConnection("jdbc:postgresql://localhost:5432/postgres", "postgres",
  139.                     "1988");
  140.  
  141.         } catch (SQLException ex) {
  142.             // log an exception. fro example:
  143.             System.out.println("Failed to create the database connection.");
  144.         }
  145.     }
  146. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement