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

Java编程

开发平台:

Java

  1. package strutsds;
  2. import java.io.IOException;
  3. import javax.servlet.ServletContext;
  4. import javax.servlet.ServletException;
  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. public class AddEmployeeAction extends Action {
  20.   protected void insertUser(ActionForm form)throws Exception {
  21.     String user = null;
  22.     Connection conn = null;
  23.     Statement stmt = null;
  24.     ResultSet rs = null;
  25.     ServletContext context = servlet.getServletContext();
  26.     DataSource dataSource = (DataSource)context.getAttribute(Action.DATA_SOURCE_KEY);
  27.     try {
  28.       EmployeeActionForm eForm = (EmployeeActionForm)form;
  29.       conn = dataSource.getConnection();
  30.       stmt = conn.createStatement();
  31.       StringBuffer sqlString =new StringBuffer("insert into employees ");
  32.       sqlString.append("values ('"
  33.                        + eForm.getUsername() + "', ");
  34.       sqlString.append("'" +
  35.                        eForm.getPassword() + "', ");
  36.       sqlString.append("'"
  37.                        + eForm.getRoleid() + "', ");
  38.       sqlString.append("'"
  39.                        + eForm.getName() + "', ");
  40.       sqlString.append("'"
  41.                        + eForm.getPhone() + "', ");
  42.       sqlString.append("'"
  43.                        + eForm.getEmail() + "', ");
  44.       sqlString.append("'"
  45.                        + eForm.getDepid() + "')");
  46.       System.out.println("insert stmt of emploees in addemployeeaction"+sqlString);
  47.       stmt.execute(sqlString.toString());
  48.     }
  49.     finally {
  50.       if (rs != null) {
  51.         rs.close();
  52.       }
  53.       if (stmt != null) {
  54.         stmt.close();
  55.       }
  56.       if (conn != null) {
  57.         conn.close();
  58.       }
  59.     }
  60. }
  61.   public ActionForward perform(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse httpServletResponse) {
  62.     /**@todo: complete the business logic here, this is just a skeleton.*/
  63.     // Default target to success
  64.         String target = "success";
  65.         /*EmployeesActionMapping employeesMapping =
  66.         (EmployeesActionMapping)mapping;
  67. // Does this action require the user to login
  68.         if ( employeesMapping.isLoginRequired() ) {
  69.         HttpSession session = request.getSession();
  70.         if ( session.getAttribute("USER") == null ) {
  71. // The user is not logged in
  72.         target = "login";
  73.         ActionErrors errors = new ActionErrors();
  74.         errors.add(ActionErrors.GLOBAL_ERROR,
  75.         new ActionError("errors.login.required"));
  76. // Report any errors we have discovered back
  77. // to the original form
  78.         if (!errors.empty()) {
  79.         saveErrors(request, errors);
  80.         }
  81.         return (mapping.findForward(target));
  82.         }
  83.         }*/
  84.         if ( isCancelled(request) ) {
  85. // Cancel button pressed back to employee list
  86.         return (mapping.findForward("success"));
  87.         }
  88.         try {
  89.         insertUser(form);
  90.         }
  91.         catch (Exception e) {
  92.         System.err.println("Setting target to error");
  93.         target = "error";
  94.         ActionErrors errors = new ActionErrors();
  95.         errors.add(ActionErrors.GLOBAL_ERROR,
  96.         new ActionError("errors.database.error",
  97.         e.getMessage()));
  98. // Report any errors
  99. if (!errors.empty()) {
  100. saveErrors(request, errors);
  101. }
  102. }
  103. // Forward to the appropriate View
  104. return (mapping.findForward(target));
  105. }
  106. }