34: Servlet Connection&Statement&ResultSet

jk·2024년 2월 20일
0

kdt 풀스택

목록 보기
66/127



1. 아래가 돌아 가도록 프로그래밍 하시오.

emp_list.jsp 에 아래를 넣고 돌아가도록하시오.
	<%
	EmpDao empDao = new EmpDao();
//	
	ArrayList<EmpDto> dtos = empDao.empList();
//
	for(EmpDto emp : dtos){
		out.print(emp+"<br>");
	}
	%>
//code1
//./src/emp/EmpDto.java
package emp;
import java.sql.*;
public final class EmpDto {
	private String empno;
	private String ename;
	private String job;
	private String mgr;
	private Date hiredate;
	private String sal;
	private String comm;
	private String deptno;
	public EmpDto() {
	}
	public EmpDto(String empno, String ename, String job, String mgr, Date hiredate, String sal, String comm, String deptno) {
		this.empno = empno;
		this.ename = ename;
		this.job = job;
		this.mgr = mgr;
		this.hiredate = hiredate;
		this.sal = sal;
		this.comm = comm;
		this.deptno = deptno;
	}
	public String getEmpno() {
		return empno;
	}
	public void setEmpno(String empno) {
		this.empno = empno;
	}
	public String getEname() {
		return ename;
	}
	public void setEname(String ename) {
		this.ename = ename;
	}
	public String getJob() {
		return job;
	}
	public void setJob(String job) {
		this.job = job;
	}
	public String getMgr() {
		return mgr;
	}
	public void setMgr(String mgr) {
		this.mgr = mgr;
	}
	public Date getHiredate() {
		return hiredate;
	}
	public void setHiredate(Date hiredate) {
		this.hiredate = hiredate;
	}
	public String getSal() {
		return sal;
	}
	public void setSal(String sal) {
		this.sal = sal;
	}
	public String getComm() {
		return comm;
	}
	public void setComm(String comm) {
		this.comm = comm;
	}
	public String getDeptno() {
		return deptno;
	}
	public void setDeptno(String deptno) {
		this.deptno = deptno;
	}
	@Override
	public String toString() {
		Print.append("EMPNO: ");
		Print.append(this.empno);
		Print.append(",&nbsp ENAME: ");
		Print.append(this.ename);
		Print.append(",&nbsp JOB: ");
		Print.append(this.job);
		Print.append(",&nbsp MGR: ");
		Print.append(this.mgr);
		Print.append(",&nbsp HIREDATE: ");
		Print.append(this.hiredate);
		Print.append(",&nbsp SAL: ");
		Print.append(this.sal);
		Print.append(",&nbsp COMM: ");
		Print.append(this.comm);
		Print.append(",&nbsp DEPTNO: ");
		Print.append(this.deptno);
		Print.append("<br/>");
		return Print.getAndReset();
	}
}
class Print {
	private static StringBuilder print = new StringBuilder();
	private static void reset() {
		print.setLength(0);
	}
	static <T> void append(T t) {
		if (t == null) {
			System.out.println("x");
			print.append("x");
		} else {
			print.append(t);
		};
	}
	static String getAndReset() {
		String str = print.toString();
		reset();
		return str;
	}
}
//code2
//./src/emp/EmpDao.java
package emp;
import java.sql.*;
import java.util.ArrayList;
import emp.EmpDto;
public class EmpDao {
	private String driver = "oracle.jdbc.driver.OracleDriver";
	private String url = "jdbc:oracle:thin:@//localhost:1521/xe";
	private String id = "scott";
	private String pw = "tiger";
	public EmpDao() {
		try {
			Class.forName(driver);
		} catch(Exception e) {
			e.printStackTrace();
		};
	}
	public ArrayList<EmpDto> empList() {
		ArrayList<EmpDto> dtos = new ArrayList<EmpDto>();
		Connection connection = null;
		Statement statement = null;
		ResultSet rs = null;
		String query = "select * from emp";
		try {
			Class.forName(driver);
			connection = DriverManager.getConnection(url, id, pw);
			statement = connection.createStatement();
			rs = statement.executeQuery(query);
			while (rs.next()) {
				String empno = rs.getString("empno");
				String ename = rs.getString("ename");
				String job = rs.getString("job");
				String mgr = rs.getString("mgr");
				Date hiredate = rs.getDate("hiredate");
				String sal = rs.getString("sal");
				String comm = rs.getString("comm");
				String deptno = rs.getString("deptno");
				EmpDto dto = new EmpDto(empno, ename, job, mgr, hiredate, sal, comm, deptno);
				dtos.add(dto);
			};
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (rs != null) {
					rs.close();
				};
				if (statement != null) {
					statement.close();
				};
				if (connection != null) {
					connection.close();
				};
			} catch(Exception e2) {
				e2.printStackTrace();
			};
		};
		return dtos;
	}
}



2.아래의 3종 세트를 설명하시오.

  • Connetion : It connects to the sql server with id.
  • Statement : It calls the worksheet from the server.
  • Resultset : It executes and gets the result of command.



3. 에러 페이지 처리 방법 2가지에 대하여 설명하시오.

  • directive
<%@ page isErrorPage ="true" %>
  • web.xml
  <error-page>
  	<error-code>404</error-code>
  	<location>/error404.jsp</location>
  </error-page>
profile
Brave but clumsy

0개의 댓글