Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
7051CEM_11779852_AttikoOmotayoOladimeji/src/main/java/com/ecommerce/dao/OrderDao.java
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
81 lines (66 sloc)
2.39 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license | |
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template | |
*/ | |
package com.ecommerce.dao; | |
import com.ecommerce.loginpack.model.*; | |
import java.sql.Connection; | |
import java.sql.PreparedStatement; | |
import java.sql.ResultSet; | |
import java.util.*; | |
/** | |
* | |
* @author oladimeji | |
*/ | |
public class OrderDao { | |
private Connection con; | |
private String query; | |
private PreparedStatement pst; | |
private ResultSet rs; | |
public OrderDao(Connection con){ | |
this.con = con; | |
} | |
public boolean insertOrder(Order model){ | |
boolean result = false; | |
try{ | |
query = "insert into orders (p_id, u_id, o_quantity, o_date) values(?,?,?,?)"; | |
pst = this.con.prepareStatement(query); | |
pst.setInt(1, model.getProductid()); | |
pst.setInt(2, model.getUid()); | |
pst.setInt(3, model.getQuantity()); | |
pst.setString(4, model.getDate()); | |
pst.executeUpdate(); | |
result = true; | |
} | |
catch(Exception e){ | |
e.printStackTrace(); | |
} | |
return result; | |
} | |
public List<Order> userOrders(int id){ | |
List<Order> list = new ArrayList<>(); | |
try{ | |
query = "select * from orders where u_id=? order by orders.orderid desc"; | |
pst = this.con.prepareStatement(query); | |
pst.setInt(1, id); | |
rs = pst.executeQuery(); | |
while(rs.next()){ | |
Order order = new Order(); | |
ProductDao productDao = new ProductDao(this.con); | |
int pId = rs.getInt("p_id"); | |
Product product = productDao.getSingleProduct(pId); | |
order.setOrderId(rs.getInt("orderid")); | |
order.setProductid(pId); | |
order.setName(product.getName()); | |
order.setCategory(product.getCategory()); | |
order.setPrice(product.getPrice()*rs.getInt("o_quantity")); | |
order.setQuantity(rs.getInt("o_quantity")); | |
order.setDate(rs.getString("o_date")); | |
list.add(order); | |
} | |
}catch(Exception e){ | |
e.printStackTrace(); | |
} | |
return list; | |
} | |
} |