您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册以获取正确的语法解决方案:

Posted

技术标签:

【中文标题】您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册以获取正确的语法解决方案:【英文标题】:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax Solution: 【发布时间】:2020-10-25 11:17:41 【问题描述】:

数据库管理员:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.JOptionPane;
    


public class DBManager 
    private String connectionString="jdbc:mysql://localhost/freedbtech_technocontrol?autoReconnect=true&useSSL=false";
    private Connection conn=null;
    private Statement st=null;
    private ResultSet rs=null;
    private String out;
    public DBManager() 
        
    
    private void createConnection() 
        try 
            
            conn=DriverManager.getConnection(connectionString,"root","");
            st=conn.createStatement();
         catch (SQLException e) 
            // TODO Auto-generated catch block
            e.printStackTrace();
        
    

    public void addProject(int po,java.sql.Date idate,String POA,String comp,String pd, String pc,java.sql.Date pcd,String r) 
        createConnection();
        
        String query="Insert into project_details(P0#,IssueDate,POAmount,CompanyName,ProjectDescription,ProjectCompletion,ProjectCompletionDate,Remarks) VALUES ("+po+",'"+idate+"','"+POA+"','"+comp+"','"+pd+"','"+pc+"','"+pcd+"','"+r+"')";
        System.out.println(query);
        try 
            st.execute(query);
            JOptionPane.showMessageDialog(null, "Added Project :)");
            st.close();
            conn.close();
         catch (SQLException e) 
            // TODO Auto-generated catch block
            e.printStackTrace();
        
    
    

public static void main(String args[]) 
    DBManager test=new DBManager();


项目详情: 这是访问数据库的部分。

AddButton.addActionListener(new ActionListener() 
            
            @Override
            public void actionPerformed(ActionEvent e) 
                // TODO Auto-generated method stub
                try 
                    db=new DBManager();
                    pon=po.getText();
                    pn=Integer.parseInt(pon);
                    idate=i.getText();
                    java.util.Date dateUtil = DATE_FORMAT.parse(idate);
                    java.sql.Date dateSql = new java.sql.Date(dateUtil.getTime());
                    pa=poa.getText();
                    comp=compName.getText();
                    pd=pdes.getText();
                    pcd=pcdate.getText();
                    java.util.Date dateUtil2 = DATE_FORMAT.parse(pcd);
                    java.sql.Date cdate = new java.sql.Date(dateUtil2.getTime());
                    r=remarks.getText();
                    
                    db.addProject(pn,dateSql, pa,comp, pd, radioResponse,cdate, r);
                 catch (ParseException e1) 
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                
                
            
        );

输出是:

Insert into project_details(P0#,IssueDate,POAmount,CompanyName,ProjectDescription,ProjectCompletion,ProjectCompletionDate,Remarks) VALUES (12312,'1999-09-12','342342sfdsdfsfsd','fsfwe','fsdfsdfs','Yes','1999-09-12','')
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 '' at line 1
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:500)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:481)
    at mysql.connector.java@5.1.38-bin/com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
    at mysql.connector.java@5.1.38-bin/com.mysql.jdbc.Util.getInstance(Util.java:387)
    at mysql.connector.java@5.1.38-bin/com.mysql.jdbc.SQLError.createSQLException(SQLError.java:939)
    at mysql.connector.java@5.1.38-bin/com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3878)
    at mysql.connector.java@5.1.38-bin/com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3814)
    at mysql.connector.java@5.1.38-bin/com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2478)
    at mysql.connector.java@5.1.38-bin/com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2625)
    at mysql.connector.java@5.1.38-bin/com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2547)
    at mysql.connector.java@5.1.38-bin/com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2505)
    at mysql.connector.java@5.1.38-bin/com.mysql.jdbc.StatementImpl.executeInternal(StatementImpl.java:840)
    at mysql.connector.java@5.1.38-bin/com.mysql.jdbc.StatementImpl.execute(StatementImpl.java:740)
    at DBManager.addProject(DBManager.java:37)
    at ProjectDetails$3.actionPerformed(ProjectDetails.java:231)
    at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1967)
    at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2308)
    at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)
    at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262)
    at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:279)
    at java.desktop/java.awt.Component.processMouseEvent(Component.java:6636)
    at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3342)
    at java.desktop/java.awt.Component.processEvent(Component.java:6401)
    at java.desktop/java.awt.Container.processEvent(Container.java:2263)
    at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5012)
    at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2321)
    at java.desktop/java.awt.Component.dispatchEvent(Component.java:4844)
    at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4918)
    at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4547)
    at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4488)
    at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2307)
    at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2762)
    at java.desktop/java.awt.Component.dispatchEvent(Component.java:4844)
    at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:772)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:95)
    at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:745)
    at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:743)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
    at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:742)
    at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

/////////////////////////////////////// ///////////////////////////////////////// ///////////////////////////////////////// ///////////////////////////////////////// ///////////////////////////////////////// ///////////////////////////////////////// /////////////////

【问题讨论】:

【参考方案1】:

您的查询以

开头
String query="Insert into project_details(P0#,IssueDate

ü0# 是无效的列名并且被禁止。

如果那是你的列名,你必须在它周围使用反引号

String query="Insert into project_details(`P0#`,IssueDate

【讨论】:

以上是关于您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册以获取正确的语法解决方案:的主要内容,如果未能解决你的问题,请参考以下文章

SQL 错误:您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册以获取正确的语法

您的 SQL 语法有错误;检查与您的 MySQL 服务器 PYTHON 对应的手册

#1064 - 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册

错误:您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册以获取正确的语法使用

您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册以获取正确的语法解决方案:

“您的 SQL 语法有错误;请检查与您的 MySQL 服务器版本相对应的手册以获取正确的语法错误”Hibernate 4 [重复]