EmployeeListAction.java~2~
上传用户: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.     ArrayList employees = new ArrayList();
  25.     Connection conn = null;
  26.     Statement stmt = null;
  27.     ResultSet rs = null;
  28.     ServletContext context = servlet.getServletContext();
  29.     DataSource dataSource = (DataSource)context.getAttribute(Action.DATA_SOURCE_KEY);
  30.     try {
  31.       conn = dataSource.getConnection();
  32.       stmt = conn.createStatement();
  33.       rs =stmt.executeQuery("select * from employees, roles,"
  34.                             +
  35.                             "departments where employees.roleid=roles.roleid "
  36.                             + "and employees.depid=departments.depid");
  37.       while (rs.next()) {
  38.         employee = new Employee();
  39.         employee.setUsername(rs.getString("username"));
  40.         employee.setName(rs.getString("name"));
  41.         employee.setRolename(rs.getString("rolename"));
  42.         employee.setPhone(rs.getString("phone"));
  43.         employee.setEmail(rs.getString("email"));
  44.         employee.setRoleid(new Integer(rs.getInt("roleid")));
  45.         employee.setDepid(new Integer(rs.getInt("depid")));
  46.         employee.setDepartment(rs.getString("depname"));
  47.         employees.add(employee);
  48.         System.err.println("Username : "
  49.                            + employee.getUsername()
  50.                            + " Department : " + rs.getString("depname"));
  51.       }
  52.     }
  53.     catch (SQLException e) {
  54.       System.err.println(e.getMessage());
  55.     }
  56.     finally {
  57.       if (rs != null) {
  58.         try {
  59.           rs.close();
  60.         }
  61.         catch (SQLException sqle) {
  62.           System.err.println(sqle.getMessage());
  63.         }
  64.         rs = null;
  65.       }
  66.       if (stmt != null) {
  67.         try {
  68.           stmt.close();
  69.         }
  70.         catch (SQLException sqle) {
  71.           System.err.println(sqle.getMessage());
  72.         }
  73.         stmt = null;
  74.       }
  75.       if (conn != null) {
  76.         try {
  77.           conn.close();
  78.         }
  79.         catch (SQLException sqle) {
  80.           System.err.println(sqle.getMessage());
  81.         }
  82.         conn = null;
  83.       }
  84.     }
  85.     return employees;
  86.   }
  87.   public ActionForward perform(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse httpServletResponse) {
  88. // Forward to the appropriate View
  89.     return (mapping.findForward("success"));
  90.   }
  91. }