19.05.15 jsp 쇼핑몰-차량 예약하기(동영상 59강)

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

쇼핑몰 차량 예약하기를 누르면 <차량 종류보기> 창이 뜨도록 한다.


상위 3종류의 모델은 최신순으로 보여주게 한다.


<소형,중형,대형> 분류 검색이 되도록 한다.


<전체검색>이 되도록 한다.



img.zip


CarReserveMain.jsp (상위 3종류의 모델을 최신순으로 보여줌)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<%@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>
    <!-- 데이터베이스에 연결하여 최신순 자동차 3대만 뿌려주는 데이터를 가져옴 -->
 
<%
    RentcarDAO rdao = new RentcarDAO();
    //벡터를 이용하여 자동차데이터를 저장함
    Vector<CarListBean> v = rdao.getSelectCar();
 
%>
 
</body>
</html>
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
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();
        }
 
    }
 
}
 
cs



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
package db;
 
public class CarListBean { //빈 클래스 생성함
    
    public String getCompany() {
        return company;
    }
    public void setCompany(String company) {
        this.company = company;
    }
    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;
    }
    
}
 
cs


728x90
반응형
: