Advertisement
Guest User

Untitled

a guest
Jun 15th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.31 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.io.FileNotFoundException;
  4. import java.sql.*;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7.  
  8. public class DatabaseDataStorage implements DataStorage {
  9.     @Override
  10.     public List<Product> findAll() throws ClassNotFoundException, SQLException {
  11.         Class.forName("org.postgresql.Driver");
  12.         Connection connection = DriverManager.getConnection(
  13.                 "jdbc:postgresql://localhost:5432/products","postgres", "postgres");
  14.         Statement s = connection.createStatement();
  15.         ResultSet rs = s.executeQuery("SELECT * FROM product");
  16.         List<Product> productList = new ArrayList<>();
  17.         while (rs.next()) {
  18.             int id = rs.getInt("id");
  19.             String name = rs.getString("name");
  20.             int price = rs.getInt("price");
  21.             int count = rs.getInt("count");
  22.             Product p = new Product(id, name, price, count);
  23.             productList.add(p);
  24.         }
  25.         return productList;
  26.     }
  27.  
  28.     @Override
  29.     public Product findById(int id) throws FileNotFoundException {
  30.         return null;
  31.     }
  32.  
  33.     @Override
  34.     public Product findMostExpensiveProduct() throws FileNotFoundException {
  35.         return null;
  36.     }
  37.  
  38.     @Override
  39.     public Product save(Product product) {
  40.         return null;
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement