19.05.17 JSP 쇼핑몰 차량 예약하기 (동영상 60강)

Back-End/JSP 2019. 5. 17. 15:01
728x90
반응형

CarListBean.java (빈 클래스)


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
package db;
 
public class CarListBean { //빈 클래스 생성함
    
    
    private int no;
    private String name;
    private int category;
    private int price;
    private int usepeople;
    private String company;
    private String img;
    private String info;
    
    
    public int getNo() {
        return no;
    }
    public void setNo(int no) {
        this.no = no;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getCategory() {
        return category;
    }
    public void setCategory(int category) {
        this.category = category;
    }
    public int getPrice() {
        return price;
    }
    public void setPrice(int price) {
        this.price = price;
    }
    public int getUsepeople() {
        return usepeople;
    }
    public void setUsepeople(int usepeople) {
        this.usepeople = usepeople;
    }
    public String getImg() {
        return img;
    }
    public void setImg(String img) {
        this.img = img;
    }
    public String getInfo() {
        return info;
    }
    public void setInfo(String info) {
        this.info = info;
    }
    
    public String getCompany() {
        return company;
    }
    public void setCompany(String company) {
        this.company = company;
    }
}
 
cs



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
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 > 2)
                    break// 반복문을 빠져나가시오.
 
            }
            con.close();
        } catch (Exception e) {// 내림차순으로 검색하는 쿼리문 작성
            e.printStackTrace();
        }
        return v;
 
    }
 
}
 
cs



Bottom.jsp (하단)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<body>
<center>
    <table width="1000">
        <tr height="100">
            <td align="center">
                <hr color="red" size="5"> 이용약관 이메일 무단수집거부 개인정보 취급(처리)방침 윤리경영
                        보안신고 Contact Us 사업장 소개 사이트맵 웹접근성 도움말<br> 배드민턴단 COPYRIGHT@2015
                        SAMSUNG ELECTRO-MECHANICS, All rights reserved.
                    </td>
                </tr>
            </table>
        </center>
    </body>
</html>
cs



CarReserveMain.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
<%@page import="db.CarListBean"%>
<%@page import="java.util.Vector"%>
<%@page import="db.RentcarDAO"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<body>
    <center>
 
        <!-- 데이터베이스에 연결하여 최신순 자동차 3대만 뿌려주는 데이터를 가져옴 -->
 
        <%
            RentcarDAO rdao = new RentcarDAO();
            //벡터를 이용하여 자동차데이터를 저장함
            Vector<CarListBean> v = rdao.getSelectCar();
        %>
 
        <table width="100">
            <tr height="240">
                <!-- 벡터에 저장된 이미지를 하나씩 출력해야하기 때문에 for문을 돌리고, 그 값들을 빈클래스 변수에 넣어준다. -->
                <%
                    for (int i = 1; i < v.size(); i++) {
                        CarListBean bean = v.get(i);
                %>
                <td width="333" align="center">
                    <!-- 이미지는 벡터의 0번지에 해당되는걸 가져온다. for문을 돌려 td를 출력해야 한다.--> <!-- 확장자는 이미지안에 있으므로 궂이 적지 않아도 된다. -->
                    <!-- 이미지를 누르면 바로 상세정보로 넘어갈수 있도록 하기위해  <a>태그를 걸고, No에 대한 상세정보가 출력되도록 하고
        이름을 사진밑에 출력하도록 한다. --> <a
                    href="CarReserveInfo.jsp?no=<%=bean.getNo()%>"> <img alt=""
                        src="img/<%=bean.getImg()%>" width="300" height="220">
                </a>
                    <p>
                        차량명 :
                        <%=bean.getName()%>
                </td>
 
 
                <%
                    }
                %>
            
        </table>
        <hr color="red" size="3">
        <p>
            <font size="4" color="gray"> </font><br> <br> <br>
            <!-- 종류를 선택하고 검색을 하면 종류라는 데이터들 가지고 넘어가야하기 때문에 form형식을 사용한다. -->
        <form action="CarCategoryList.jsp" method="post">
            <font size="3" color="gray"> <b>차량 검색 하기</b>
            </font>&nbsp;&nbsp; <select name="category">
                <option value="1">소형</option>
                <option value="2">중형</option>
                <option value="3">대형</option>
            </select>&nbsp;&nbsp; <input type="submit" value="검색"> &nbsp;&nbsp;
            <button onclick="location.href='CarAllList.jsp'">전체 검색</button>
 
        </form>
    </center>
