/ LECTURE

JDBC - DAO / VO

이 페이지는 다음에 대한 공부 기록입니다

JAVA(자바), Python(파이썬) 기반의

AI 활용 응용 소프트웨어 개발자 양성 과정

2021.11.10. ~ 2022.05.18.

찾으시는 정보가 있으시다면
주제별reference를 이용하시거나
우측 상단에 있는 검색기능을 이용해주세요

53일차 수업

DAO와 VO

import java.sql.*;
import java.util.*;

public class EmpDAO{
private static final String dbDriver = oracle.jdbc.OracleDriver;
private static final String url = jdbc.oracle:thin:@db주소;
private static final String id = db 계정;
private static final String pw = db 암호;

	private EmpDAO instance;
	private Connection conn;

	public static empDAO getInstance() throws Exception{
		if(instance == null){
			instance = new EmpDAO();
		}
		Return instance;
	}

	private EmpDAO() throws Exception{
		Class.forName(dbDriver);
	}

	public int insert(EmpVO vo) throws Exception{
		PreparedStatement ps = null;
		try{
			int result = 0;
			conn = DriverManager.getConnection(url,id,pw);
			
			String sql = INSERT INTO emp(empno, ename) VALUES(?,?);
			
			ps = conn.prepareStatement(sql);
			ps.setInt(1, vo.getEmpno());
			ps.setString(2,vo.getEname());

			result = ps.executeUpdate();
			return result;
		}	// finally 로 ps와 conn 닫기
	}

	public List<temp.EmpVO> select() throws Exception {
		PreparedStatement ps = null;
		ResultSet rs = null;

		String sql = SELECT * FROM emp;

		List<EmpVO> eList = new ArrayList();
		// 값이 없을 경우 emptyList를 반환해주기 위해 List로 선언,
		Boolean isEmpty = true;
		// 값이 없을 경우를 구분하기 위한 변수

		try{
			conn = DriverManager.getConnection(url,id,pw);
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			
			while (rs.next()){
				isEmpty = false;
				EmpVO e = new EmpVO();
				e.setEmpno(rs.getInt(empno));
				e.setEname(rs.getString(ename));

				eList.add(e);
				// row당 하나의 클래스를 인스턴스로 만들어 배열에 넣기
			}
		}
			if(isEmpty){
				return Collections.emptyList();
			}
			return eList;
	}
}

Request

<!-- 서버의 정보를 얻어오기 -->
요청정보 프로토콜 = <%= request.getProtocol() %><br><!--HTTP/1.1 -->
요청정보 전송방식 = <%= request.getMethod() %><br><!-- GET -->
요청 URI = <%= request.getRequestURI() %><br><!--- /learning_war_exploded/3_objects/01_requestInfo.jsp -->
컨텍스트 경로 = <%= request.getContextPath() %><br><!-- /learning_war_exploded -->
서버이름 = <%= request.getServerName() %><br><!-- localhost -->
서버포트 = <%= request.getServerPort() %><br><!-- 8080 -->

<!-- 리다이렉트 페이지 이동 -->
<% response.sendRedirect(04_responseSecond.jsp); %>

<!-- 파일 불러오기 -->
<%
char[] buff = new char[1024];
int len = -1;
FileReader fr = new FileReader(파일경로);

	while ((len = fr.read(buff)) != -1){
		out.print(new String(buff,0,len));
	}
	fr.close();
%>