EditEmployeeAction.java
资源名称:某公司的java培训教材 [点击查看]
上传用户:dinglihq
上传日期:2013-02-04
资源大小:99958k
文件大小:4k
源码类别:
Java编程
开发平台:
Java
- package strutsds;
- import java.io.IOException;
- import javax.servlet.ServletContext;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import javax.servlet.http.HttpSession;
- import org.apache.struts.action.Action;
- import org.apache.struts.action.ActionForm;
- import org.apache.struts.action.ActionForward;
- import org.apache.struts.action.ActionMapping;
- import org.apache.struts.action.ActionErrors;
- import org.apache.struts.action.ActionError;
- import javax.sql.DataSource;
- import java.sql.Connection;
- import java.sql.Statement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- public class EditEmployeeAction extends Action {
- protected void updateUser(ActionForm form)throws Exception {
- String user = null;
- Connection conn = null;
- Statement stmt = null;
- ResultSet rs = null;
- ServletContext context = servlet.getServletContext();
- DataSource dataSource = (DataSource)
- context.getAttribute(Action.DATA_SOURCE_KEY);
- try {
- EmployeeActionForm eForm = (EmployeeActionForm)form;
- conn = dataSource.getConnection();
- stmt = conn.createStatement();
- StringBuffer sqlString =new StringBuffer("update employees ");
- sqlString.append("set password='"
- + eForm.getPassword() + "', ");
- sqlString.append("roleid="
- + eForm.getRoleid() + ", ");
- sqlString.append("name='"
- + eForm.getName() + "', ");
- sqlString.append("phone='"
- + eForm.getPhone() + "', ");
- sqlString.append("email='"
- + eForm.getEmail() + "', ");
- sqlString.append("depid="
- + eForm.getDepid());
- sqlString.append(" where username='"
- + eForm.getUsername() + "'");
- stmt.execute(sqlString.toString());
- conn.commit();
- System.out.println("udpate in edit employee actioni"+sqlString);
- }
- finally {
- if (rs != null) {
- rs.close();
- }
- if (stmt != null) {
- stmt.close();
- }
- if (conn != null) {
- conn.close();
- }
- }
- }
- public ActionForward perform(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse httpServletResponse) {
- /**@todo: complete the business logic here, this is just a skeleton.*/
- // Default target to success
- String target = "success";
- /*EmployeesActionMapping employeesMapping =(EmployeesActionMapping)mapping;
- // Does this action require the user to login
- if ( employeesMapping.isLoginRequired() ) {
- HttpSession session = request.getSession();
- if ( session.getAttribute("USER") == null ) {
- // The user is not logged in
- target = new "login";
- ActionErrors errors = new ActionErrors();
- errors.add(ActionErrors.GLOBAL_ERROR,
- new ActionError("errors.login.required"));
- // Report any errors we have discovered
- //back to the original form
- if (!errors.empty()) {
- saveErrors(request, errors);
- }
- return (mapping.findForward(target));
- }
- }*/
- if ( isCancelled(request) ) {
- // Cancel pressed back to employee list
- return (mapping.findForward("success"));
- }
- try {
- updateUser(form);
- }
- catch (Exception e) {
- System.err.println("Setting target to error");
- target = "error";
- ActionErrors errors = new ActionErrors();
- errors.add(ActionErrors.GLOBAL_ERROR,
- new ActionError("errors.database.error",
- e.getMessage()));
- // Report any errors
- if (!errors.empty()) {
- saveErrors(request, errors);
- }
- }
- // Forward to the appropriate View
- return (mapping.findForward(target));
- }
- }