今天在写一个hibernate的更新操作
String hql="update "+list.get(1)+" set "+columnName+"="+list.get(3)+" where "+list.get(1)+"id="+list.get(0);
Query q=se.createQuery(hql);q.executeUpdate();tr.commit();
se.close();
遇到了如下错误:xception in thread "AWT-EventQueue-0" org.hibernate.exception.SQLGrammarException: could not execute update queryat org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:90)at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)at org.hibernate.hql.ast.exec.BasicExecutor.execute(BasicExecutor.java:110)at org.hibernate.hql.ast.QueryTranslatorImpl.executeUpdate(QueryTranslatorImpl.java:421)at org.hibernate.engine.query.HQLQueryPlan.performExecuteUpdate(HQLQueryPlan.java:283)at org.hibernate.impl.SessionImpl.executeUpdate(SessionImpl.java:1169)at org.hibernate.impl.QueryImpl.executeUpdate(QueryImpl.java:117)at service.UpdataService.update(UpdataService.java:56)at frame.listener.TableListener.tableChanged(TableListener.java:50)at javax.swing.table.AbstractTableModel.fireTableChanged(AbstractTableModel.java:280)at javax.swing.table.AbstractTableModel.fireTableCellUpdated(AbstractTableModel.java:259)at frame.model.TableModel.setValueAt(TableModel.java:56)at javax.swing.JTable.setValueAt(JTable.java:2719)at javax.swing.JTable.editingStopped(JTable.java:4721)at javax.swing.AbstractCellEditor.fireEditingStopped(AbstractCellEditor.java:125)at javax.swing.DefaultCellEditor$EditorDelegate.stopCellEditing(DefaultCellEditor.java:350)at javax.swing.DefaultCellEditor.stopCellEditing(DefaultCellEditor.java:215)at javax.swing.JTable$GenericEditor.stopCellEditing(JTable.java:5475)at javax.swing.plaf.basic.BasicTableUI$Handler.mousePressed(BasicTableUI.java:986)at java.awt.AWTEventMulticaster.mousePressed(AWTEventMulticaster.java:263)at java.awt.Component.processMouseEvent(Component.java:6213)at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)at java.awt.Component.processEvent(Component.java:5981)at java.awt.Container.processEvent(Container.java:2041)at java.awt.Component.dispatchEventImpl(Component.java:4583)at java.awt.Container.dispatchEventImpl(Container.java:2099)at java.awt.Component.dispatchEvent(Component.java:4413)at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4556)at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4217)at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4150)at java.awt.Container.dispatchEventImpl(Container.java:2085)at java.awt.Window.dispatchEventImpl(Window.java:2475)at java.awt.Component.dispatchEvent(Component.java:4413)at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)Caused by: com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: Unknown column '??????é??1' in 'field list'at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936)at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2985)at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1631)at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1723)at com.mysql.jdbc.Connection.execSQL(Connection.java:3283)at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1332)at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1604)at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1519)at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1504)at org.hibernate.hql.ast.exec.BasicExecutor.execute(BasicExecutor.java:101)... 37 more
上网搜了下,和我的错方式又不一样,后来将生成的sql语句放到mysql中去执行才发现原来构造sql语句的时候发生了错误
String hql="update "+list.get(1)+" set "+columnName+"="+list.get(3)+" where "+list.get(1)+"id="+list.get(0);
原来columnName的赋值需要用单引号
String hql="update "+list.get(1)+" set "+columnName+"=’"+list.get(3)+"' where "+list.get(1)+"id="+list.get(0);