IT기술/Flex

mvc ex

dobbby 2008. 11. 13. 09:59
반응형

package mvc;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

public class GetEmpInfo {
 private HttpServletRequest req;
 
 public GetEmpInfo(HttpServletRequest req){
  this.req=req;
 }
 
 public Hashtable getInfo(){
  Connection con = null;
  DataSource ds = null;
  PreparedStatement ps = null;
  ResultSet rs = null;
  Hashtable ht = new Hashtable();
  String ename = req.getParameter("ENAME");
  
  try{
   Context ctx = new InitialContext();
   ds = (DataSource)ctx.lookup("java:comp/env/jdbc/myoracle");
   con = ds.getConnection();   
   ps = con.prepareStatement("select * from EMP where ENAME=?");
   ps.setString(1, ename);
   rs = ps.executeQuery();
   if(rs.next()){
    ht.put("EMPNO",rs.getInt("EMPNO"));
    ht.put("ENAME",rs.getString("ENAME"));
    ht.put("JOB",rs.getString("JOB"));
    ht.put("MGR",rs.getInt("MGR"));
    ht.put("HIREDATE",rs.getString("HIREDATE"));
    ht.put("SAL",rs.getInt("SAL"));
    ht.put("COMM",rs.getInt("COMM"));
    ht.put("DEPTNO",rs.getInt("DEPTNO"));
   }
   return ht;
  }
  catch (Exception e){
   e.printStackTrace();
  }
  finally{
   try{
    if (rs != null) rs.close();
    if (ps != null) ps.close();
    if (con !=null) con.close();
   }
   catch (Exception e){
    e.printStackTrace();
   }
  }
  return null;
 }
}

-----------j-a-v-a----c-o-n-t-r-o-l-l-e-r---------------------

package mvc;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Hashtable;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import mvc.Gugu;

public class EmpController extends HttpServlet {
 protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 
 
 GetEmpInfo g = new GetEmpInfo(request);
 Hashtable result = g.getInfo();
 
 request.setAttribute("empInfo" , result);
 
 RequestDispatcher rd = request.getRequestDispatcher("/jsp/mvc/viewEmp.jsp");
 rd.forward(request, response);
 
 
 }
}

---------------------------j-s-p-------------------------------------

<%@ page contentType="text/html; charset=EUC-KR" %>

<%@ page import = "java.util.*" %>

사번: ${requestScope.empInfo.EMPNO}<br>
이름: ${requestScope.empInfo.ENAME}<br>
직업: ${requestScope.empInfo.JOB}<br>
담당매니저: ${requestScope.empInfo.MGR}<br>
입사일: ${requestScope.empInfo.HIREDATE}<br>
연봉: ${requestScope.empInfo.SAL}<br>
커미션: ${requestScope.empInfo.COMM}<br>
부서번호: ${requestScope.empInfo.DEPTNO}<br>


<%Hashtable empInfo = (Hashtable)request.getAttribute("empInfo");
%>

<%="<br>사번: "+empInfo.get("EMPNO")+"<br>"%>
<%="이름: "+empInfo.get("ENAME")+"<br>"%>
<%="직업: "+empInfo.get("JOB")+"<br>"%>
<%="담당매니저: "+empInfo.get("MGR")+"<br>"%>
<%="입사일: "+empInfo.get("HIREDATE")+"<br>"%>
<%="연봉: "+empInfo.get("SAL")+"<br>"%>
<%="커미션: "+empInfo.get("COMM")+"<br>"%>
<%="부서번호: "+empInfo.get("DEPTNO")+"<br>"%>

---------------------h-t-m-l------------------------------------

<html>
<head><title>사원정보보기</title></head>
<body>
 <center><br>
  <h5>사원의 이름을 클릭하시면 사원의 정보를 볼 수 있습니다.</h5>
  <a href="/Webtest/EmpController?ENAME=SMITH">SMITH</a><br>
  <a href="/Webtest/EmpController?ENAME=ALLEN">ALLEN</a><br>  
  <a href="/Webtest/EmpController?ENAME=WARD">WARD</a><br>
  <a href="/Webtest/EmpController?ENAME=JONES">JONES</a><br>
  <a href="/Webtest/EmpController?ENAME=MARTIN">MARTIN</a><br>
  <a href="/Webtest/EmpController?ENAME=BLAKE">BLAKE</a><br>
  <a href="/Webtest/EmpController?ENAME=CLARK">CLARK</a><br>
  <a href="/Webtest/EmpController?ENAME=SCOTT">SCOTT</a><br>
  <a href="/Webtest/EmpController?ENAME=KING">KING</a><br>
  <a href="/Webtest/EmpController?ENAME=TURNER">TURNER</a><br>
  <a href="/Webtest/EmpController?ENAME=ADAMS">ADAMS</a><br>
  <a href="/Webtest/EmpController?ENAME=JAMES">JAMES</a><br>
  <a href="/Webtest/EmpController?ENAME=FORD">FORD</a><br>
  <a href="/Webtest/EmpController?ENAME=MILLER">MILLER</a><br>
 </center>
</body>
</html>



반응형