0
String sql = "update `library`.`memebers` set 'STATUS'='" + "1" +"' where mem_id = '"+str+"'";

int i = st.executeUpdate(sql);

上面第二行的错误:

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 ''status' = '1' where mem_id = '656597'' at line 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:411)
        at com.mysql.jdbc.Util.getInstance(Util.java:386)
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1052)
        at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3609)
        at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3541)
        at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2002)
        at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2163)
        at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2618)
        at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1749)
        at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1666)
        at Frames.Addmembers.jButton1ActionPerformed(Addmembers.java:181)
        at Frames.Addmembers.access$000(Addmembers.java:21)
        at Frames.Addmembers$1.actionPerformed(Addmembers.java:83)
        at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
        at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
        at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
        at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
        at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
        at java.awt.Component.processMouseEvent(Component.java:6263)
        at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
        at java.awt.Component.processEvent(Component.java:6028)
        at java.awt.Container.processEvent(Container.java:2041)
        at java.awt.Component.dispatchEventImpl(Component.java:4630)
        at java.awt.Container.dispatchEventImpl(Container.java:2099)
        at java.awt.Component.dispatchEvent(Component.java:4460)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4574)
        at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
        at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
        at java.awt.Container.dispatchEventImpl(Container.java:2085)
        at java.awt.Window.dispatchEventImpl(Window.java:2478)
        at java.awt.Component.dispatchEvent(Component.java:4460)
        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)
4

2 回答 2

0

status 是列的名称,因此,它应该是status`status`

实体要么写成普通的,要么用 `` 引用。(反引号,不是撇号。)

除非您为列名使用保留字段名称(您不应该这样做),否则您不必引用字段。

String sql = "update library.memebers set STATUS = 1 where mem_id = " + Integer.valueOf(str);

请注意,我删除了状态和 mem_id 周围的引号。我假设两者都是整数字段,如果是,则不应引用它们的值。整数字段应该有整数值,而不是计算为整数值的字符串。(请注意,尽管您永远不会盲目地假设一个字符串是一个整数并将其连接到一个查询中——始终首先确保它是一个正确的整数)

于 2012-04-03T20:45:46.660 回答
0

单引号中的字段名称。它应该不带引号,或者在后面的单引号内:```:

String sql = "update library.memebers set `STATUS`='1' where mem_id = '"+str+"'";
于 2012-04-03T20:47:27.670 回答