Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.io.FileNotFoundException;
- import java.sql.*;
- import java.util.ArrayList;
- import java.util.List;
- public class DatabaseDataStorage implements DataStorage {
- @Override
- public List<Product> findAll() throws ClassNotFoundException, SQLException {
- Class.forName("org.postgresql.Driver");
- Connection connection = DriverManager.getConnection(
- "jdbc:postgresql://localhost:5432/products","postgres", "postgres");
- Statement s = connection.createStatement();
- ResultSet rs = s.executeQuery("SELECT * FROM product");
- List<Product> productList = new ArrayList<>();
- while (rs.next()) {
- int id = rs.getInt("id");
- String name = rs.getString("name");
- int price = rs.getInt("price");
- int count = rs.getInt("count");
- Product p = new Product(id, name, price, count);
- productList.add(p);
- }
- return productList;
- }
- @Override
- public Product findById(int id) throws FileNotFoundException {
- return null;
- }
- @Override
- public Product findMostExpensiveProduct() throws FileNotFoundException {
- return null;
- }
- @Override
- public Product save(Product product) {
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement