Java Learning Series - Updated Product Management System with a DAO class
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 categor
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;
}
// Product p1 = new Product(int id, String name, String category, int 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;
}
@Override
public String toString(){
return this.getId() + "-" + this.getName() + "-" + this.getCategory() + "-" + this.getPrice();
}
}
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{
String url = "jdbc:postgresql://localhost:5432/productdb";
String username = "postgres";
String password = "saravanan";
con = DriverManager.getConnection(url, username, password);
System.out.println("SQL is Connected");
}catch(SQLException exe){
System.out.println(exe.getMessage());
}
return con;
}
}
ProductDAO.java
package com.company.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.sql.SQLException;
import com.company.entity.Product;
import com.company.db.DBConnection;
public class ProductDAO{
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 + " product added");
}catch(SQLException exe){
System.out.println(exe.getMessage());
}
}
public ArrayList<Product> viewProduct(){
ArrayList<Product> arr = new ArrayList();
try{
Connection con = DBConnection.getPSQLConnection();
String sql = "select * from products";
PreparedStatement ps = con.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
while(rs.next()){
int id = rs.getInt("id");
String name = rs.getString("name");
String category = rs.getString("category");
int price = rs.getInt("price");
Product p = new Product(id, name, category, price);
arr.add(p);
}
}catch(SQLException exe){
System.out.println(exe.getMessage());
}
return arr;
}
public void updateProduct(Product product){
try{
Connection con = DBConnection.getPSQLConnection();
String sql = "update products set name = ?, category = ?, price = ? where id = ?";
PreparedStatement ps = con.prepareStatement(sql);
ps.setString(1,product.getName());
ps.setString(2,product.getCategory());
ps.setInt(3,product.getPrice());
ps.setInt(4,product.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());
}
}
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());
}
}
}
ProductService.java
package com.company.service;
import com.company.dao.ProductDAO;
import com.company.entity.Product;
import java.util.ArrayList;
public class ProductService{
ProductDAO dao = new ProductDAO();
public void addProduct(Product product){
dao.addProduct(product);
}
public ArrayList<Product> viewProduct(){
return dao.viewProduct();
}
public void updateProduct(Product product){
dao.updateProduct(product);
}
public void deleteProduct(int id){
dao.deleteProduct(id);
}
public Product findTopPrice(){
ArrayList<Product> arr = dao.viewProduct();
Product topPrice = arr.get(0);
for(Product product : arr){
if(topPrice.getPrice() < product.getPrice()){
topPrice = product;
}
}
return topPrice;
}
}
Main.java
package com.company.controller;
import com.company.dao.ProductDAO;
import com.company.entity.Product;
import java.util.ArrayList;
import com.company.service.ProductService;
public class Main{
public static void main(String[] args){
ProductDAO dao = new ProductDAO();
ArrayList<Product> arr = dao.viewProduct();
System.out.println(arr);
Product p = new Product(1,"Chair","Furniture",10000);
// dao.addProduct(p);
arr = dao.viewProduct();
System.out.println(arr);
ProductService service = new ProductService();
Product p1 = service.findTopPrice();
System.out.println(p1);
}
}
Output:
SQL is Connected
[3-Laptop-Electronics-50000, 2-Ipad-Electronics-40000, 1-Chair-Furniture-10000]
SQL is Connected
[3-Laptop-Electronics-50000, 2-Ipad-Electronics-40000, 1-Chair-Furniture-10000]
SQL is Connected
3-Laptop-Electronics-50000
📰 Read the original article on Dev.to WebDev
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.