본문 바로가기

bitcamp/면접족보

면접족보 21/01/12_오라클 table 생성법

1. Oracle에서 mvc_board table 생성하는 방법

 

※연습문제

♣아래를 프로그래밍 하시오♣

· 객체를 생성하고, 성적을 입력하여 다른 페이지에서 평균점수를 출력할 것!

 

<구현 화면>

 

<grade.java>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
 <head>
 <meta charset="EUC-KR">
 <title>Insert title here</title>
 <style>
    table{
        border: 1px solid blue;
        border-collapse: collapse;
    }
 </style>
 </head>
 <body>
    <form action="grade.jsp" method="post">
        <h1>성적입력</h1>
        <table border="1">
            <tr>
                <td colspan="2">학번</td>
                <td><input type="text" name="num" size=15></td>
            </tr>
            <tr> 
                <td rowspan="4">과목</td>
            </tr>    
            <tr>
                <td>Java</td>
                <td><input type="text" name="java" size=15></td>
            </tr>    
            <tr>
                <td>Database</td>
                <td><input type="text" name="database" size=15></td>
            </tr>    
            <tr>
                <td>JSP</td>
                <td><input type="text" name="jsp" size=15></td>
            </tr>    
            <tr>
                <td colspan="3"><input type="submit" value="전송"></td>
            </tr>    
        </table>
    </form>
 </body>
 </html>
cs

 

<grade.html>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
 public class grade {
    private int num;
    private int java;
    private int database;
    private int jsp;
    
    public grade() {
    }
    public int getNum() {
        return num;
    }
    public void setNum(int num) {
        this.num = num;
    }
    public int getJava() {
        return java;
    }
    public void setJava(int java) {
        this.java = java;
    }
    public int getDatabase() {
        return database;
    }
    public void setDatabase(int database) {
        this.database = database;
    }
    public int getJsp() {
        return jsp;
    }
    public void setJsp(int jsp) {
        this.jsp = jsp;
    }
    public double getResult() {
        return (java + database + jsp)/3.0;
    }
 }
cs

 

<grade.jsp>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
 <head>
 <meta charset="EUC-KR">
 <title>Insert title here</title>
 <style>
    table{
        border: 1px color #cccccc;
    }
 </style>
 </head>
 <body>    
    <% 
        int num = Integer.parseInt(request.getParameter("num"));
        int java = Integer.parseInt(request.getParameter("java"));
        int database = Integer.parseInt(request.getParameter("database"));
        int jsp = Integer.parseInt(request.getParameter("jsp"));
        grade.setNum(num);
        grade.setJava(java);
        grade.setDatabase(database);
        grade.setJsp(jsp);
    %>
    <table border="1">
        <tr>
            <td colspan="2">학번</td>
            <td>${param.num}</td>
        </tr>    
        <tr> 
            <td rowspan="4">과목</td>
        </tr>    
        <tr>
            <td>Java</td>
            <td>${param.java}</td>
        </tr>
        <tr>
            <td>Database</td>
            <td>${param.database}</td>
        </tr>
        <tr>
            <td>JSP</td>
            <td>${param.jsp}</td>
        </tr>
        <tr>
            <td colspan="2">평균점수</td>
            <td>${grade.getResult}</tr>
        <tr>
            <td colspan="3"><input type="button" value="입력화면" onClick=history.back()></td>
        </tr>
    </table>
 </body>
cs

 

 

 

♣MVC 형태로 EMP 리스트를 출력하시오♣

 

<구현 화면>

 

<FrontController.java>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
 @WebServlet("*.do")
 public class FrontController extends HttpServlet {
    private static final long serialVersionUID = 1L;
 
        public FrontController() {
            super();
    }
 
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("doGet");
        actionDo(request, response);
    }
 
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("doPost");
        actionDo(request, response);
    }
 
    private void actionDo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("actionDo");
        
        request.setCharacterEncoding("EUC-KR");
        
        String viewPage = null;
        Command command = null;
        
        String uri = request.getRequestURI();
        String conPath = request.getContextPath();
        String com = uri.substring(conPath.length());
        
        if(com.equals("/list.do")) {
            command = new ListCommand();
            command.execute(request, response);
            viewPage = "list.jsp";
        } 
        RequestDispatcher dispatcher = request.getRequestDispatcher(viewPage);
        dispatcher.forward(request, response);      
    }
 }
