这是我创建的数据库
下面是我的各部分代码:
1.这是一个利用get方法组(没有贴上来)获得从添加信息中输入的信息,然后把获得的信息连接成一个字符串,使用创建的数据库对象Statement的executeUpdate()执行sql语句,如果成功,转向success,失败转向fail。
public final class ShowAction extends Action{
public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,
HttpServletResponse response)throws Exception{
Locale locale=getLocale(request);
MessageResources message=getResources(request);
HttpSession session=request.getSession(true);
GetNewForm userform=(GetNewForm)form;
try{
DataSource dataSource;
Connection con;
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/xinwen","root","123456");
Statement st=con.createStatement();
String title=userform.getTitle();
String content=userform.getContent();
String time="";
String keyword=userform.getKeyword();
String author=userform.getAuthor();
java.util.Date d=new java.util.Date();
String sql="insert into news(title,content,news_time,keyword,author)values("+title+","+content+","+time+","+keyword+","+author+")";
st.executeUpdate(sql);
return (mapping.findForward("success"));
}
catch(Exception e)
{
System.out.println(e.toString());
return(mapping.findForward("fail"));
}
}}
2.下面这个类主要实现从数据库中获得记录,把记录放入到ArrayList类的对象里面
public class ListAction extends Action {
public Connection getCon()throws Exception
{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/xinwen","root","123456");
return con;
}
public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,
HttpServletResponse response)throws Exception
{
HttpSession session=request.getSession(true);
ArrayList l=new ArrayList();
try{
Connection con=getCon();
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select *from news");
while(rs.next())
{
ListForm f=new ListForm();
f.setTitle(rs.getString(1));
f.setContent(rs.getString(2));
f.setTimes(rs.getString(3));
f.setKeyword(rs.getString(4));
f.setAuthor(rs.getString(5));
l.add(f);
}
request.setAttribute("1",l);
System.out.println("ok");
return(mapping.findForward("view"));
}
catch(SQLException ex)
{
ex.printStackTrace();
return(mapping.findForward("fail"));
}
}
}
下面是我跳转的 jsp页面 添加数据后一直跳转到自己编写的error.jsp 出错 页面
'"+title+"','"+content+"'