본문 바로가기
교육/Java&Spring

kosta 클라우드 네이티브 애플리케이션 개발 과정 day 32

by Renechoi 2023. 2. 7.

kosta 클라우드 네이티브 애플리케이션 개발 과정 day 32


 

mvc 패턴 1을 적용한 email 리스트를 만들어보자 

 

package email;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class EmaillistDao {

  private DBConnectionMgr pool;

  public EmaillistDao() {
    try {
      pool = DBConnectionMgr.getInstance();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  public ArrayList<EmaillistVo> getList() {

    // 0. import java.sql.*;
    Connection conn = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    ArrayList<EmaillistVo> list = new ArrayList<EmaillistVo>();

    try {
      conn = pool.getConnection();
      
      // 3. SQL문 준비 / 바인딩 / 실행
      String query = "select no, last_name, first_name, email "
                   + "from emaillist "
                   + "order by no desc" ;
      pstmt = conn.prepareStatement(query);
      
      rs = pstmt.executeQuery();
      // 4.결과처리
      while(rs.next()) {
        int no = rs.getInt("no");
        String lastName = rs.getString("last_name");
        String firstName = rs.getString("first_name");
        String email = rs.getString("email");
        
        EmaillistVo vo = new EmaillistVo(no, lastName, firstName, email);
        list.add(vo);
      }
      
      
    } catch (SQLException e) {
      
      System.out.println("error:" + e);
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      // 5. 자원정리
      try {
        if (pstmt != null) {
          pstmt.close();
        }
        if (conn != null) {
          pool.freeConnection(conn);
        }
      } catch (SQLException e) {
        System.out.println("error:" + e);
      }

    }

    return list;
  }
  
  
  public boolean insert(EmaillistVo vo) throws Exception {
    Connection conn = null;
    PreparedStatement pstmt = null;
    int count = 0 ;
    
      conn = pool.getConnection();
      
      String query ="insert into emaillist values (seq_emaillist_no.nextval, ?, ?, ?)";
      pstmt = conn.prepareStatement(query); 
      
      pstmt.setString(1, vo.getLast_name());
      pstmt.setString(2, vo.getFirst_name());
      pstmt.setString(3, vo.getEmail());
    
      count = pstmt.executeUpdate();
      
      System.out.println(count + "건 등록");
      

    return (count>0)?true:false;
  }
  
  public boolean delete(EmaillistVo vo) {
    Connection conn = null;
    PreparedStatement pstmt = null;
    int count = 0 ;
    
    try {
      conn = pool.getConnection();
      
      String query ="delete from emaillist where no = ?";
      pstmt = conn.prepareStatement(query); 
      
      pstmt.setInt(1, vo.getNo());
      
      count = pstmt.executeUpdate();
      
      System.out.println(count + "건 삭제");
      
    } catch (SQLException e) {
      System.out.println("error:" + e);
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      try {
        if (pstmt != null) pstmt.close();
        if (conn != null) pool.freeConnection(conn);
      } catch (SQLException e) {
        System.out.println("error:" + e);
      }
    }
    return (count>0)?true:false;
  }
  

}

 

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="email.EmaillistDao"%>
<%@ page import="email.EmaillistVo"%>

<% 
   request.setCharacterEncoding("UTF-8");
   String lastName = request.getParameter("ln");
   String firstName = request.getParameter("fn");
   String email = request.getParameter("email");
   
   EmaillistVo vo = new EmaillistVo(lastName, firstName, email);
   
   EmaillistDao dao = new EmaillistDao();
   if(dao.insert(vo)){
     response.sendRedirect("list.jsp");  
  }else{
%>
<script type="text/javascript">
  alert("데이터 입력 실패");
  history.go(-1);
</script>

<%    
  }
%>

 

 

<%@ page language="java" 
         contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>
<%@ page import="email.EmaillistDao"%>
<%@ page import="email.EmaillistVo"%>
<%@ page import="java.util.*"%>

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Email 목록 </title>
</head>
<body>
   <h1>메일 리스트에 가입되었습니다.</h1>
   <p>입력한 정보 내역입니다.</p>
   <!-- 메일정보 리스트 -->
   <%-- list에서 하나씩 빼서 테이블를 채운다--%>
  <%
    email.EmaillistDao dao = new email.EmaillistDao();
      ArrayList<email.EmaillistVo> list = dao.getList();
      
      for(email.EmaillistVo vo : list) {
  %>
 
  <table border="1" cellpadding="5" cellspacing="2">
      <tr>
         <td align=right width="110"> Last name:</td>
         <td width="170"><a href="delete.jsp?no=<%=vo.getNo()%>"><%=vo.getLast_name()%></a></td>
      </tr>
      <tr>
         <td align=right >First name:</td>
         <td><%=vo.getFirst_name()%></td>
      </tr>
      <tr>
         <td align=right>Email address: </td>
         <td><%=vo.getEmail()%></td>
      </tr>
   </table>
   <br>
  <%
  }
      
  %>
  
   <br>
   <p>
      <a href="form.jsp">추가메일 등록</a>
   </p>
   <br>
</body>
</html>

 

 

 

반응형