0

I'm using below code to do checking before data get inserted into MySQL. However, I get error as below

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 'WHERE NOT EXISTS (SELECT * FROM movie_title WHERE title = 'ABC')' at line 1
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)

Here my code

public void checkAndAdd(String movieTitle) throws Exception {
        // TODO Auto-generated method stub
        DatabaseConnection db=new DatabaseConnection();
        Connection connect=db.getConnection();
        String sql="INSERT INTO movie_title(title) VALUES (?) WHERE NOT EXISTS (SELECT * FROM movie_title WHERE title = ?)";
        PreparedStatement ps=connect.prepareStatement(sql);
        ps.setString(1,movieTitle);
        ps.setString(2,movieTitle);
        ps.executeUpdate();
        connect.close();
        ps.close(); 

    }   

Edited

String sql=" IF NOT EXISTS(SELECT * FROM movie_title WHERE title = ?) INSERT INTO movie_title (title) VALUES (?) ";
1
  • 1
    How to find your problem: take the SQL that you are executing in Java, copy it into mysql and run it. When mysql gives you an error, debug your SQL Commented May 12, 2016 at 15:40

1 Answer 1

4

You can't put a WHERE clause in your SQL when you're using the VALUES keyword. Try something like this:

IF NOT EXISTS(SELECT 1 FROM movie_title WHERE title = ?)
    INSERT INTO movie_title (title)
    VALUES (?)

I work with SQL Server mostly, so I'm not sure if that's completely valid for MySQL, but it will be something similar.

Okay, for MySQL it has to be a little different:

INSERT INTO movie_title (title)
SELECT mt.title
FROM
    (SELECT ? title) mt
WHERE NOT EXISTS (
    SELECT 1 FROM movie_title WHERE title = ?)
Sign up to request clarification or add additional context in comments.

6 Comments

you mean String sql="IF NOT EXISTS(SELECT 1 FROM movie_title WHERE title = ?) INSERT INTO movie_title (title) VALUES (?)"; ?
Yes, I just edited the query itself, but you can put it in a string like that.
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 'IF NOT EXISTS(SELECT * FROM movie_title WHERE title = '1') INSERT INTO movie_tit' at line 1
the movieTitle parameter holds 1. What should I change in this line (SELECT 1 FROM movie_title WHERE title = ?)
Like mmcrae said above, try it directly in MySQL and debug from there. The SELECT 1 should be fine, I'm just not completely sure how to use the ? for parameters. Replace the ? with actual values and test it that way first.
|

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.