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

时间:2021-01-29 22:45:54

I am using eclipse and mySQL for coding, while inserting the values I received the syntax error.

我正在使用eclipse和mySQL进行编码,同时插入我收到的语法错误的值。

if(!(nameOfConvo.equals(visitorName))){
                    staffConvo = StringUtils.substringAfter(convo, ": ");
                    System.out.println("Staff - " + staffConvo);
                    String staffSql = "INSERT INTO webchatdata" + "(staffConvo)" + "VALUES ('"+ staffConvo+ "')";
                    myStat.executeUpdate(staffSql);
                }
                    else {
                        visitorConvo = StringUtils.substringAfter(convo, ": ");
                        System.out.println("Visitor - " + visitorConvo);
                        String visitorSql = "INSERT INTO webchatdata" + "(visitorConvo)" + "VALUES ('" +visitorConvo+"')";
                        myStat.executeUpdate(visitorSql);
                }

while in mySQL it is printing out some values, it'll only print halfway and display :

在mySQL中,它会打印一些值,但只打印一半并显示:

 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 's in a course that he has interest in, it is likely that he will excel in it.  I' at line 1

Inserting other variables into the database were fine like ex.

将其他变量插入到数据库中很好,就像ex一样。

String timeStampSql = "INSERT INTO conversation" + "(timestamp)" + "VALUES ('" +timeStamp+"')";
            myStat.executeUpdate(timeStampSql);

1 个解决方案

#1


4  

The problem may be with the content you're putting in to SQL.

问题可能在于您要输入到SQL的内容。

Assuming the full string might be something like this.. (You have not provided what the actual input is in this case, so I can only assume)

假设整个字符串是这样的。(在本例中,您没有提供实际的输入,因此我只能假设)

Bob's in a course that he has interest in, it is likely that he will excel in it. I'm writing

鲍勃正在学习一门他感兴趣的课程,很可能他会在这门课上表现出色。我写

Notice that the first and last character are single quotation marks.

注意,第一个和最后一个字符是单引号。

This is breaking your sql insert string, because it will close the string when it reads ' in the text.

这将破坏sql insert字符串,因为当它在文本中读取'时将关闭该字符串。

When you save the string, you need to escape the quotations so the string is not finished incorrectly. Note the backslashes added.

保存字符串时,需要转义引号,这样字符串就不会不正确地结束。注意,反斜杠补充道。

Bob\'s in a course that he has interest in, it is likely that he will excel in it. I\'m writing

Bob\’s在他感兴趣的课程中,很可能他会在这方面做得更好。I \ ' m写作

#1


4  

The problem may be with the content you're putting in to SQL.

问题可能在于您要输入到SQL的内容。

Assuming the full string might be something like this.. (You have not provided what the actual input is in this case, so I can only assume)

假设整个字符串是这样的。(在本例中,您没有提供实际的输入,因此我只能假设)

Bob's in a course that he has interest in, it is likely that he will excel in it. I'm writing

鲍勃正在学习一门他感兴趣的课程,很可能他会在这门课上表现出色。我写

Notice that the first and last character are single quotation marks.

注意,第一个和最后一个字符是单引号。

This is breaking your sql insert string, because it will close the string when it reads ' in the text.

这将破坏sql insert字符串,因为当它在文本中读取'时将关闭该字符串。

When you save the string, you need to escape the quotations so the string is not finished incorrectly. Note the backslashes added.

保存字符串时,需要转义引号,这样字符串就不会不正确地结束。注意,反斜杠补充道。

Bob\'s in a course that he has interest in, it is likely that he will excel in it. I\'m writing

Bob\’s在他感兴趣的课程中,很可能他会在这方面做得更好。I \ ' m写作