<%@ page language="java" contentType="text/html; charset=euc-kr" %>
<%@ page import = "java.sql.*, java.util.*" %>
<% request.setCharacterEncoding("euc-kr"); %>
<HTML>
<HEAD>
<TITLE> 게시판 </TITLE>
<SCRIPT language="JavaScript">
function Check()
{
if (Form.keyword.value.length < 1) {
alert("검색어를 입력하세요.");
Form.keyword.focus();
return false;
}
}
</SCRIPT>
<META http-equiv="Content-Type" content="text/html; charset=euc-kr">
<style type='text/css'>
<!--
a:link {font-family:"";color:black;text-decoration:none;}
a:visited {font-family:"";color:black;text-decoration:none;}
a:hover {font-family:"";color:black;text-decoration:underline;}
-->
</style>
</HEAD>
<BODY>
<%
String key = request.getParameter("key");
String keyword = request.getParameter("keyword");
String pageNum = request.getParameter("pageNum");
if(pageNum == null){
pageNum = "1";
}
int listSize = 5;
int currentPage = Integer.parseInt(pageNum);
int startRow = (currentPage - 1) * listSize + 1;
int endRow = currentPage * listSize;
int lastRow = 0;
int i = 0;
String strSQL = "";
String driverName = "com.mysql.jdbc.Driver";
String dbURL = "jdbc:mysql://localhost:3306/univdb";
Class.forName(driverName);
Connection conn = DriverManager.getConnection(dbURL, "root", "dongyang");
Statement stmt = conn.createStatement();
ResultSet rs = null;
if (key==null || keyword==null){
strSQL = "SELECT count(*) FROM tblboard";
}else{
strSQL = "SELECT count(*) FROM tblboard WHERE " + key + " like '%" + keyword + "%'";
}
rs = stmt.executeQuery(strSQL);
rs.next();
lastRow = rs.getInt(1);
%>
<center><font size='3'><b> 게시판 </b></font></TD>
<TABLE border='0' width='600' cellpadding='0' cellspacing='0'>
<TR>
<TD><hr size='1' noshade>
</TD>
</TR>
</TABLE>
<TABLE border='0' cellspacing=1 cellpadding=2 width='600'>
<TR bgcolor='cccccc'>
<TD><font size=2><center><b>번호</b></center></font></TD>
<TD><font size=2><center><b>글 제목</b></center></font></TD>
<TD><font size=2><center><b>작성자</b></center></font></TD>
<TD><font size=2><center><b>작성일</b></center></font></TD>
<TD><font size=2><center><b>조회</b></center></font></TD>
</TR>
<%
if(lastRow > 0) {
if(key==null || keyword==null){
strSQL = "SELECT * FROM tblboard WHERE num BETWEEN " + startRow + " and " + endRow + " ORDER BY num DESC";
rs = stmt.executeQuery(strSQL);
} else {
strSQL = "SELECT * FROM tblboard WHERE " + key + " like '%" + keyword + "%' ORDER BY num DESC";
rs = stmt.executeQuery(strSQL);
}
for (i = 1; i < listSize; i++){
while(rs.next()){
int listnum = rs.getInt("num");
String name = rs.getString("name");
String email = rs.getString("email");
String title = rs.getString("title");
String writedate = rs.getString("writedate");
int readcount = rs.getInt("readcount");
%>
<TR bgcolor='ededed'>
<TD align=center><font size=2 color='black'><%=listnum %></font></TD>
<TD align=left>
<a href="write11_output.jsp?num=<%=listnum %>">
<font size=2 color="black"><%=title %></font></a>
</TD>
<TD align=center>
<a href="<%=email %>">
<font size=2 color="black"><%=name %></font></a>
</TD>
<TD align=center><font size=2><%=writedate %></font>
</TD>
<TD align=center><font size=2><%=readcount %></font>
</TR>
<%
}
}
%>
</TABLE>
<TABLE border='0' width='600' cellpadding='0' cellspacing='0'>
<TR>
<TD><hr size='1' noshade>
</TD>
</TR>
</TABLE>
<%
}
if(lastRow > 0) {
int setPage = 1;
int lastPage = 0;
if(lastRow % listSize == 0)
lastPage = lastRow / listSize;
else
lastPage = lastRow / listSize + 1;
if(currentPage > 1) {
%>
<a href="listboard11.jsp?pageNum=<%=currentPage-1%>">[이전]</a>
<%
}
for(i=setPage; i<=lastPage; i++) {
if (i == Integer.parseInt(pageNum)){
%>
[<%=i%>]
<%
}else{
%>
<a href="listboard11.jsp?pageNum=<%=i%>">[<%=i%>]</a>
<%
}
}
if(lastPage > currentPage) {
%>
<a href="listboard11.jsp?pageNum=<%=currentPage+1%>">[다음]</a>
<%
}
}
%>
<TABLE border='0' width='600' cellpadding='0' cellspacing='0'>
<TR>
<TD><hr size='1' noshade>
</TD>
</TR>
</TABLE>
<TABLE border=0 width=600>
<TR>
<TD align='center'>
<TABLE border='0' cellpadding='0' cellspacing='0'>
<FORM Name='Form' Method='POST' Action='listboard11.jsp' OnSubmit='return Check()'>
<input type='hidden' name='search' value='1'>
<TR>
<TD align='right'>
<select name='key' style="background-color:cccccc;">
<option value='title' selected><font size='2'>
글제목</font></option>
<option value='contents'><font size='2'>
글내용</font></option>
<option value='name'><font size='2'>
작성자</font></option>
</select>
</TD>
<TD align='left'>
<input type='text' name='keyword' value='' size='20' maxlength='30'>
<input type='submit' value='검색'>
</td>
</TR>
</FORM>
</TABLE>
</TD>
<TD align='right'>
<a href='write11.jsp'>[등록]</a>
</TD>
</TR>
</TABLE>
</BODY>
</HTML>
jsp공부중인 학생인데요
게시판을 최근글 순서대로 하고싶은데요
1번에 2번에
5번게시물 10
4번게시물 9
3번게시풀 8
2번게시물 7
1번게시물 6
1 2 3 4 5
현재이렇게나오거든요 ㅜㅜ 근데 이거를 최근글 순서대로
12 7 2
11 6 1
10 5
9 4
8 3
이렇게 나오게할수는있나요ㅠㅠ
안녕하세요~^^
올려주신 코드를 확인해보니
키워드 검색이 아닐 경우 아래의 SQL문으로 게시글을 불러오고 있는 듯합니다.
strSQL = "SELECT * FROM tblboard WHERE num BETWEEN " + startRow + " and " + endRow + " ORDER BY num DESC";
startRow 변수와 endRow 변수는 현재 페이징 번호에 따라 설정되기 때문에
올려주신 내용처럼 표시됩니다.
MySQL을 사용 중이시라면 쿼리에 LIMIT를 활용해보시겠어요?
strSQL = "SELECT * FROM tblboard ORDER BY num DESC LIMIT "+startRow+", "+endRow+"";
구글에 jsp 페이징 키워드로 검색해보시면
관련 자료가 많이 있으니 참고해보시는 것도 좋을 듯합니다.
고맙습니다.