cs

 

<Command.java>

1
2
3
 public interface Command {
    abstract void execute(HttpServletRequest request, HttpServletResponse response);
 }
cs

 

<ListCommand.java>

1
2
3
4
5
6
7
8
 public class ListCommand implements Command{
    @Override
    public void execute(HttpServletRequest request, HttpServletResponse response) {
        Dao dao = new Dao();
        ArrayList<Dto> dtos = dao.list();
        request.setAttribute("list", dtos);
    }
 }
cs

 

<Dao.java>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
 public class Dao {
    DataSource dataSource;
    
    public Dao() {
        try {
            Context context = new InitialContext();
            dataSource = (DataSource)context.lookup("java:comp/env/jdbc/oracle");
        }catch(Exception e) {
            e.printStackTrace();
        }
    }
    public ArrayList<Dto> list(){
        ArrayList<Dto> dtos = new ArrayList<Dto>();
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        
        try {
            con = dataSource.getConnection();
            String query = "select * from emp,dept where emp.deptno=dept.deptno";
            ps = con.prepareStatement(query);
            rs = ps.executeQuery();
            
            while(rs.next()) {
                int empno = rs.getInt("empno");
                String ename = rs.getString("ename");
                String job = rs.getString("job");
                String mgr = rs.getString("mgr");
                String hiredate = rs.getString("hiredate");
                int sal = rs.getInt("sal");
                int comm = rs.getInt("comm");
                int deptno = rs.getInt("deptno");
                String dname = rs.getString("dname");
                String loc = rs.getString("loc");
            
                Dto dto = new Dto(empno, ename, job, mgr, hiredate, sal, comm, deptno, dname, loc);
                dtos.add(dto);
            }
        }catch(Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if(rs != null) rs.close();
                if(ps != null) ps.close();
                if(con != null) con.close();
            }catch(Exception e2) {
                e2.printStackTrace();
            }
        }
        return dtos;
    }
 }
cs

 

<Dto.java>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
 public class Dto {
    private int empno;   
    private String ename; 
    private String job;         
    private String mgr;              
    private String hiredate;         
    private int sal;
    private int comm;
    private int deptno;
    private String dname;
    private String loc;
    
    public Dto() {}
    
    public Dto(int empno, String ename, String job, String mgr, String hiredate, int sal, int comm, int deptno,
            String dname, String loc) {
        this.empno = empno;
        this.ename = ename;
        this.job = job;
        this.mgr = mgr;
        this.hiredate = hiredate;
        this.sal = sal;
        this.comm = comm;
        this.deptno = deptno;
        this.dname = dname;
        this.loc = loc;
    }
    public int getEmpno() {
        return empno;
    }
    public void setEmpno(int 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 String getHiredate() {
        return hiredate;
    }
    public void setHiredate(String hiredate) {
        this.hiredate = hiredate;
    }
    public int getSal() {
        return sal;
    }
    public void setSal(int sal) {
        this.sal = sal;
    }
    public int getComm() {
        return comm;
    }
    public void setComm(int comm) {
        this.comm = comm;
    }
    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;
    }
 }
cs

 

<list.jsp>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
 <title></title>
    <style>
        table{
            width: 1000px; 
        }
        #title{
            font-size: 1.2em;
            text-align:center;
        }
    </style>
 </head>
 <body>
   <h1>EMP 테이블 직원 목록</h1>
   <table border="1">
      <tr id="title">
         <td>사원번호</td>
         <td>사원이름</td>
         <td>직급(업무)</td>
         <td>상사(이름)</td>
         <td>입사일</td>
         <td>급여</td>
         <td>커미션</td>
         <td>부서번호</td>
         <td>부서이름</td>
         <td>부서위치</td>
         <td>관리</td>
      </tr>
      <c:forEach items="${list}" var="dto">
      <tr>
         <td>${dto.empno}</td>
         <td>${dto.ename}</td>
         <td>${dto.job}</td>
         <td>${dto.mgr}</td>
         <td>${dto.hiredate}</td>
         <td>${dto.sal}</td>
         <td>${dto.comm}</td>
         <td>${dto.deptno}</td>
         <td>${dto.dname}</td>
         <td>${dto.loc}</td>
         <td><a href="write_view.do">수정</a></td>
      </tr>
      </c:forEach>
   </table>  
 </body>
cs