EmployeeListAction.java~5~
上传用户:dinglihq
上传日期:2013-02-04
资源大小:99958k
文件大小:3k
源码类别:

Java编程

开发平台:

Java

  1. package strutsds;
  2. import java.io.IOException;
  3. import javax.servlet.ServletException;
  4. import javax.servlet.ServletContext;
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;
  7. import javax.servlet.http.HttpSession;
  8. import org.apache.struts.action.Action;
  9. import org.apache.struts.action.ActionForm;
  10. import org.apache.struts.action.ActionForward;
  11. import org.apache.struts.action.ActionMapping;
  12. import org.apache.struts.action.ActionErrors;
  13. import org.apache.struts.action.ActionError;
  14. import javax.sql.DataSource;
  15. import java.sql.Connection;
  16. import java.sql.Statement;
  17. import java.sql.ResultSet;
  18. import java.sql.SQLException;
  19. import java.util.HashMap;
  20. import java.util.ArrayList;
  21. public class EmployeeListAction extends Action {
  22.   protected ArrayList getEmployees() {
  23.     Employee employee = null;
  24.     //1.
  25.     ArrayList employees = new ArrayList();
  26.     Connection conn = null;
  27.     Statement stmt = null;
  28.     ResultSet rs = null;
  29.     ServletContext context = servlet.getServletContext();
  30.     DataSource dataSource = (DataSource)context.getAttribute(Action.DATA_SOURCE_KEY);
  31.     try {
  32.       conn = dataSource.getConnection();
  33.       stmt = conn.createStatement();
  34.       rs =stmt.executeQuery("select * from employees, roles,"
  35.                             +
  36.                             "departments where employees.roleid=roles.roleid "
  37.                             + "and employees.depid=departments.depid");
  38.       while (rs.next()) {
  39.        //2.new Employee() object
  40.         employee = new Employee();
  41.         employee.setUsername(rs.getString("username"));
  42.         employee.setName(rs.getString("name"));
  43.         employee.setRolename(rs.getString("rolename"));
  44.         employee.setPhone(rs.getString("phone"));
  45.         employee.setEmail(rs.getString("email"));
  46.         employee.setRoleid(new Integer(rs.getInt("roleid")));
  47.         employee.setDepid(new Integer(rs.getInt("depid")));
  48.         employee.setDepartment(rs.getString("depname"));
  49.         //3.add it to the collection
  50.         employees.add(employee);
  51.         System.err.println("in EmployeeActionList ---Username : "
  52.                            + employee.getUsername()
  53.                            + " Department : " + rs.getString("depname"));
  54.       }
  55.     }
  56.     catch (SQLException e) {
  57.       System.err.println(e.getMessage());
  58.     }
  59.     finally {
  60.       if (rs != null) {
  61.         try {
  62.           rs.close();
  63.         }
  64.         catch (SQLException sqle) {
  65.           System.err.println(sqle.getMessage());
  66.         }
  67.         rs = null;
  68.       }
  69.       if (stmt != null) {
  70.         try {
  71.           stmt.close();
  72.         }
  73.         catch (SQLException sqle) {
  74.           System.err.println(sqle.getMessage());
  75.         }
  76.         stmt = null;
  77.       }
  78.       if (conn != null) {
  79.         try {
  80.           conn.close();
  81.         }
  82.         catch (SQLException sqle) {
  83.           System.err.println(sqle.getMessage());
  84.         }
  85.         conn = null;
  86.       }
  87.     }
  88.     return employees;
  89.   }
  90.   public ActionForward perform(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse httpServletResponse) {
  91. // Forward to the appropriate View
  92.     request.setAttribute("employees",getEmployees());
  93.     return (mapping.findForward("success"));
  94.   }
  95. }