</body>
 
</html>
cs



Center.jsp (중간)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <center>
        <table width="1000">
            <tr height="600">
                <!-- 센터에 출력될 이미지를 추가 -->
                <td align="center"><img alt="" src="img/Main.jpg" height="470"
                    width="1000"></td>
            </tr>
        </table>
    </center>
</body>
</html>
cs



RentcarMain.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
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<body>
 
    <%
        String center = request.getParameter("center");
        //처음 실행시에는 center 값이 넘어오지 않기에 반드시  null처리를 해야한다.
        //처리를 하지않으면 에러가 발생될수 있다.
        if (center == null) {
            center = "Center.jsp"//디폴트 center값을 부여(첫 화면에는 center이 뜨도록 한다는 말)    
        }
                    
%>
    <center>
        <table width="1000">
 
            <!-- Top 부분 -->
            <tr height="140" align="center">
                <!-- include page를 사용하여서 main페이지에서 각 페이지가 호출될수 있도록 한다. -->
                <td align="center" width="1000"><jsp:include page="Top.jsp" /></td>
            </tr>
 
            <!-- Center 부분 -->
            <!-- Top랑,Bottom은 화면이 넘어가더라도 바뀌지 않지만 center은 계속 바뀌기 때문에 center값을 준다 -->
 
            <tr align="center">
                <td align="center" width="1000"><jsp:include page="<%=center%>" />
                </td>
            </tr>
 
            <!-- Bottom 부분 -->
            <tr height="140" align="center">
                <td align="center" width="1000"><jsp:include page="Bottom.jsp" /></td>
            </tr>
 
        </table>
    </center>
</body>
</html>
cs



Top.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
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<body>
 
    <!-- 세션을 이용한 로그인 처리 -->
    <!-- 세션으로 받아온 값은 오브젝트 타입이기 때문에 String 타입으로 컨버팅 한다. -->
 
    <%
        String id = (String) session.getAttribute("id");
 
        //로그인이 되어있지 않다면 id에 "GUEST"값을 준다
        if (id == null) {
            id = "GUEST";
        }
    %>
 
    <table width="1000" bordercolor="white">
        <tr height="70">
            <td colspan="4">
                <!-- 이미지를 불러오기위한 태그 작성 --> <img alt="" src="img/RENT.jpg"
                height="150" width="250">
            </td>
            <td align="center" width="200"><%=id%> 님 반갑습니다.</td>
        </tr>
 
        <tr height="50">
            <td align="center" width="200" bgcolor="pink">
                <!-- 글자를 누르면 화면이 넘어갈수 있도록 a태그를 걸어줌 --> <font color="white" size="5"><a
                    href="RentcarMain.jsp?center='CarReserveMain.jsp'"
                    style="text-decoration: none"> 예 약 하 기 </a></font>
            </td>
 
            <td align="center" width="200" bgcolor="pink">
                <!-- 글자를 누르면 화면이 넘어갈수 있도록 a태그를 걸어줌 --> <font color="white" size="5"><a
                    href="#" style="text-decoration: none"> 예 약 확 인</a></font>
            </td>
 
            <td align="center" width="200" bgcolor="pink">
                <!-- 글자를 누르면 화면이 넘어갈수 있도록 a태그를 걸어줌 --> <font color="white" size="5"><a
                    href="#" style="text-decoration: none"> 자 유 게 시 판 </a></font>
            </td>
 
            <td align="center" width="200" bgcolor="pink">
                <!-- 글자를 누르면 화면이 넘어갈수 있도록 a태그를 걸어줌 --> <font color="white" size="5"><a
                    href="#" style="text-decoration: none"> 이 벤 트 </a></font>
            </td>
 
            <td align="center" width="200" bgcolor="pink">
                <!-- 글자를 누르면 화면이 넘어갈수 있도록 a태그를 걸어줌 --> <font color="white" size="5"><a
                    href="#" style="text-decoration: none"> 고 객 센 터 </a></font>
            </td>
        </tr>
 
    </table>
</body>
</html>
cs




728x90
반응형
: