一碗白菜豆腐
2019-05-05 23:21
采纳率: 44.4%
浏览 330
已采纳

Http servlet中使用hibernate无法添加数据到mysql

public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html;charset=utf-8");
        request.setCharacterEncoding("utf-8");//这里是设置一下编码格式

        Configuration cfg=new AnnotationConfiguration().configure();
        SessionFactory sessionfactory=cfg.buildSessionFactory();
        Session session=sessionfactory.openSession();
        Transaction tx=session.beginTransaction();

        String _index= request.getParameter("index");
        int _index1=Integer.parseInt(_index);
        String  _content= request.getParameter("content");
        System.out.println(_content);
        String  _des= request.getParameter("des");
        String  _style= request.getParameter("style");
        String  _menu= request.getParameter("menu");
        String  _campus= request.getParameter("campus");
        String  _condition= request.getParameter("condition");
        String  _wxNumber= request.getParameter("wxNumber");
        int _wxNumber1 = Integer.parseInt(_wxNumber);
        String  _phoneNumber= request.getParameter("phoneNumber");
        int _phoneNumber1 = Integer.parseInt(_phoneNumber);
        String  _date= request.getParameter("date");
        String  _images= request.getParameter("images");
        String  _userinfoAvatarUrl= request.getParameter("userinfoAvatarUrl");
        String  _userinfoNickName= request.getParameter("userinfoNickName");

        Topic topic =new Topic();

        topic.setOpenid(_openid);
        topic.setContent( _content);
        topic.setIndex(_index1);
        topic.setDes(_des);
        topic.setDate(_date);
        topic.setImages(_images);
        topic.setStyle(_style);
        topic.setMenu(_menu);
        topic.setCampus(_campus);
        topic.setCondition(_condition);
        topic.setWxNumber(_wxNumber1);
        topic.setPhoneNumber(_phoneNumber1);
        topic.setUserinfoNickName(_userinfoNickName);
        topic.setUserinfoAvatarUrl(_userinfoAvatarUrl);

        session.save(topic);
        Writer out2=response.getWriter();
        out2.write("success");
        out2.close();
    }

可以打印出数据 但是存不进去
错误如下:::
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'condition, content, date, des, images, index, menu, style, userinfoNickName, use' at line 1
1. at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
at com.mysql.jdbc.Util.getInstance(Util.java:381)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1030)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3515)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3447)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1951)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2101)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2554)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1761)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2046)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1964)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1949)
at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:94)
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:57)
... 30 more

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • 斯科达克 2019-05-06 15:58
    已采纳

    报的是sql语法错误,是因为你建表的使用了mysql的保留字作为字段名。从错误信息中可以看到condition和index字段都是保留字。改一下字段名称吧。

    评论
    解决 无用
    打赏 举报
查看更多回答(3条)

相关推荐 更多相似问题