Skip to content
Permalink
6dbc4a8127
Switch branches/tags

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?
Go to file
 
 
Cannot retrieve contributors at this time
executable file 85 lines (73 sloc) 2.46 KB
package cfy.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
import cfy.model.*;
public class OrderDao {
private Connection con;
private String query;
private PreparedStatement pst;
private ResultSet rs;
public OrderDao(Connection con) {
super();
this.con = con;
}
public boolean insertOrder(Order model) {
boolean result = false;
try {
query = "insert into orders(p_id, u_id, o_quantity) values(?,?,?)";
pst = this.con.prepareStatement(query);
pst.setInt(1, model.getP_id());
pst.setInt(2, model.getUid());
pst.setInt(3, model.getQunatity());
pst.executeUpdate();
result = true;
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return result;
}
public List<Order> userOrders(int id) {
List<Order> list = new ArrayList<>();
try {
query = "select * from orders where u_id=? order by orders.o_id 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("o_id"));
order.setId(pId);
order.setName(product.getName());
order.setCategory(product.getCategory());
order.setPrice(product.getPrice()*rs.getInt("o_quantity"));
order.setQunatity(rs.getInt("o_quantity"));
order.setDate(rs.getString("o_date"));
list.add(order);
}
} catch (Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
return list;
}
public void cancelOrder(int id) {
//boolean result = false;
try {
query = "delete from orders where o_id=?";
pst = this.con.prepareStatement(query);
pst.setInt(1, id);
pst.execute();
//result = true;
} catch (SQLException e) {
e.printStackTrace();
System.out.print(e.getMessage());
}
//return result;
}
}