0

My program executes INSERT query. When I run it, I get an error

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 'INSERT INTO words(lang1, lang2, category, lang2_code, user) SELECT 'ahoj', 'hell' at line 1

I tried to print the actual statement to the stdout:

SET @lang:='Angličtina', @categ:='Nová';
INSERT INTO words(lang1, lang2, category, lang2_code, user)
SELECT 'ahoj', 'hello', c.id, l.id, 1 FROM categories c, languages l
WHERE c.name = @categ AND l.name = @lang;

As you can see, the statement is altered in the log. 'hell' instead of 'hello'. When I copy that into the mysql command line and execute, it works just fine so I assume the problem is in the JDBC somewhere.

1
  • 1
    can you share your java code? Commented Jun 15, 2013 at 16:47

1 Answer 1

1

That's not one statement. If you want to use more than one statement at a time, don't use a PreparedStatement but for example addBatch but it seems that here you should simply issue 2 JDBC statements, one after the other.

5
  • what if I need the preparedStatement? Commented Jun 15, 2013 at 16:50
  • 2
    Then use 2 preparedStatement, not one. You can't have a semicolon in the middle of a statement, prepared or not. Commented Jun 15, 2013 at 16:51
  • ok if there is no other option, then I assume this is answer I needed, thanks Commented Jun 15, 2013 at 16:52
  • @dystroy looks like OP is attempting a nested query. How will 2 statements solve that problem?
    – jlordo
    Commented Jun 15, 2013 at 17:04
  • 1
    @jlordo There's a nested query, which is fine, but there's also a first SET statement before that. Commented Jun 15, 2013 at 17:06

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.