AddEmployeeAction.java
上传用户: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.       //submit the insertion!
  49.       conn.commit();
  50.     }
  51.     finally {
  52.       if (rs != null) {
  53.         rs.close();
  54.       }
  55.       if (stmt != null) {
  56.         stmt.close();
  57.       }
  58.       if (conn != null) {
  59.         conn.close();
  60.       }
  61.     }
  62. }
  63.   public ActionForward perform(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse httpServletResponse) {
  64.     /**@todo: complete the business logic here, this is just a skeleton.*/
  65.     // Default target to success
  66.         String target = "success";
  67.         /*EmployeesActionMapping employeesMapping =
  68.         (EmployeesActionMapping)mapping;
  69. // Does this action require the user to login
  70.         if ( employeesMapping.isLoginRequired() ) {
  71.         HttpSession session = request.getSession();
  72.         if ( session.getAttribute("USER") == null ) {
  73. // The user is not logged in
  74.         target = "login";
  75.         ActionErrors errors = new ActionErrors();
  76.         errors.add(ActionErrors.GLOBAL_ERROR,
  77.         new ActionError("errors.login.required"));
  78. // Report any errors we have discovered back
  79. // to the original form
  80.         if (!errors.empty()) {
  81.         saveErrors(request, errors);
  82.         }
  83.         return (mapping.findForward(target));
  84.         }
  85.         }*/
  86.         if ( isCancelled(request) ) {
  87. // Cancel button pressed back to employee list
  88.         return (mapping.findForward("success"));
  89.         }
  90.         try {
  91.         insertUser(form);
  92.         }
  93.         catch (Exception e) {
  94.         System.err.println("Setting target to error");
  95.         target = "error";
  96.         ActionErrors errors = new ActionErrors();
  97.         errors.add(ActionErrors.GLOBAL_ERROR,
  98.         new ActionError("errors.database.error",
  99.         e.getMessage()));
  100. // Report any errors
  101. if (!errors.empty()) {
  102. saveErrors(request, errors);
  103. }
  104. }
  105. // Forward to the appropriate View
  106. return (mapping.findForward(target));
  107. }
  108. }