com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException

时间:2023-02-11 20:32:03

Error is:

错误:

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 'book_id = 'DSP123'' 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:2624) at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2127) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2427) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2345) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2330) at Frames.Books.jButton2ActionPerformed(Books.java:424) at Frames.Books.access$300(Books.java:26) at Frames.Books$4.actionPerformed(Books.java:181) 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)

com.mysql.jdbc.exceptions.jdbc4。mysqlsyntaxerror异常:在SQL语法中有一个错误;请检查与您的MySQL服务器版本对应的手册,以便在sun.reflect.NativeConstructorAccessorImpl中使用“book_id = 'DSP123”。newInstance0(本地方法)在sun.reflect.nativeconstructoraccessor. newinstance (NativeConstructorAccessorImpl.java:39)在sun.reflect.delegate . atingaccessorrequest . newinstance(委托构造toraccessor. java:27)在java.lang. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .在com.mysql.jdbc. mysqliot . checkerrorpacket (mysql. java:3609) at com.mysql.jdbc.MysqlIO.sendCommand(mysql. java:2163) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java: 2163) at com.mysql.jdbc. connectionrequest.execsql (connection. java:2624) at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2127) at com.mysql.jdbc. preparedstatement.java21227 . .在框架中,我们将会在一个框架中,在一个框架中,在一个框架中执行一个文件,并在这个框架中执行这个操作(Books.java:424),在框架中,可以访问$300(Books.java:26)。javax. defaultbuttonmodel . fireactionperformed (DefaultButtonModel.java:387)在javax.swing. default . buttonmodel . setpressed (DefaultButtonModel.java:242)在javax.swing. component . processmouseevent(组件.java:6263)在java.awt.Component.processMouseEvent(java. java:3267) at java.awt.Component.processEvent(组件.java:6028) at .java .awt. containessevent(容器.java:2041)。java.awt.Component.dispatchEventImpl(组件.java:4630)在java.awt. componentmpl . dispatchevent(组件.java:4460)在java.awt.LightweightDispatcher.retargetMouseEvent(容器.java:4574) at java.awt.LightweightDispatcher.processMouseEvent(容器.java:4238) at java.awt.LightweightDispatcher.dispatchEvent(容器.java:4168) at java.awt. container . dispatchevent(容器.java:4168)。在java.awt.EventQueue.dispatchEvent(java:4460)中java.awt.EventQueue.dispatchEvent(EventQueue.java:599)在java.awt. eventdispatchthread . pumponeventforfilter (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: 174) at。在java.awt.EventDispatchThread.run java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)(EventDispatchThread.java:122)

Code is:

代码是:

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    String bookid = jTextField2.getText();
    String doi = "";
    Calendar cal = new GregorianCalendar();
    doi = doi + cal.get(Calendar.YEAR) + "-" + cal.get(Calendar.MONTH) + "-" + cal.get(Calendar.DAY_OF_MONTH);
    String sql = "insert into library.issue values ('"+ bookid + "','librarian','"+ username + "','" + doi + "')";
    try
    {
        int i = st.executeUpdate(sql);
        if(i>0)
           JOptionPane.showMessageDialog(null,"BOOK IS ISSUED","SUCCESS",JOptionPane.PLAIN_MESSAGE);
        else
           JOptionPane.showMessageDialog(null,"problem in issuing book","FAILURE",JOptionPane.ERROR_MESSAGE);
        bookid = bookid.toUpperCase();
        String sql1 = "update library.books set status = " + 1 + "where book_id = '" + bookid + "'";
        PreparedStatement pstmt=c.prepareStatement(sql1);
        int j= pstmt.executeUpdate() ;
        //int j = st.executeUpdate(sql1);
    }
    catch(Exception ex)
    {
        ex.printStackTrace();
    }
} 

2 个解决方案

#1


2  

Insert a space before where:

插入空格前:

String sql1 = "update library.books set status = " + 1 
      + " where book_id = '" + bookid + "'";

#2


0  

check once it should be

检查一下。

String sql1 = "update library.books set status =  1 where book_id = '" + bookid + "'";

#1


2  

Insert a space before where:

插入空格前:

String sql1 = "update library.books set status = " + 1 
      + " where book_id = '" + bookid + "'";

#2


0  

check once it should be

检查一下。

String sql1 = "update library.books set status =  1 where book_id = '" + bookid + "'";