고인돌개발자 2021. 8. 6. 14:26

▶ DB connection bean 

더보기
package comm;

import java.sql.Connection;
import java.sql.DriverManager;

public class DbConn {

	
	String driver = "oracle.jdbc.driver.OracleDriver";
	String dbURL="jdbc:oracle:thin:@localhost:1521:xe";
	String user_id="scott";        
	String user_pw="tiger";
	String qry="";

	private Connection conn = null; // DB 에 connection 된 객체를 저장 
	
	public Connection getConn(){
		
		try{
			/* Driver Loading */
		    Class.forName(driver);
		    conn = DriverManager.getConnection(dbURL, user_id, user_pw);
		    
		    System.out.println("OK Connection");
			
		}catch (Exception e) {
			// TODO: handle exception
		}
		
		return conn;
	}
	
	
}

 People 등록 컨트롤서 ( RegistPeople.java )   |   등록 서비스 ( RegistService.java )

더보기

RegistPeople.java

package controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import service.PeopleService;
import service.RegistService;
import vo.People;

/**
 * Servlet implementation class RegistPeople
 */
@WebServlet("/RegistPeople")
public class RegistPeople extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
   
    public RegistPeople() {
        super();
        // TODO Auto-generated constructor stub
    }

	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		/* ID , Name, Age 를 받는 부분 */
		request.setCharacterEncoding("UTF-8");
    	
	    String strID 	= request.getParameter("input_id");
	    String strName 	= request.getParameter("input_name");
	    String strAge	= request.getParameter("input_age");
		
		/* 해당 인자값을 던져서 Insert 시키는 메서드 Call */
	    RegistService registservice = new RegistService();
	    int intResult = registservice.doInsert(strID, strName, strAge);
	    
		/* 작업이 끝난 후 가야할 위치 지정 */
		List<People> list = new ArrayList<>();
	    
	    PeopleService peopleService = new PeopleService();
		
		list = peopleService.doSelect();
		// 리스트의 값을 Attribute 를 통해 주고 받는다.
		request.setAttribute("people", list);
		
		/* forward 방식 
		 * List 의 값을 Attribute 를 통해 전달한다. 
		 * */		
		 RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/view/list.jsp");
		 dispatcher.forward(request, response);
		 		 
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request,response);
	}

}

 

RegistService.java

package service;

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

import comm.DbConn;
import vo.People;

public class RegistService {
	
	
	public int doInsert(String strID, String strName, String strAge){
	
		int intI =0;
		Connection conn = null; // DB 에 connection 된 객체를 저장 
	    PreparedStatement ps = null;  // connection 객체에 실행문을 던지는 역할(창구)
	    ResultSet rs = null;     // select 결과값을 받아옮
	    String qry="";
		DbConn dbConn = new DbConn();	    
		conn = dbConn.getConn();
				
		 try{ 
			   
			 /* Result Set , Print */	
				qry = " Insert into people(id, name, age) "
					 +" values (? , ?, to_number(?))";
			     
				ps = conn.prepareStatement(qry);			
				ps.setString(1, strID);
				ps.setString(2, strName);
				ps.setString(3, strAge);			
				
				ps.executeUpdate();
				
				System.out.println("ID "+strID+" 를 등록 했습니다.");
				
				System.out.println("Model 2");
			
		   }catch (Exception e) {
			    intI = 1;
				System.out.println("Error =>"+e);			
			 }finally {
				 /* Close */ 
				try {
					if(rs != null) rs.close();
					if(ps != null) ps.close();
					if(conn != null) conn.close();			
				}catch (Exception e2) {			
				}
			}	
		 
		 return intI;
		
		
	}

}

▶ People List  서블릿 ( PeopleList.java )   |   List 서비스 ( PeopleService.java )

더보기

PeopleList.java

package controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;


import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import service.PeopleService;
import vo.People;
import comm.DbConn;

/**
 * Servlet implementation class PeopleList
 */
@WebServlet("/PeopleList")
public class PeopleList extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    
    public PeopleList() {
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	    List<People> list = new ArrayList<>();
	    
	    PeopleService peopleService = new PeopleService();
	    
	    
	    	list = peopleService.doSelect();
			// 리스트의 값을 Attribute 를 통해 주고 받는다.
			request.setAttribute("people", list);
			
			/* forward 방식 
			 * List 의 값을 Attribute 를 통해 전달한다. 
			 * */		
			 RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/view/list.jsp");
			 dispatcher.forward(request, response);
		
		
		System.out.println("Hi Servlet Controller, Controller2");
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request,response);
	}

}

 

PeopleService.java

package service;

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

import comm.DbConn;
import vo.People;

public class PeopleService {
	
	List<People> list = new ArrayList<>();
	
