편집 기록

편집 기록
  • 프로필 엽토군님의 편집
    날짜2019.07.29

    JSP MYSQL 연동 관련


    .<%
    
        ArrayList<RecipeDTO> recipeList = new ArrayList<RecipeDTO>();
        recipeList = new RecipeDAO().getList(recipeDivide, searchType, search, pageNumber);
        if(recipeList != null){
            for(int i=0; i< recipeList.size(); i++){
                if(i==5) break;                 
                RecipeDTO recipe = recipeList.get(i);
       .%>
    
    
                <div class="col-4 col-6-medium col-12-small">
                    <section>
                       <a href="#" class="image featured"><img src="images/pic01.jpg" alt="" /></a>
                            <header>
                                    <h3><%=recipe.getFoodTitle()%></h3>
                                        </header>
                                            <h4>재료리스트</h4>
                                            <ul>
                                            <li>
                                            <span><%=recipe.getIngredients()%></span>
                                            <em>400g</em>
                                            </li>
                                            </ul>
                                            <h4>조리 방법</h4>
                                            <p>
                                            <%=recipe.getFoodContent()%>
                                            </p>
                                        </section>
                                    </div>
    <%
            }
        }
    %>
    

    JSP와 자바로 웹 페이지 제작중인데요 recipeDAO 에 getList 함수를 통해서 MYSQL 에 저장돼있는 데이터를 가져와서 출력하려고 하는데 아에 이미지까지 빈 화면으로 뜹니다. 디버깅을 해보니까 recipeList.size() 가 0으로 돼서 for문이 아에 안도는것 같더라구요. 왜 size가 0으로 되는걸까요? 아래 코드는 RecipeDAO 안에 포함된 getList함수 입니다. 도움 주시면 감사하겠습니다ㅠ

    public ArrayList<RecipeDTO> getList (String recipeDivide, String searchType, String search, int pageNumber){
    
            if(recipeDivide.equals("전체")) {
                recipeDivide = "";
            }
    
            ArrayList<RecipeDTO> recipeList = null;
            String SQL = "";
            PreparedStatement pstmt = null;
            Connection conn = null;
            ResultSet rs = null;
            try {
                if(searchType.contentEquals("최신순")) {
                    SQL = "SELECT * FROM recipe WHERE recipeDivide LIKE ? AND CONCAT(foodTitle, foodContent, ingredients, classification, tag) LIKE ? ORDER BY recipeID DESC LIMIT " + pageNumber * 5 + ", " + pageNumber * 5 + 6;
                } else if (searchType.contentEquals("추천순")) {
                    SQL = "SELECT * FROM recipe WHERE recipeDivide LIKE ? AND CONCAT(foodTitle, foodContent, ingredients, classification, tag) LIKE ? ORDER BY likeCount DESC LIMIT " + pageNumber * 5 + ", " + pageNumber * 5 + 6;
                }
                conn = DatabaseUtil.getConnection();
                pstmt = conn.prepareStatement(SQL);
                pstmt.setString(1,  "%" + recipeDivide + "%");
                pstmt.setString(2,  "%" + search + "%");
                rs = pstmt.executeQuery();
                recipeList = new ArrayList<RecipeDTO>();
                while(rs.next()) {
                    RecipeDTO recipe = new RecipeDTO(
                        rs.getInt(1),
                        rs.getString(2),
                        rs.getString(3),
                        rs.getString(4),
                        rs.getString(5),
                        rs.getString(6),
                        rs.getString(7),
                        rs.getInt(8),
                        rs.getInt(9),
                        rs.getString(10)
                    );
                    recipeList.add(recipe);
    
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {if(conn!=null) conn.close();} catch(Exception e) { e.printStackTrace();}
                try {if(pstmt!=null) pstmt.close();} catch(Exception e) { e.printStackTrace();}
                try {if(rs!=null) rs.close();} catch(Exception e) { e.printStackTrace();}
            }
            return recipeList;
        }
    
  • 프로필 알 수 없는 사용자님의 편집
    날짜2019.07.29

    JSP MYSQL 연동 관련


    .<%

    ArrayList<RecipeDTO> recipeList = new ArrayList<RecipeDTO>();
    recipeList = new RecipeDAO().getList(recipeDivide, searchType, search, pageNumber);
    if(recipeList != null){
        for(int i=0; i< recipeList.size(); i++){
            if(i==5) break;                 
            RecipeDTO recipe = recipeList.get(i);
    

    .%>

            <div class="col-4 col-6-medium col-12-small">
                <section>
                   <a href="#" class="image featured"><img src="images/pic01.jpg" alt="" /></a>
                        <header>
                                <h3><%=recipe.getFoodTitle()%></h3>
                                    </header>
                                        <h4>재료리스트</h4>
                                        <ul>
                                        <li>
                                        <span><%=recipe.getIngredients()%></span>
                                        <em>400g</em>
                                        </li>
                                        </ul>
                                        <h4>조리 방법</h4>
                                        <p>
                                        <%=recipe.getFoodContent()%>
                                        </p>
                                    </section>
                                </div>
    

    <% } } %>

    JSP와 자바로 웹 페이지 제작중인데요 recipeDAO 에 getList 함수를 통해서 MYSQL 에 저장돼있는 데이터를 가져와서 출력하려고 하는데 아에 이미지까지 빈 화면으로 뜹니다. 디버깅을 해보니까 recipeList.size() 가 0으로 돼서 for문이 아에 안도는것 같더라구요. 왜 size가 0으로 되는걸까요? 아래 코드는 RecipeDAO 안에 포함된 getList함수 입니다. 도움 주시면 감사하겠습니다ㅠ

    public ArrayList getList (String recipeDivide, String searchType, String search, int pageNumber){

        if(recipeDivide.equals("전체")) {
            recipeDivide = "";
        }
    
        ArrayList<RecipeDTO> recipeList = null;
        String SQL = "";
        PreparedStatement pstmt = null;
        Connection conn = null;
        ResultSet rs = null;
        try {
            if(searchType.contentEquals("최신순")) {
                SQL = "SELECT * FROM recipe WHERE recipeDivide LIKE ? AND CONCAT(foodTitle, foodContent, ingredients, classification, tag) LIKE ? ORDER BY recipeID DESC LIMIT " + pageNumber * 5 + ", " + pageNumber * 5 + 6;
            } else if (searchType.contentEquals("추천순")) {
                SQL = "SELECT * FROM recipe WHERE recipeDivide LIKE ? AND CONCAT(foodTitle, foodContent, ingredients, classification, tag) LIKE ? ORDER BY likeCount DESC LIMIT " + pageNumber * 5 + ", " + pageNumber * 5 + 6;
            }
            conn = DatabaseUtil.getConnection();
            pstmt = conn.prepareStatement(SQL);
            pstmt.setString(1,  "%" + recipeDivide + "%");
            pstmt.setString(2,  "%" + search + "%");
            rs = pstmt.executeQuery();
            recipeList = new ArrayList<RecipeDTO>();
            while(rs.next()) {
                RecipeDTO recipe = new RecipeDTO(
                    rs.getInt(1),
                    rs.getString(2),
                    rs.getString(3),
                    rs.getString(4),
                    rs.getString(5),
                    rs.getString(6),
                    rs.getString(7),
                    rs.getInt(8),
                    rs.getInt(9),
                    rs.getString(10)
                );
                recipeList.add(recipe);
    
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {if(conn!=null) conn.close();} catch(Exception e) { e.printStackTrace();}
            try {if(pstmt!=null) pstmt.close();} catch(Exception e) { e.printStackTrace();}
            try {if(rs!=null) rs.close();} catch(Exception e) { e.printStackTrace();}
        }
        return recipeList;
    }