Java và MySQL – CRUD bảng order_items

Thêm order_item mới

Câu lệnh SQL

INSERT INTO ORDER_ITEMS(ID, QUANTITY, PRICE, ORDER_ID, PRODUCT_ID) VALUES(NULL, ?, ?, ?, ?)

Thực thi

public boolean insert() {
		// TODO Auto-generated method stub
		String sql = "INSERT INTO ORDER_ITEMS(ID, QUANTITY, PRICE, ORDER_ID, PRODUCT_ID) VALUES(NULL, ?, ?, ?, ?)";
		try {
			PreparedStatement stmt = con.prepareStatement(sql);
			stmt.setInt(1,"orderItem Quantity");
			stmt.setDouble(2, "orderItem Price");
			stmt.setInt(3, "orderItem OrderId");
			stmt.setInt(4, "orderItem ProductId");
			
			stmt.execute();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

Cập nhật order_item

Câu lệnh SQL

UPDATE ORDER_ITEMS SET quantity = ?, price = ?, order_id = ?, product_id = ? WHERE id = ?

Thực thi

public boolean update() {
		// TODO Auto-generated method stub
		String sql = "UPDATE ORDER_ITEMS SET quantity = ?, price = ?, order_id = ?, product_id = ? WHERE id = ?";
		try {
			PreparedStatement stmt = con.prepareStatement(sql);
			stmt.setInt(1,"orderItem Quantity");
			stmt.setDouble(2, "orderItem Price");
			stmt.setInt(3, "orderItem OrderId");
			stmt.setInt(4, "orderItem ProductId");
			return stmt.execute();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

Xoá order_item

Câu lệnh SQL

SELECT * FROM ORDER-ITEMS

Thực thi

public boolean delete(int id) {
		// TODO Auto-generated method stub
		String sql = "DELETE FROM ORDER-ITEMS WHERE ID = ?";
		try {
			PreparedStatement stmt = con.prepareStatement(sql);
			stmt.setInt(1, id);
			
			return stmt.execute();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

Tìm kiếm order_item theo id

Câu lệnh SQL

SELECT * FROM USERS WHERE ID = ?

Thực thi

public void find(int id) {
		// TODO Auto-generated method stub
		String sql = "SELECT * FROM ORDER-ITEMS" ;
		try {
			PreparedStatement stmt = con.prepareStatement(sql);
			
			ResultSet rs = stmt.executeQuery();
			while (rs.next()) {
				int quantity = rs.getInt("quantity");
				double price = rs.getDouble("price");
				int orderId = rs.getInt("order_id");
				int productId = rs.getInt("product_id");
				
                System.out.format("%d:%d:%.2f:%d:%d", id, quantity, price, orderId, productId).println();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

Lấy toàn bộ order_items

Câu lệnh SQL

SELECT * FROM CATEGORIES

Thực thi

public void findAll() {
		// TODO Auto-generated method stub
		String sql = "SELECT * FROM CATEGORIES" ;
		try {
			PreparedStatement stmt = con.prepareStatement(sql);
			
			ResultSet rs = stmt.executeQuery();
			while (rs.next()) {
				int id = rs.getInt("id");
				int quantity = rs.getInt("quantity");
				double price = rs.getDouble("price");
				int orderId = rs.getInt("order_id");
				int productId = rs.getInt("product_id");
				
                System.out.format("%d:%d:%.2f:%d:%d", id, quantity, price, orderId, productId).println();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

Tìm kiếm theo order

Câu lệnh SQL

SELECT * FROM ORDER_ITEMS WHERE order_id = ?

Thực thi

public void findByOder(int orderId) {
        String sql = "SELECT * FROM ORDER_ITEMS WHERE order_id = ?";
        try {
            PreparedStatement stmt = con.prepareStatement(sql);
            stmt.setInt(1, orderId);

            ResultSet rs = stmt.executeQuery();
            while (rs.next()) {
                int id = rs.getInt("id");
                int quantity = rs.getInt("quantity");
                double price = rs.getDouble("price");
                int productId = rs.getInt("product_id");
                System.out.format("%d:%d:%.2f:%d:%d", id, quantity, price, orderId, productId).println();
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
	}