본문 바로가기

Side Project

#08. [Side Project 1] JSP 로그인, 게시판 만들기 (8) :: 글 검색 기능

 

■ BbsDao.java (getBbsList())

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
public List<BbsDto> getBbsList(String choice, String searchWord) {
        
        String sql = " SELECT SEQ, ID, REF, STEP, DEPTH, "
                        + " TITLE, CONTENT, WDATE, "
                        + " DEL, READCOUNT "
                        + " FROM BBS ";                    
                        
        String sqlWord = "";
        if(choice.equals("title")) {
            sqlWord = " WHERE TITLE LIKE '%" + searchWord.trim() + "%' AND DEL=0 ";
        }else if(choice.equals("writer")) {
            sqlWord = " WHERE ID='" + searchWord.trim() + "'"
        }else if(choice.equals("content")) {
            sqlWord = " WHERE CONTENT LIKE '%" + searchWord.trim() + "%' ";
        } 
        sql = sql + sqlWord;
                
        sql += " ORDER BY REF DESC, STEP ASC ";
        
        Connection conn = null;
        PreparedStatement psmt = null;
        ResultSet rs = null;
        
        List<BbsDto> list = new ArrayList<BbsDto>();
        
        try {
            conn = DBConnection.getConnection();
            System.out.println("1/6 getBbsList success");
            
            psmt = conn.prepareStatement(sql);
            System.out.println("2/6 getBbsList success");
            
            rs = psmt.executeQuery();
            System.out.println("3/6 getBbsList success");
            
            while(rs.next()) {
                int i = 1;
                BbsDto dto = new BbsDto(rs.getInt(i++), 
                                        rs.getString(i++), 
                                        rs.getInt(i++), 
                                        rs.getInt(i++), 
                                        rs.getInt(i++), 
                                        rs.getString(i++), 
                                        rs.getString(i++), 
                                        rs.getString(i++), 
                                        rs.getInt(i++), 
                                        rs.getInt(i++));                
                list.add(dto);
            }
            System.out.println("4/6 getBbsList success");
            
        } catch (Exception e) {            
            e.printStackTrace();
        } finally {
            DBClose.close(psmt, conn, rs);            
        }
        return list;
    }
    
cs

 

 

 

■ BbsDao.jsp (getAllBbs())

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
public int getAllBbs(String choice, String searchWord) {
        String sql = " SELECT COUNT(*) FROM BBS ";
        
        String sqlWord = "";
        if(choice.equals("title")) {
            sqlWord = " WHERE TITLE LIKE '%" + searchWord.trim() + "%' ";
        }else if(choice.equals("writer"&& !searchWord.equals("")) {
            sqlWord = " WHERE ID='" + searchWord.trim() + "'";
        }else if(choice.equals("content")) {
            sqlWord = " WHERE CONTENT LIKE '%" + searchWord.trim() + "%' ";
        }
        sql += sqlWord;
        
        Connection conn = null;
        PreparedStatement psmt = null;
        ResultSet rs = null;
        int len = 0;
                
        try {
            conn = DBConnection.getConnection();
            psmt = conn.prepareStatement(sql);
            rs = psmt.executeQuery();
            if(rs.next()) {
                len = rs.getInt(1);
            }            
        } catch (Exception e) {
            System.out.println("getAllBbs fail");
            e.printStackTrace();
        } finally {
            DBClose.close(psmt, conn, rs);            
        }
        return len;        
    }
cs

 

■ bbslist.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
<select id="choice"> <!-- 높이:20 중간맞춤 -->
    <option value="sel">선택</option>
    <option value="title">제목</option>
    <option value="writer">작성자</option>
    <option value="content">내용</option>
</select>
 
<input type="text" id="search" value="<%=searchWord %>">
 
<button onclick="searchBbs()">검색</button>
 
</div>
 
<script type="text/javascript">
function searchBbs() {
    var choice = document.getElementById("choice").value;
    var word = document.getElementById("search").value;
//    alert(choice);
//    alert(word);
 
    /* if(word == ""){
        document.getElementById("search").value = "";
        document.getElementById("choice").value = 'sel';
    } */
    
    location.href = "bbslist.jsp?searchWord=" + word + "&choice=" + choice;
}
 
cs
function searchBbs() {
	var choice = document.getElementById("choice").value;
	var word = document.getElementById("search").value;
//	alert(choice);
//	alert(word);

	/* if(word == ""){
		document.getElementById("search").value = "";
		document.getElementById("choice").value = 'sel';
	} */
	
	location.href = "bbslist.jsp?searchWord=" + word + "&choice=" + choice;
}