오늘은 방명록입니다. ---박태영님이 제공한 소스입니다. ::: MYSQL에서 테이블생성::::::::::::::::::::::::::: create table guestbook( seg int(10) auto_increment primary key, name varchar(20), email varchar(50), home varchar(50), passwd varchar(11), content text not null, now_date varchar(11), ip varchar(17)); ); ※참고: 방명록소스 그대로 사용하실경우는 sql int(10) auto_increment primary key, 로 변경해 주세요. /////write.html//////////////////////////////////

글쓰기

이름
이메일
홈페이지
제목
내용
비밀번호
   
/////db.jsp////////////////////////////////// <% String drivers = "org.gjt.mm.mysql.Driver"; String dbhost = "jdbc:mysql://localhost:3306/jsptestdb"; String dbuser= "root"; String dbpassword = ""; try{ Class.forName(drivers); }catch(ClassNotFoundException e){out.println(e);} %> /////delete.jsp////////////////////////////////// <%@ include file="db.jsp"%> <%@ page language="java" import="java.sql.*,java.io.*,java.text.*,java.util.*" contentType="text/html;charset=ksc5601"%> <% Connection con; Statement stmt; %> <% try{ con = DriverManager.getConnection(dbhost,dbuser,dbpassword); stmt = con.createStatement(); String sql = request.getParameter("sql"); stmt.executeUpdate("delete from guestbook where sql="+sql); stmt.close(); con.close(); response.sendRedirect("list.jsp"); } catch(SQLException se) { out.println(se.getMessage()); } %> /////list.jsp////////////////////////////////// <%@ include file="db.jsp"%> <%@ page language="java" import="java.sql.*,java.io.*,java.text.*,java.util.*" contentType="text/html;charset=ksc5601"%> <% Connection con; Statement stmt; ResultSet rs,rs1; int back,front; %>
<%@ include file="top.html"%>
<% int pagesize = 10; //한페이지당 표시 개수 int totalpage = 0 ; int cntpage = 0; //현재 페이지 int rate = 0; //나머지 int maxcount = 0; //레코드 개수 try{ con = DriverManager.getConnection(dbhost, dbuser, dbpassword); stmt = con.createStatement(); rs1 = stmt.executeQuery("select count(*) from guestbook"); while(rs1.next()) {maxcount = rs1.getInt(1);} rate = maxcount % pagesize; totalpage = maxcount / pagesize; if(rate > 0) totalpage += 1; String str = request.getParameter("page"); if(str == null) str = "1"; cntpage = Integer.parseInt(str); rs = stmt.executeQuery("select sql, name, home,now_date,content from guestbook order by sql desc"); int i=0; int j=0; int pagecnt = (cntpage * pagesize) - pagesize; while(rs.next()) { if((i=pagecnt)) { String sql=rs.getString("sql"); String name=rs.getString("name"); String home=rs.getString("home"); String day=rs.getString("now_date"); String content=rs.getString("content"); %> <% i++; } j++; } rs.close(); stmt.close(); con.close(); }catch(SQLException se) { out.println(se.getMessage()); } %>
<%= sql%> <%= name%> <%= home%> <%= day%> >삭제
<%=content%>
   
<% if(cntpage > 1) { //첫번째 페이지만을 제외하고 이전페이지보기(◀)를 출력 front = cntpage-1; %> > ◀ <% } //End if for(int a=1 ; a<=totalpage ; a++) { if(a == cntpage) { %> [ <%=a%> ] <% } else { %> [><%=a%>] <% } //End else } //End for if(cntpage < totalpage) { back = cntpage+1; %> > ▶ <% } //End if %>
/////post.jsp////////////////////////////////// <%@ include file="db.jsp"%> <%@ page language="java" import="java.sql.*,java.io.*,java.text.*,java.util.*" contentType="text/html;charset=ksc5601"%> <%! Connection con; Statement stmt; %> <% String sql=request.getParameter("sql"); String name=request.getParameter("name"); String home=request.getParameter("home"); String content=request.getParameter("content"); java.util.Date now=new java.util.Date(); SimpleDateFormat date= new SimpleDateFormat("yyyy/MM/dd"); String regdate = date.format(now); try{ con = DriverManager.getConnection(dbhost,dbuser,dbpassword); stmt = con.createStatement(); stmt.executeUpdate("insert into guestbook(sql,name,home,content,now_date) values ('"+sql +"','"+name+"','"+home+"','"+content+"',now())"); stmt.close(); con.close(); response.sendRedirect("list.jsp"); } catch(SQLException se) { out.println(se.getMessage()); } %> /////top.html//////////////////////////////////

방 명 록