DEV Community

Saravanan Lakshmanan
Saravanan Lakshmanan

Posted on

Java Learning Series - Product Management System with CRUD operations for beginners

Product.java

package com.company.entity;

public class Product{

    private int id;
    private String name;
    private String category;
    private int price;

    public Product(int id, String name, String category, int price) {
        this.id = id;
        this.name = name;
        this.category = category;
        this.price = price;
    }

    public int getId() {
        return this.id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCategory() {
        return this.category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public int getPrice() {
        return this.price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public String toString() {
        return this.getId() + " , " + this.getName() + " , " + this.getCategory() + " , " + this.getPrice();
    }
}
Enter fullscreen mode Exit fullscreen mode

ProductService.java

package com.company.service;
import com.company.entity.Product;
import com.company.db.DBConnection;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.ArrayList;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class ProductService{

    ArrayList<Product> arr = new ArrayList();

    public void addProduct(Product product) {
        try{
            Connection con = DBConnection.getPSQLConnection();
        String sql = "insert into products values(?,?,?,?)";
        PreparedStatement ps = con.prepareStatement(sql);
            ps.setInt(1, product.getId());
            ps.setString(2,product.getName());
            ps.setString(3,product.getCategory());
            ps.setInt(4,product.getPrice());
        int row = ps.executeUpdate();
            System.out.println(row);
        }catch(SQLException exe_obj){
            System.out.println(exe_obj.getMessage());
        }

//        boolean addStatus = arr.add(product);
//        if (addStatus) {
//            System.out.println("Product is Added");
//        } else {
//            System.out.println("Product is not Added");
//        }
    }

    public ArrayList<Product> viewProduct() {
//        for (Product product : arr) {
//            System.out.println(product);
//        }
//            arr.clear();
            try{
                Connection con = DBConnection.getPSQLConnection();
                String sql = "select * from products";
                PreparedStatement ps = con.prepareStatement(sql);
//                System.out.println(ps);
                ResultSet rs = ps.executeQuery();
//                System.out.println(rs);
                while(rs.next()){     //next() - is used to read data(row by row)
//                    System.out.println(rs.getInt("id"));
//                    System.out.println(rs.getString("name"));
//                    System.out.println(rs.getString("category"));
//                    System.out.println(rs.getInt("price"));   
//                if(rs.next()){
//                    System.out.println("Row found");
//                }else{
//                    System.out.println("Row not found");
//                }

                int id = rs.getInt("id");
                String name = rs.getString("name");
                String category = rs.getString("category");
                int price = rs.getInt("price");
                //after reading datas from database. It is converted into Product object
                Product product = new Product(id,name,category,price);
                //storing the object in ArrayList
                arr.add(product);
                }

            }catch(SQLException exe){
                System.out.println("SQLException");
            }
    return arr;
    }

    public void searchProduct(int id) {
        try{
            Connection con = DBConnection.getPSQLConnection();
            String sql = "select * from products where id = ?";
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setInt(1,id);

            ResultSet rs = ps.executeQuery();

            if(rs.next()){
                System.out.println(rs.getInt("id"));
                System.out.println(rs.getString("name"));
                System.out.println(rs.getString("category"));
                System.out.println(rs.getInt("price"));
            }else{
                System.out.println("Product is Not Found");
            }        

        }catch(SQLException exe){
            System.out.println(exe.getMessage());
        }







//        System.out.println();
//        boolean found = false;
//
//        for (Product product : arr) {
//            if (product.getId() == id) {
//                System.out.println(product.getId() + " - " + product.getName() + " - " + product.getCategory() + " - " + product.getPrice());
//                found = true;
//                break;
//            }
//        }
//
//        if (found == true) {
//            System.out.println("Product is Found");
//        } else {
//            System.out.println("Product is not Found");
//        }
    }

    public void updateProduct(Product newProduct) {
        try{
            Connection con = DBConnection.getPSQLConnection();
            String sql = "update products set name = ?, category = ?, price = ? where id = ?";
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setString(1,newProduct.getName());
            ps.setString(2,newProduct.getCategory());
            ps.setInt(3,newProduct.getPrice());
            ps.setInt(4,newProduct.getId());
            int updateStatus = ps.executeUpdate();
            System.out.println(updateStatus);

            if(updateStatus > 0){
                System.out.println("Product is Updated");
            }else{
                System.out.println("Product is Not Updated");
            }    
        }catch(SQLException exe){
                System.out.println(exe.getMessage());
        }

//        for (Product existingProduct : arr) {
//            if (existingProduct.getId() == newProduct.getId()) {
//                existingProduct.setName(newProduct.getName());
//                existingProduct.setCategory(newProduct.getCategory());
//                existingProduct.setPrice(newProduct.getPrice());
//            }
//        }
    }

    public void deleteProduct(int id) {
        try{
            Connection con = DBConnection.getPSQLConnection();
            String sql = "delete from products where id = ?";
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setInt(1,id);
            int deleteStatus = ps.executeUpdate();
            System.out.println(deleteStatus);

            if(deleteStatus > 0){
                System.out.println("Product is Deleted");
            }else{
                System.out.println("Product is Not Found");
            }
        }catch(SQLException exe){
            System.out.println(exe.getMessage());
        }
    }



//        boolean deleteStatus = false;
//
//        for (Product product : arr) {
//            if (product.getId() == id) {
//                arr.remove(product);
//                deleteStatus = true;
//                break;
//            }
//        }
//
//        if (deleteStatus == true) {
//            System.out.println("Product is Deleted");
//        } else {
//            System.out.println("Product is Not Found");
//        }
//    }
}
Enter fullscreen mode Exit fullscreen mode

Main.java

package com.company.controller;
import com.company.entity.Product;
import com.company.service.ProductService;
import java.util.ArrayList;

import java.io.File;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.FileNotFoundException;

public class Main{

    public static void main(String[] args) {

        ProductService service = new ProductService();


        Product product = new Product(3,"Laptop","Electronics",50000);
//        service.addProduct(product);

        ArrayList<Product> arr1 = service.viewProduct();
        System.out.println(arr1);
//        service.searchProduct(1);
        service.deleteProduct(1);

        System.out.println(service.viewProduct());   

        System.out.println(service.viewProduct());
        Product p1 = new Product(2,"Ipad","Electronics",40000);
        service.updateProduct(p1);

        System.out.println(service.viewProduct());

        service.searchProduct(3);

//        File file = new File("products.csv");
//        System.out.println(file.canRead());
//        
//        try{
//            FileReader reader = new FileReader(file);
//            BufferedReader br = new BufferedReader(reader);
//            String line = br.readLine();
//
//            line = br.readLine();
//            while(line!=null){
//                String[] data = line.split(",");
//                System.out.println(data);
//                System.out.println(line);
//
//                    int id = Integer.parseInt(data[0]);
//                    String name = data[1];
//                    String category = data[2];
//                    int price = Integer.parseInt(data[3]);
//
//                Product product = new Product(id, name, category, price);
//                service.addProduct(product);
//
//                    System.out.println(data[0]);
//                    System.out.println(data[1]);
//                    System.out.println(data[2]);
//                    System.out.println(data[3]);
//
//                for(int i = 0; i<data.length; i++){
//                    System.out.println(data[i]);
//                }
//
//                for(String str : data){
//                    System.out.println(str);
//                }
//                line = br.readLine();
//            }
//
//
//
//        }catch(FileNotFoundException file_Obj){
//            System.out.println("FileNotFoundException - Error");
//        }catch(IOException io_Obj){
//            System.out.println("IOException - Error");
//        }
//
//
//        Product p1 = new Product(101, "Laptop", "Electronics", 50000);
//        Product p2 = new Product(102, "Mobile", "Electronics", 30000);
//        Product p3 = new Product(102, "Chair", "Furniture", 5000);
//        service.addProduct(p2);
//
//        service.viewProduct();
//
//        service.searchProduct(102);
//
//        service.updateProduct(p3);
//
//        service.viewProduct();
//
//        service.deleteProduct(101);
//
//        service.viewProduct();
//
    }
}
Enter fullscreen mode Exit fullscreen mode

DBConnection.java

package com.company.db;

import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;

public class DBConnection{

    public static Connection getPSQLConnection(){
        Connection con = null;
        try{
            con = DriverManager.getConnection("jdbc:postgresql://localhost:5432/productdb", "postgres", "saravanan");
            System.out.println("SQL is connected");
        }
        catch(SQLException obj){
            System.out.println("SQL not connected");
        }
    return con;
    }


//public static void main(String[] args){
//    DBConnection.getPSQLConnection();
//}

}
Enter fullscreen mode Exit fullscreen mode

Output:

SQL is connected
[3 , Laptop , Electronics , 50000, 2 , Ipad , Electronics , 40000]
SQL is connected
0
Product is Not Found
SQL is connected
[3 , Laptop , Electronics , 50000, 2 , Ipad , Electronics , 40000, 3 , Laptop , Electronics , 50000, 2 , Ipad , Electronics , 40000]
SQL is connected
[3 , Laptop , Electronics , 50000, 2 , Ipad , Electronics , 40000, 3 , Laptop , Electronics , 50000, 2 , Ipad , Electronics , 40000, 3 , Laptop , Electronics , 50000, 2 , Ipad , Electronics , 40000]
SQL is connected
1
Product is Updated
SQL is connected
[3 , Laptop , Electronics , 50000, 2 , Ipad , Electronics , 40000, 3 , Laptop , Electronics , 50000, 2 , Ipad , Electronics , 40000, 3 , Laptop , Electronics , 50000, 2 , Ipad , Electronics , 40000, 3 , Laptop , Electronics , 50000, 2 , Ipad , Electronics , 40000]
SQL is connected
3
Laptop
Electronics
50000
Enter fullscreen mode Exit fullscreen mode

Top comments (0)