19.05.18 jsp 쇼핑몰 차량 상세보기 (동영상 63강)

Back-End/JSP 2019. 5. 18. 21:27
728x90
반응형

쇼핑몰 차량 상세보기 구현

 

RentcarDAO.java (db연결)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package db;
 
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Vector;
 
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
 
public class RentcarDAO {
 
    Connection con;
    PreparedStatement pstmt;
    ResultSet rs;
 
    // 커넥션풀을 이용한 데이터베이스 연결
 
    public void getcon() {
 
        // DB에 접속할때는 예외처리를 실시해야됨
        try {
 
            Context initctx = new InitialContext(); // 외부서버로 부터 데이터를 읽어들이는것이기 때문에 드라이버가 없을수 있어
            Context envctx = (Context) initctx.lookup("java:comp/env"); // 자바를 읽어들일수 있는 환경에서 사용 //예외처리를 해준다.
            DataSource ds = (DataSource) envctx.lookup("jdbc/pool");
            con = ds.getConnection();
            // 데이터소스에 username, url, password를 집어넣는다. 그렇게 하면 데이터소스가 커넥션을 얻어 온다.
            // jdbc/pool에 있는 데이터소스를 사용할수 있다.
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
 
        }
    }
 
    // 최신순 3대의 자동차를 리턴하는 메소드
    public Vector<CarListBean> getSelectCar() {
        // 리턴타입을 설정
        Vector<CarListBean> v = new Vector<>();
        getcon(); // 커넥션이 연결되어야 쿼리를 실행 가능
 
        try {
 
            String sql = "select * from rentcar order by no desc ";
            pstmt = con.prepareStatement(sql);
            // 쿼리 실행후 실행결과 Result리턴함
            rs = pstmt.executeQuery();
            int count = 0;
            while (rs.next())// 결과값이 끝날때까지만 실행
            {
                CarListBean bean = new CarListBean();
                bean.setNo(rs.getInt(1));
                bean.setName(rs.getString(2));
                bean.setCategory(rs.getInt(3));
                bean.setPrice(rs.getInt(4));
                bean.setUsepeople(rs.getInt(5));
                bean.setCompany(rs.getString(6));
                bean.setImg(rs.getString(7));
                bean.setInfo(rs.getString(8));
                // 벡터에 빈 클래스를 저장
                v.add(bean);
                count++;
                // 3개만 저장이 되야하기 때문..
                if (count > 3)
                    break// 반복문을 빠져나가시오.
 
            }
            con.close();
        } catch (Exception e) {// 내림차순으로 검색하는 쿼리문 작성
            e.printStackTrace();
        }
        return v;
 
    }
 
    // 카테고리별 자동차 리스트를 저장하는 메소드
    // 벡터로 받았으니 벡터로 리턴함..
    public Vector<CarListBean> getCategoryCar(int cate)
 