	public List<People> doSelect(){
	
		Connection conn = null; // DB 에 connection 된 객체를 저장 
	    PreparedStatement ps = null;  // connection 객체에 실행문을 던지는 역할(창구)
	    ResultSet rs = null;     // select 결과값을 받아옮
	    String qry="";
		DbConn dbConn = new DbConn();	    
		conn = dbConn.getConn();
				
		 try{ 
			    /* Result Set , Print */	
				qry = " select id, name, age, to_char(reg_date,'yyyy.mm.dd') as dati " 
					+" from people ";
				
				ps = conn.prepareStatement(qry);
				rs = ps.executeQuery();
				
				while(rs.next()) {
					// Poeple 생성자를 이용하여 값을 입력 
					People people = new People(rs.getString("id"), rs.getString("name"), rs.getString("age"), rs.getString("dati"));
					list.add(people);				
				}
				
				System.out.println(list.size());
				System.out.println("Model 2");
			
		   }catch (Exception e) {
				System.out.println("Error =>"+e);			
			 }finally {
				 /* Close */ 
				try {
					if(rs != null) rs.close();
					if(ps != null) ps.close();
					if(conn != null) conn.close();			
				}catch (Exception e2) {			
				}
			}				
		
		  return list;
	}

}

▶ People 삭제 서블릿 ( DeletePeople.java )   |   삭제 서비스 ( DeleteService.java )

더보기

DeletePeople.java

package controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;


import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import service.DeleteService;
import service.PeopleService;
import service.RegistService;
import vo.People;
import comm.DbConn;

/**
 * Servlet implementation class PeopleList
 */
@WebServlet("/DeletePeople")
public class DeletePeople extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    
    public DeletePeople() {
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
    	
    	/* ID 를 받는 부분 */
		request.setCharacterEncoding("UTF-8");
    	
	    String strID 	= request.getParameter("id");
	   	
		/* 해당 인자값을 던져서 Insert 시키는 메서드 Call */
	    DeleteService deleteservice = new DeleteService();
	    int intResult = deleteservice.doDelete(strID);
	    
		/* 작업이 끝난 후 가야할 위치 지정 */
		List<People> list = new ArrayList<>();
	    
	    PeopleService peopleService = new PeopleService();
		
		list = peopleService.doSelect();
		// 리스트의 값을 Attribute 를 통해 주고 받는다.
		request.setAttribute("people", list);
		
		/* forward 방식 
		 * List 의 값을 Attribute 를 통해 전달한다. 
		 * */		
		 RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/view/list.jsp");
		 dispatcher.forward(request, response);
		 
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request,response);
	}

}

 

DeleteService.java 

package service;

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

import comm.DbConn;
import vo.People;

public class DeleteService {
	
	
	public int doDelete(String strID){
	
		int intI =0;
		Connection conn = null; // DB 에 connection 된 객체를 저장 
	    PreparedStatement ps = null;  // connection 객체에 실행문을 던지는 역할(창구)
	    ResultSet rs = null;     // select 결과값을 받아옮
	    String qry="";
		DbConn dbConn = new DbConn();	    
		conn = dbConn.getConn();
				
		 try{ 
			       
			     /* Result Set , Print */	
				qry = " delete from people where id=?";
				 
				ps = conn.prepareStatement(qry);			
				ps.setString(1, strID);
				
				ps.executeUpdate();
				
				System.out.println("ID "+strID+" 를 삭제 했습니다.");				
				System.out.println("Model 2, doDelete");
			
		   }catch (Exception e) {
			    intI = 1;
				System.out.println("Error =>"+e);			
			 }finally {
				 /* Close */ 
				try {
					if(rs != null) rs.close();
					if(ps != null) ps.close();
					if(conn != null) conn.close();			
				}catch (Exception e2) {			
				}
			}	
		 
		 return intI;
		
		
	}

}

▶ People VO ( People.java )

더보기

People VO

package vo;

/* People DB 에서 값을 받아 저장하고, View 에 던져주는 매개체 역할을 합니다. */
public class People {
	
	private String strID;
	private String strName;
	private String strAge;
	private String strDati;
	
	
	public People(String strID, String strName, String strAge, String strDati) {
		this.strID = strID;
		this.strName = strName;
		this.strAge = strAge;
		this.strDati = strDati;
	}
	
	public String getStrID() {
		return strID;
	}
	public void setStrID(String strID) {
		this.strID = strID;
	}
	public String getStrName() {
		return strName;
	}
	public void setStrName(String strName) {
		this.strName = strName;
	}
	public String getStrAge() {
		return strAge;
	}
	public void setStrAge(String strAge) {
		this.strAge = strAge;
	}
	public String getStrDati() {
		return strDati;
	}
	public void setStrDati(String strDati) {
		this.strDati = strDati;
	}

	@Override
	public String toString() {
		return "People [strID=" + strID + ", strName=" + strName + ", strAge="
				+ strAge + ", strDati=" + strDati + "]";
	}
	

}

작업 화면 구조

 

<작업화면 캡쳐>