FrontEnd/JSP (JavaServerPage)

JSP Templates(템플릿 이용, 부서 별 사원 입력 및 출력)

H_Develop 2022. 8. 19. 18:00

New Dynamic Web Project 생성

Project name > TempEx

 

WEB-INF > lib

commons-collections-3.2.1.jar

commons-dbcp-1.2.2.jar

commos-pool-1.4.jar

ojdbc14.jar

 

META-INF

context.xml

 

파일 저장

eclipse > window > Preferences > Java > Editor > Templates

TempEx 패키지 안에 service 패키지 DBService 클래스 만들기

 

package service;

import java.sql.SQLException;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import java.sql.Connection;

public class DBService {
	static DBService single = null;
    public static DBService getInstance() {
    	if(single == null) {single = new DBService();}
        return single;
    }	// 템플릿으로 생성하는 싱글톤 코드
    DataSource ds;
    public DBService() {
    	try {
        	InitialContext ic = new InitialContext();
            ds = (DataSource)ic.lookup("java:comp/env/jdbc/oracle_test");
        }	catch (NamingException e) {
        	e.printStackTrace();
        }
    }
    public Connection getConnection() {
    	Connection conn = null;
        try {
        	conn = ds.getConnection();
        }	catch (SQLException e) {
        	e.printStackTrace();
        }
        return conn;
    }
}

 

dao 패키지 > DeptDAO 클래스 생성

 

package dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import service.DBService;	// 같은 프로젝트 안에서 다른 패키지에 있는 클래스를 불러서 사용가능
import vo.DeptVO;

public class DeptDAO {
	static DeptDAO single = null;
    public static DeptDAO getInstance() {
    	// 생성되지 않았으면 생성
        if(single == null) {single = new DeptDAO();}
        // 생성된 객체정보를 반환
        return single;
    }
    public List<DeptVO> selectList() {
    	List<DeptVO> list = new ArrayList<>();
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        String sql = "select * from dept";
        
        try {
        	conn = DBService.getInstance().getConnection();
            // 2. 명령처리객체정보를 얻어오기
            pstmt = conn.prepareStatement(sql);
            // 3. 결과행 처리객체 얻어오기
            rs = pstmt.executeQuery();
            
            while(rs.next()) {
            	DeptVO vo = new DeptVO();
                // 현재 레코드값 > vo 저장
                vo.setDeptNo(rs.getInt("deptno"));
                vo.setdName(rs.getString("dname"));
                vo.setLoc(rs.getString("loc"));
                // ArrayList 추가
                list.add(vo);
            }
        }	catch (Exception e) {
        	e.printStackTrace();
		}	finally {
        	try {
            	if(rs != null) {rs.close();}
            	if(pstmt != null) {pstmt.close();}
            	if(conn != null) {conn.close();}
            }	catch (SQLException e) {
            	e.printStackTrace();
            }
        }
        return list;
    }
}

 

 

같은 프로젝트에서 vo 패키지 > DeptVO 클래스 생성

 

package vo;

public class DeptVO {
	int deptNo;
    String dName;
    String loc;
    public int getDeptNo() {
    	return deptNo;
    }
    public void setDeptNo (int deptNo) {
    	this.deptNo = deptNo;
    }
    public String getdName() {
    	return dName;
    }
    public void setdName(String dName) {
    	this.dName = dName;
    }
    public String getLoc() {
    	return loc;
    }
    public void setLoc(String loc) {
		this.loc = loc;
	}
}

 

 

jdbc_dept.jsp 생성

 

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ page import="dao.DeptDAO" %>
<%@ page import="vo.DeptVO" %>
<%@ page import="java.util.List" %>
<!DOCTYPE html>
<%
	DeptDAO dao = Dept.DAO.getInstance();
    List<DeptVO> dept_list = dao.selectList();
    System.out.println(dept_list.size());
%>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
	<table border="1">
    	<caption>부서목록</caption>
        <tr>
        	<th>부서번호</th>
            <th>부서명</th>
            <th>부서위치</th>
        </tr>
        <%
        	for(int i=0; i<dept_list.size(); i++) {
            	DeptVO dv = dept_list.get(i);
        %>
        <tr>
        	<td><%= dv.getDeptNo() %></td>
        	<td><%= dv.getdName() %></td>
        	<td><%= dv.getLoc() %></td>
        </tr>
        <%
        	}
        %>
    </table>
</body>
</html>

 

결과는 부서 별 사원 입력 및 출력 꺼랑 같다.

같은 프로젝트 안에 있는 클래스 import로 가져다 쓸 수 있고,

템플릿을 넣어놔서 자동완성 기능이 있다는 것을 실습하기 위해 한 것 이다.