?? msgbookaction.java
字號:
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");
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -