3

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

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 :

 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.

String timeStampSql = "INSERT INTO conversation" + "(timestamp)" + "VALUES ('" +timeStamp+"')";
            myStat.executeUpdate(timeStampSql);
2
  • 1
    A space is missing between (timestamp) and VALUES . Commented Sep 19, 2017 at 6:34
  • @Berger actually, that's the one he claims worked ... Commented Sep 19, 2017 at 6:35

1 Answer 1

4

The problem may be with the content you're putting in to 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.

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.