求助!添加数据到数据库为什么一直产生执行数据库异常跳转到出错页面

?????233 2017-05-05 10:55:04

这是我创建的数据库
下面是我的各部分代码:
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 出错 页面



...全文
223 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
?????233 2017-05-06
  • 打赏
  • 举报
回复
解决了,一个好心人的答案还有自己录入时间的格式错误,很开心
?????233 2017-05-05
  • 打赏
  • 举报
回复
期待有人来解答
?????233 2017-05-05
  • 打赏
  • 举报
回复
引用 2 楼 happy002的回复:
value的内容如果是字符型,需要用单引号扩上吧?
'"+title+"','"+content+"'
对的,这是个问题,不过运行完之后还是产生错误,不过已经很感谢了
happy002 2017-05-05
  • 打赏
  • 举报
回复
value的内容如果是字符型,需要用单引号扩上吧?
'"+title+"','"+content+"'

87,994

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