본문 바로가기

학원/스프링-학원

MVC_상품정보 관리 문제

728x90

 

컨트롤러 패키지


ItemController.java

 

 

서비스 패키지


ItemService.java - 인터페이스

 

 

ItemWriteSevice.java - 상품 작성

 

 

ItemContentService.java - 목록

 

 

DAO


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
public class ItemDao {
    DataSource dataSource;
    
    public ItemDao() {
        try {
            Context ctx = new InitialContext();
            dataSource = (DataSource) ctx.lookup("java:comp/env/jdbc/oracle");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    //목록 불러오는 메서드
    public ArrayList<ItemDto> list() {
        ArrayList<ItemDto> dtos = new ArrayList<ItemDto>();
        Connection con = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        
        try {
            con = dataSource.getConnection();
            String sql = "select name, price, description from test_item order by name";
            pstmt = con.prepareStatement(sql);
            rs = pstmt.executeQuery();
            
            while (rs.next()) {
                String name = rs.getString("name");
                int price = rs.getInt("price");
                String description = rs.getString("description");
                
                ItemDto dto = new ItemDto(name, price, description);
                dtos.add(dto);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if(rs != null)rs.close();
                if(pstmt != null)pstmt.close();
                if(con != null)con.close();
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
        return dtos;        
    }
    
    //글 작성하는 메서드
    public void itemWrite(String name, String price, String description) {
        Connection con = null;
        PreparedStatement pstmt = null;
        
        try {
            con = dataSource.getConnection();
            String sql = "insert into test_item (name, price, description) values (?,?,?)";
            pstmt = con.prepareStatement(sql);
            //ItemWriteService에서 넘어온 값들을 넣어줌
            pstmt.setString(1, name);
            pstmt.setInt(2, Integer.parseInt(price));
            pstmt.setString(3, description);
            pstmt.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if(pstmt != null)pstmt.close();
                if(con != null)con.close();
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
    }
}
cs

 

DTO


 

 


itemWrite.jsp - 상품등록 

 

 

writeResult.jsp - 결과 보기

 

 

content_view.jsp - 상품 목록

 

728x90

'학원 > 스프링-학원' 카테고리의 다른 글

스프링 JDBC  (0) 2022.05.19
MVC 로그인 문제 (JDBC 템플릿 사용 부분도 추가)  (0) 2022.05.19
스프링 MVC 게시판 - 가입, 수정, 삭제  (0) 2022.05.18
MVC 기초  (0) 2022.05.16
AOP(Aspect Oriented Programming)  (0) 2022.05.13