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个回答

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

根据报错信息,是sql语句不正确,请检查下sql语句,例如字段名称,字段个数,数据与字段是否匹配

显示是sql语句有语法错误
先去看一下你这个Topic实体在hibernate-mapping里面的属性与数据库字段的映射是否有错误
上面没查出问题的话,根据你这个要存的对象,自己手写SQL语句到MYSQL管理器里面是执行一下看看能不能插入数据,会更明确到底错在哪里

另外你这个最后一段最好改成
try{
session.save(topic);
tx.commit();//事务提交
}catch(Exception e){
tx.rollback(); //事务回滚
throw(e);
}finally{
Writer out2=response.getWriter();
out2.write("success");
out2.close();
session.close(); //关闭session
}

session.save(topic);后面是不是少了=》session.getTransaction().commit();

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!