日期:2014-05-17 浏览次数:20665 次
ackage actionform; import java.util.*; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import javax.sql.DataSource; public class ActionNews { public NewsForm form; public Connection conn; public ActionNews(NewsForm form, DataSource ds) throws Exception { super(); this.form = form; this.conn = ds.getConnection(); }public void save() throws Exception { try { String newstheme = form.getNews_theme(); String newsauthor = form.getNews_author(); String newsdate = form.getNews_date(); String newsdetail = form.getNews_detail(); String newsurl = form.getNews_url(); String sql = "insert into s_news(news_theme,news_author,news_date,news_detail,news_url) values('" + newstheme + "','" + newsauthor + "','" + newsdate + "','" + newsdetail + "','" + newsurl + "')"; PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.executeUpdate(sql); pstmt.close(); conn.commit(); conn.close(); } catch (Exception e) { conn.rollback(); throw new Exception(e.getMessage()); } }
public class AllNewsAction extends DispatchAction { public ActionForward addnews(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { NewsForm AllNewsActionForm = (NewsForm) form; DataSource ds = getDataSource(request, "struts"); try { ActionNews actionNews = new ActionNews(AllNewsActionForm, ds); if (((NewsForm) form).getNews_theme().trim().equals("") || ((NewsForm) form).getNews_author().trim().equals("") || ((NewsForm) form).getNews_date().trim().equals("")) throw new Exception("主题,作者,日期,三者不允许为空!"); else { actionNews.save(); request.setAttribute("info", "保存成功!"); } } catch (Exception e) { request.setAttribute("info", e.getMessage()); } return mapping.findForward("save"); }