MemoEditServlet
package my.memo;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class MemoEditFormServlet
*/
@WebServlet({ "/MemoEditFormServlet", "/MemoEditForm" })
public class MemoEditFormServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private MemoDAO dao = new MemoDAO();
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
res.setContentType("text/html; charset=UTF-8");
PrintWriter out=res.getWriter();
//1.수정할 글번호 받기
String idx=req.getParameter("idx");
System.out.println(idx);
//2.유효성 체크 - MemoList로 돌려보내기
if(idx==null||idx.trim().isEmpty()) {
res.sendRedirect("MemoList");
return;
}
//3.MemoDao의 selectMemo(idx)==>반환타입 MemoVO유형
MemoDAO dao= new MemoDAO();
MemoVO m=null;
try {
m = dao.selectMemo(Integer.parseInt(idx));
if(m==null) {
out.println("<script>");
out.println("alert('존재하지 않는 글이에요')");
out.println("history.go(-1)");
out.println("</script>");
return;
}
} catch (Exception e) {
out.println("<h3 style='color:red'>서버 에러: "+e+"</h3>");
e.printStackTrace();
}
//4.input value값으로 받아온 메모내용 값을 출력하기
out.println("<link rel='stylesheet' type='text/css' href='CSS/memo.css'>");
out.println("<script src='js/memo.js'></script>");
out.println("<div id='wrap'>");
out.println("<form name='frm' action='MemoEdit' method='POST'>");
out.println("<input type='hidden' name='idx' value='"+m.getIdx()+"'>");
out.println("<table border=\"1\">");
out.println("<tr><th colspan=\"2\"><h2>::한줄 메모장 수정::</h2></th></tr>");
out.println("<tr><td width=\"20%\"><b>작성자</b></td>");
out.println("<td width=\"80%\"><input type=\"text\" name=\"name\" value='"+m.getName()+"' placeholder=\"Name\" class=\"writer\"></td></tr>");
out.println("<tr><td width=\"20%\"><b>메모내용</b></td><td width=\"80%\">");
out.println("<input type=\"text\" name=\"msg\" value='"+m.getMsg()+"' placeholder=\"Message\" class=\"msg\"></td></tr>");
out.println("<tr><td colspan=\"2\" style=\"text-align:center\">");
out.println("<button type=\"button\" onclick=\"check()\">글수정하기</button>");
out.println("<button type=\"reset\">다시쓰기</button></td></tr>");
out.println("</table>");
out.println("</form>");
out.println("</div>");
out.close();
}
}
MemoDAO
package my.memo;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import jdbc.util.DBUtil;
public class MemoDAO {
Connection con;
PreparedStatement ps;
ResultSet rs;
public int insertMemo(MemoVO memo) throws SQLException{
try {
con=DBUtil.getCon();
//시퀀스명 : memo_seq
String sql ="INSERT INTO memo(idx,name,msg,wdate)"
+" VALUES(memo_seq.nextval, ?,?,sysdate)";
ps = con.prepareStatement(sql);
String name=memo.getName();
String msg=memo.getMsg();
ps.setString(1, name);
ps.setString(2, msg);
int n = ps.executeUpdate();
return n;
} finally {
close();
}
}
//select문 where절
public MemoVO selectMemo(int idx) throws SQLException{
try {
con=DBUtil.getCon();
String sql="SELECT * FROM MEMO WHERE IDX=?";
ps=con.prepareStatement(sql);
ps.setInt(1, idx);
rs=ps.executeQuery();
List<MemoVO> arr=makeList(rs);
if(arr!=null||arr.size()==1) {
return arr.get(0);
}
return null;
}finally {
close();
}
}
//최근 쓴 글 순으로 모든 메모글 가져오기
public List<MemoVO> listMemo() throws SQLException{
try {
con=DBUtil.getCon();
String sql = "SELECT * FROM MEMO";
ps=con.prepareStatement(sql);
rs = ps.executeQuery();
List<MemoVO> arr=makeList(rs);
return arr;
}finally {
close();
}
}
public List<MemoVO> makeList(ResultSet rs) throws SQLException{
List<MemoVO> arr=new ArrayList<>();
//idx,msg,name,date
while(rs.next()) {
int idx = rs.getInt("idx");
String msg = rs.getString("msg");
String name = rs.getString("name");
Date date = rs.getDate("wdate");
MemoVO m = new MemoVO(idx, name, msg, date);
arr.add(m);
}
return arr;
}
public int deleteMemo(int idx) throws SQLException{
try {
con=DBUtil.getCon();
String sql="DELETE FROM MEMO WHERE IDX=?";
ps=con.prepareStatement(sql);
ps.setInt(1, idx);
int n=ps.executeUpdate();
return n;
}finally {
close();
}
}
public int updateMemo(MemoVO memo) throws SQLException{
try {
con=DBUtil.getCon();
String sql="UPDATE MEMO set msg=?,name=? where idx=?";
ps=con.prepareStatement(sql);
ps.setInt(3, memo.getIdx());
ps.setString(2, memo.getName());
ps.setString(1, memo.getMsg());
System.out.println(memo.getMsg());
int n = ps.executeUpdate();
return n;
}finally {
close();
}
}
public void close() {
try {
if(rs!=null) rs.close();
if(ps!=null) ps.close();
if(con!=null) con.close();
}catch(Exception e){
System.out.println(e);
}
}
}
MemoEdit
package my.memo;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class MemoEdit
*/
@WebServlet("/MemoEdit")
public class MemoEdit extends HttpServlet {
private static final long serialVersionUID = 1L;
private MemoDAO dao=new MemoDAO();
protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
//0. post방식일때 한글처리
req.setCharacterEncoding("UTF-8");
res.setContentType("text/html;charset=UTF-8");
PrintWriter out = res.getWriter();
//1.글번호, 작성자, 글내용값 받기
String idx = req.getParameter("idx");//hidden data
String name = req.getParameter("name");
String msg = req.getParameter("msg");
System.out.println(idx+name+msg);
//2.유효성 체크 -> MemoList로 돌려보내기
if(idx==null||name==null||idx.trim().isEmpty()||name.trim().isEmpty()) {
res.sendRedirect("MemoList");
return;
}
//3. MemoDAO 생성해서 updateMemo(MemoVO객체) 호출
MemoVO memo = new MemoVO(Integer.parseInt(idx),name,msg,null);
try {
int n=dao.updateMemo(memo);
String str =(n>0)? "수정 성공" : "수정 실패";
out.println("<script>");
out.println("alert('"+str+"');");
out.println("location.href='MemoList'");
out.println("</script>");
}catch(SQLException e) {
}
//4. 그 실행 결과에 따라서 메시지 보여주고 페이지 이동(MemoList)
}
}
ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ
팀프로젝트 진행
'개발자 > 국비지원 SW' 카테고리의 다른 글
국비지원 56일차 - 팀프로젝트 (0) | 2020.07.02 |
---|---|
국비지원 55일차 - Bootstrap Layout, Color, Card, Table, dropdown (0) | 2020.07.01 |
국비지원 53일차 - JS 자동차견적, 갤러리, Bootstrap/jQuery 다운로드방식, CDN방식 (0) | 2020.06.29 |
국비지원 52일차 - HTTP, POST방식, memo예시 (0) | 2020.06.26 |
국비지원 51일차 - JavaScript String객체, 함수, 정규식(RegExp)객체 (1) | 2020.06.25 |