81,092
社区成员
发帖
与我相关
我的任务
分享
public class AllNewsAction extends DispatchAction
{
public ActionForward addnews(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response)
{
NewsForm AllNewsActionForm=(NewsForm)form;
BasicDataSource ds = (BasicDataSource) getDataSource(request,"struts");
try
{
ActionNews actionNews= new ActionNews(AllNewsActionForm,ds);
if(((NewsForm) form).getNews_id().trim().equals(""))
throw new Exception("编号不允许为空!");
else if (((NewsForm) form).getNews_id().length()>4)
throw new Exception("编号长度最长为4位!");
else
{
actionNews.save();
request.setAttribute("info","保存成功!");
}
}
catch(Exception e)
{
request.setAttribute("info",e.getMessage());
}
return mapping.findForward("save");
}
public ActionForward showall(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)
{
NewsForm AllNewsActionForm=(NewsForm)form;
BasicDataSource ds = (BasicDataSource) getDataSource(request,"struts");
try
{
ActionNews actionNews= new ActionNews(AllNewsActionForm,ds);
List<String[]>result=actionNews.search();
if(result.size()>0)
{
request.setAttribute("result",result);
request.setAttribute("info_all", "总记录数:" + String.valueOf(result.size()));
}
else
request.setAttribute("info_all", "没有符合要求的记录!");
}
catch(Exception e)
{
request.setAttribute("info_all",e.getMessage());
}
return mapping.findForward("searchAll");
}
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 List<String[]>search() throws Exception
{
List<String[]> result = new LinkedList<String[]>();
String sql="select * from s_news";
PreparedStatement pstmt= conn.prepareStatement(sql);
ResultSet rs =pstmt.executeQuery();
while(rs.next())
{
String[] row=new String[6];
row[0] = rs.getString(1);
row[1] = rs.getString(2);
row[2] = rs.getString(3);
row[3] = rs.getString(4);
row[4] = rs.getString(5);
row[5] = rs.getString(6);
result.add(row);
}
rs.close();
conn.close();
return result;
}
public void save()throws Exception
{
try
{
String newsid=form.getNews_id();
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 values('"+newsid+"','"+newstheme+"','"+newsauthor+"','"+newsdate+"','"+newsdetail+"','"+newsurl+"')";
PreparedStatement pstmt=conn.prepareStatement(sql);
pstmt.executeUpdate(sql);
pstmt.close();
conn.close();
}
catch(Exception e)
{
throw new Exception(e.getMessage());
}
}
<global-forwards>
<forward name="searchAll" path="/index.jsp"/>
<forward name="save" path="/message.jsp" />
</global-forwards>
<action-mappings>
<action name="AllNewsActionForm" path="/AllNewsAction" scope="request" type="action.AllNewsAction" parameter="method">
<forward name="searchAll" path="/index.jsp"/>
<forward name="save" path="/message.jsp" />
</action>
//这个form可以查询数据库里现有所有的数据
<html:form action="AllNewsAction"><input type=hidden name="method" value="showall">
<html:submit value="查询所有记录 " />
</html:form>
//这个增加却不行,求解答。
<html:form action="AllNewsAction"><input type=hidden name="method" value="addnews">
<table width=100%>
<tr>
<td align="center">
编号:
<html:text property="news_id" maxlength="4" />
<p>
日期:
<html:text property="news_date" />
<br><br>链接:
<html:text property="news_url" />
<br><br>
</td>
</tr>
<tr>
<td align="center">
<html:submit value="提交" />
<html:reset value="重置" />
</td>
</tr>
</table>
</html:form>