dao/CartDAO.java > MODEL2

본문 바로가기
사이트 내 전체검색

MODEL2

dao/CartDAO.java

페이지 정보

profile_image
작성자 관리자
댓글 0건 조회 726회 작성일 23-03-25 09:26

본문

package dao;


import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.Statement;

import java.util.ArrayList;


import model.Cart;

import model.Item;


public class CartDAO {


Statement stmt;

PreparedStatement pstmt;

ResultSet rs;


DAO d = new DAO();


//추가

public void insertCart(Cart c) {

d.getCon();


try {

String sql = "insert into cart values (null,?,?,?,?,?,?,?,?)";


pstmt = d.conn.prepareStatement(sql);


pstmt.setString(1, c.getOd_id());

pstmt.setString(2, c.getMb_id());

pstmt.setString(3, c.getIt_uid());

pstmt.setInt(4, c.getIt_price());

pstmt.setInt(5, c.getIt_point());

pstmt.setInt(6, c.getIt_qty());

pstmt.setString(7, c.getCt_status());

pstmt.setString(8, c.getCt_date());


pstmt.executeUpdate();

}catch(Exception e) {

e.printStackTrace();

}finally {

try {

pstmt.close();

d.conn.close();

}catch(Exception e) {

e.printStackTrace();

}

}

}


//장바구니 상품 존재 여부

public int selectItem(Cart c) {

d.getCon();


int num = 0;


try {

String sql = "select count(*) from cart where od_id=? and it_uid=?";


pstmt = d.conn.prepareStatement(sql);


pstmt.setString(1, c.getOd_id());

pstmt.setString(2, c.getIt_uid());


rs = pstmt.executeQuery();


if(rs.next()) {

num = rs.getInt(1);

}

} catch (Exception e) {

e.printStackTrace();

}finally {

try {

rs.close();

pstmt.close();

d.conn.close();

}catch(Exception e) {

e.printStackTrace();

}

}

return num;

}


//장바구니 상품 존재시 수량 업데이트

public void oneUpdate(Cart c) {

d.getCon();


try {

String sql = "update cart set it_qty = it_qty + ? where od_id=? and it_uid=?";


pstmt = d.conn.prepareStatement(sql);


pstmt.setInt(1, c.getIt_qty());

pstmt.setString(2, c.getOd_id());

pstmt.setString(3, c.getIt_uid());


pstmt.executeUpdate();

} catch (Exception e) {

e.printStackTrace();

}finally {

try {

pstmt.close();

d.conn.close();

}catch(Exception e) {

e.printStackTrace();

}

}

}


//총 수

public int getAllcount(String field, String search) {

d.getCon();


int count = 0;


try {

String sql = "select count(*) from cart";

if(search != null && !search.equals("")) {

sql = "select count(*) from cart where "+field+" like '%"+search+"%'";

}

stmt = d.conn.createStatement();


rs = stmt.executeQuery(sql);


if(rs.next()) {

count = rs.getInt(1);

}

}catch(Exception e) {

e.printStackTrace();

}finally {

try {

stmt.close();

d.conn.close();

}catch(Exception e) {

e.printStackTrace();

}

}

return count;

}


//목록

public ArrayList<Cart> getAllMember(int startRow, int endRow, String field, String search) {

d.getCon();


ArrayList<Cart> v = new ArrayList<Cart>();


try {

String sql = "select *,(select file1 from item where item.it_uid=cart.it_uid) as file1 from cart order by ct_uid desc limit ?,?";

if(search != null && !search.equals("")) {

sql = "select *,(select file1 from item where item.it_uid=cart.it_uid) as file1 from cart where "+field+" like '%"+search+"%' order by ct_uid asc limit ?,?";

}


pstmt = d.conn.prepareStatement(sql);


pstmt.setInt(1, startRow);

pstmt.setInt(2, endRow);


rs = pstmt.executeQuery();


while(rs.next()) {

Cart c = new Cart();


String ct_date = rs.getString("ct_date").substring(0, 10); //등록일 년월일


c.setCt_uid(rs.getInt("ct_uid"));

c.setOd_id(rs.getString("od_id"));

c.setMb_id(rs.getString("mb_id"));

c.setIt_uid(rs.getString("it_uid"));

c.setIt_price(rs.getInt("it_price"));

c.setIt_point(rs.getInt("it_point"));

c.setIt_qty(rs.getInt("it_qty"));

c.setCt_status(rs.getString("ct_status"));

c.setCt_date(ct_date);

c.setFile1(rs.getString("file1"));


v.add(c);

}

}catch(Exception e) {

e.printStackTrace();

}finally {

try {

rs.close();

pstmt.close();

d.conn.close();

}catch(Exception e) {

e.printStackTrace();

}

}

return v;

}


//삭제

public void deleteOne(int ct_uid) {

d.getCon();


try {

String sql = "delete from cart where ct_uid=?";


pstmt = d.conn.prepareStatement(sql);

pstmt.setInt(1, ct_uid);


pstmt.executeUpdate();

} catch (Exception e) {

e.printStackTrace();

} finally {

try {

pstmt.close();

d.conn.close();

}catch(Exception e) {

e.printStackTrace();

}

}

}


//수량 변경 처리

public void qtyUpdate(int ct_uid, String str) {

d.getCon();


try {

String sql = "update cart set it_qty = it_qty - 1 where ct_uid=?"; //감소 버튼

if(str.equals("p")) {

sql = "update cart set it_qty = it_qty + 1 where ct_uid=?"; //증가 버튼

}


pstmt = d.conn.prepareStatement(sql);

pstmt.setInt(1, ct_uid);


pstmt.executeUpdate();

} catch (Exception e) {

e.printStackTrace();

} finally {

try {

pstmt.close();

d.conn.close();

}catch(Exception e) {

e.printStackTrace();

}

}

}


}


추천0

댓글목록

등록된 댓글이 없습니다.

회원로그인

회원가입

사이트 정보

공지사항
자유게시판
질문답변
1:1문의

 

별명 : 터푸가위
주소 : 부산시 동래구 명장로20번길 90
대표 : 박규태
메일 : dancepkt@******.com

접속자집계

오늘
187
어제
123
최대
10,760
전체
263,786
Copyright © dancePKT . All rights reserved.