1. 아래가 돌아 가도록 프로그래밍 하시오.
emp_list_use_bean.jsp 에서
- 최대한 액션태그를 활용하여 12명의 정보가 출력되도록 해보시오.
package emp;
import java.sql.*;
import java.util.ArrayList;
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(",  ENAME: ");
Print.append(this.ename);
Print.append(",  JOB: ");
Print.append(this.job);
Print.append(",  MGR: ");
Print.append(this.mgr);
Print.append(",  HIREDATE: ");
Print.append(this.hiredate);
Print.append(",  SAL: ");
Print.append(this.sal);
Print.append(",  COMM: ");
Print.append(this.comm);
Print.append(",  DEPTNO: ");
Print.append(this.deptno);
Print.append("<br/>");
return Print.getAndReset();
}
public String getResult() {
EmpDao empDao = new EmpDao();
ArrayList<EmpDto> dtos = empDao.empList();
for(EmpDto emp : dtos){
Print.append(emp);
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) {
print.append("x");
} else {
print.append(t);
};
}
static String getAndReset() {
String str = print.toString();
reset();
return str;
}
}
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;
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"
import="emp.*" import="java.util.*"
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>emp_list_use_bean.jsp</title>
</head>
<body>
<jsp:useBean id="bean" class="emp.EmpDto"/>
<jsp:getProperty property="result" name="bean"/>
</body>
</html>