    {
        // 리턴타입이 벡터 객체이기 때문에 벡터 객체를 생성한다.
        Vector<CarListBean> v = new Vector<>();
 
        // 데이터를 저장할 빈 클래스 선언
        CarListBean bean = null;
 
        getcon();
 
        try {
            String sql = "select * from rentcar where category=?";
            pstmt = con.prepareStatement(sql);
 
            // ?에 값을 넣는다.
            pstmt.setInt(1, cate);
            // 결과를 리턴
            rs = pstmt.executeQuery();
            // 반복문을 돌려서 데이터를 저장
 
            while (rs.next()) { // 데이터를 저장할 빈 클래스 생성
                bean = new CarListBean();
                bean.setNo(rs.getInt(1));
                bean.setName(rs.getString(2));
                bean.setCategory(rs.getInt(3));
                bean.setPrice(rs.getInt(4));
                bean.setUsepeople(rs.getInt(5));
                bean.setCompany(rs.getString(6));
                bean.setImg(rs.getString(7));
                bean.setInfo(rs.getString(8));
                // 벡터에 빈 클래스를 저장
                v.add(bean);
 
            }
 
            con.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
 
        return v;
 
    }
 
    // 모든 차량을 검색하는 메소드
    public Vector<CarListBean> getAllCar() {
 
        Vector<CarListBean> v = new Vector<>();
        // 데이터를 저장할 빈클래스 선언
        CarListBean bean = null;
 
        getcon();
 
        try {
            String sql = "select * from rentcar";
            pstmt = con.prepareStatement(sql);
            rs = pstmt.executeQuery();
            // 반복문을 돌리면서 데이터를 저장함
 
            while (rs.next()) {// 데이터를 저장할 빈클래스 생성
                bean = new CarListBean();
                bean.setNo(rs.getInt(1));
                bean.setName(rs.getString(2));
                bean.setCategory(rs.getInt(3));
                bean.setPrice(rs.getInt(4));
                bean.setUsepeople(rs.getInt(5));
                bean.setCompany(rs.getString(6));
                bean.setImg(rs.getString(7));
                bean.setInfo(rs.getString(8));
                // 벡터에 빈 클래스를 저장
                v.add(bean);
 
            }
 
            con.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return v;
 
    }
 
    // 하나의 자동차 정보를 리턴하는 메소드
    public CarListBean getOneCar(int no) {
 
        // 리턴타입을 선언한다. 객체를 생성해서 그 객체에 값을 넣은후 리턴해야하기 때문
        CarListBean bean = new CarListBean();
 
        getcon();
 
        try {
            String sql = "select * from rentcar where no=?";
            pstmt = con.prepareStatement(sql);
            // sql문에 ?표가 들어가 있으면 초기 인덱스 값을 할당해주어야 한다.
            pstmt.setInt(1, no);
            rs = pstmt.executeQuery();
 
            if (rs.next()) {
                bean.setNo(rs.getInt(1));
                bean.setName(rs.getString(2));
                bean.setCategory(rs.getInt(3));
                bean.setPrice(rs.getInt(4));
                bean.setUsepeople(rs.getInt(5));
                bean.setCompany(rs.getString(6));
                bean.setImg(rs.getString(7));
                bean.setInfo(rs.getString(8));
 
            }
 
            con.close();
 
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bean;
 
    }
 
}
 
cs

 

 

CarReserveInfo.jsp (차량 상세 보기)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<%@page import="db.CarListBean"%>
<%@page import="db.RentcarDAO"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<body>
 
<% 
    //차량의 상세정보를 보기위해 no값을 가져온다.
    int no = Integer.parseInt(request.getParameter("no"));
 
    //데이터베이스에 접근
    RentcarDAO rdao = new RentcarDAO();
    //렌트카 하나에 대한 정보를 얻어옴
    CarListBean bean = rdao.getOneCar(no);
    
    int category = bean.getCategory();
    String temp = "";
    if (category == 1)
        temp = "소형";
    else if (category == 2)
        temp = "중형";
    else if (category == 3)
        temp = "대형";
    
%>
<center>
<form action="RentcarMain.jsp?center=CarOptionSelect.jsp"></form>
<table width= "1000" >
<tr height="100">
            <td align="center" colspan="3"><font size="6" color="gray">
                        <%=bean.getName() %> 차량 선택
                </font></td>
            </tr>
            <tr>
            <!-- colspan은 가로셀끼리 병합하는 것이고, rowspan은 세로 셀끼리 병합하는 것 -->
            <!-- 차량 사진 옆에 정보들이 떠야하기때문에 세로병합인 rowspan을 사용 -->
            <td rowspan="6" width="500" align="center">
            <img alt="" src="img/<%=bean.getImg()%>" width="450"></td>
            <td width="250" align="center"> 차량이름</td>
            <td width="250" align="center"> <%=bean.getName() %></td>
            </tr>
            
            <tr>
            <td width="250" align="center"> 차량수량</td>
            <td width="250" align="center"> <select name = "qty">
            <option value="1">1</option>
            <option value="2">2</option>
            <option value="3">3</option>
            </select>
            </td>
            </tr>
            
            
            <tr>
            <td width="250" align="center"> 차량분류</td>
            <td width="250" align="center"> <%=temp %>
            </tr>
            
            <tr>
            <td width="250" align="center"> 대여가격</td>
            <td width="250" align="center"> <%=bean.getPrice() %></td>
            </tr>
            
            <tr>
            <td width="250" align="center"> 제조회사</td>
            <td width="250" align="center"> <%=bean.getCompany() %></td>
            </tr>
            
            <tr>
            <td align="center" colspan="2">
                <input type = "hidden" name ="no" value="<%=bean.getNo() %>">
                <input type="submit" value="옵션선택후 구매하기">
                </td>
            </tr>
</table>
<br><br><br>
<font size="6" color="gray"> 차량 정보 보기 </font>
<p>
<%=bean.getInfo() %>
</center>
</body>
</html>
cs

 

 

 

728x90
반응형
: