MsgBookAction.java
上传用户:jishiqi_cj
上传日期:2022-08-08
资源大小:24765k
文件大小:2k
- package classmate;
- 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.ActionMessages;
- import org.apache.struts.action.ActionMessage;
- import org.apache.struts.action.DynaActionForm;
- import javax.servlet.ServletContext;
- import javax.servlet.http.*;
- import javax.sql.DataSource;
- import java.sql.*;
- public final class MsgBookAction extends Action{
- public ActionForward execute(
- ActionMapping mapping,
- ActionForm form,
- HttpServletRequest request,
- HttpServletResponse response) throws Exception {
-
-
- DynaActionForm dyform = (DynaActionForm) form;
- String name = (String)dyform.get("name");
- String sex = (String)dyform.get("sex");
- String email = (String)dyform.get("email");
- String title = (String)dyform.get("title");
- String content = (String)dyform.get("content");
-
- ServletContext context = servlet.getServletContext();
- DataSource dataSource = (DataSource)context.getAttribute("oracleDB1");
-
- Connection conn = null;
- ActionMessages errors = new ActionMessages();
- String strsql="insert into msgbook values('"
- + name + "','"
- + sex + "','"
- + email + "','"
- + title + "','"
- + content + "')";
- System.out.println("strsql: "+strsql);
- try {
- conn = dataSource.getConnection();
- Statement stmt = conn.createStatement();
- int result = 0;
- result = stmt.executeUpdate(strsql);
-
- if ( result == 1) {
- errors.add(ActionMessages.GLOBAL_MESSAGE,
- new ActionMessage("errors.Submitsuccess"));
- }
- else{
- errors.add(ActionMessages.GLOBAL_MESSAGE,
- new ActionMessage("errors.SubmitFail"));
- }
-
- }catch(SQLException e){
- System.out.println("sqlerror");
- errors.add(ActionMessages.GLOBAL_MESSAGE,
- new ActionMessage("errors.SubmitFail"));
- }finally{
- if(conn!=null)
- conn.close();
- }
-
- if (!errors.isEmpty()) {
- saveErrors(request, errors);
- }
- System.out.println("errors: "+errors.size());
- return mapping.findForward("toMsg");
- }
- }