Untitled

adminDAOs
 avatar
unknown
plain_text
2 years ago
3.8 kB
5
Indexable
package DAOs;

import Models.Order;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class AdminDAOs {

    public Connection conn; // Use the connection from DBConnection

    public AdminDAOs() throws SQLException, ClassNotFoundException {
        // Initialize the connection when creating an instance of AdminDAOs
        conn = Database.DBConnection.connect();
    }

    public ResultSet GetAllOrder() {
        ResultSet rs = null;
        try {
            String sql = "Select * From OrderList";
            PreparedStatement ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
        } catch (SQLException ex) {
            Logger.getLogger(AdminDAOs.class.getName()).log(Level.SEVERE, null, ex);
        }
        return rs;
    }

//    public void deleteOrders(List<Integer> orderIds) throws SQLException, ClassNotFoundException {
//        try {
//            String sql = "DELETE FROM OrderList WHERE order_id IN (";
//            for (int i = 0; i < orderIds.size(); i++) {
//                sql += "?";
//                if (i < orderIds.size() - 1) {
//                    sql += ", ";
//                }
//            }
//            sql += ")";
//
//            PreparedStatement ps = conn.prepareStatement(sql);
//
//            for (int i = 0; i < orderIds.size(); i++) {
//                ps.setInt(i + 1, orderIds.get(i));
//            }
//
//            ps.executeUpdate();
//        } catch (SQLException ex) {
//            Logger.getLogger(AdminDAOs.class.getName()).log(Level.SEVERE, null, ex);
//        }
//    }
    public int Delete(int id) {
        String sql = "delete from OrderList where id=?";
        int ketqua = 0;

        try {
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setInt(1, id);
            ketqua = ps.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(AdminDAOs.class.getName()).log(Level.SEVERE, null, ex);
        }
        return ketqua;
    }

    public Order GetOrder(int id) {
        String sql = "Select * From OrderList where id=?";
        Order od = null;
        try {
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setInt(1, id);
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                od = new Order(rs.getInt("ID"), rs.getDate("Date"), rs.getString("Address"), rs.getString("Phone"), rs.getString("Product"), rs.getInt("Price"), rs.getDate("DeliveryDate"), rs.getString("Status"));
            }
        } catch (SQLException ex) {
            Logger.getLogger(AdminDAOs.class.getName()).log(Level.SEVERE, null, ex);
        }
        return od;
    }

    public int UpdateOrder(Order newod) {
        int ketqua = 0;
        String sql = "update Order set ID=?, Date=?, Address=?, Phone=?, Product=? ,Status=?,where ID=?";
        try {
            PreparedStatement ps = conn.prepareStatement(sql);

            ps.setInt(1, newod.getID());
            ps.setDate(2, (Date) newod.getDate());
            ps.setString(3, newod.getAddress());
            ps.setString(4, newod.getPhone());
            ps.setString(5, newod.getProduct());
            ps.setInt(6, newod.getPrice());
            ps.setDate(7, (Date) newod.getDeliveryDate());
            ps.setString(8, newod.getStatus());
            ketqua = ps.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(AdminDAOs.class.getName()).log(Level.SEVERE, null, ex);
        }
        return ketqua;
    }
}
Editor is loading...