Search code examples
javasqlsql-updatesyntax-error

Updating value in DB throws java.sql.SQLSyntaxErrorException


I'm struggling with this supposed SQL Syntax Error. I get this error:

java.sql.SQLSyntaxErrorException: 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

from this code at the line statement.executeUpdate(("UPDATE tasks SET status = " + statusDescription + " WHERE Id = " + taskID));:

public void updateDB(String statusDescription, String taskID) {
    try {
        java.sql.Connection conn = DriverManager.getConnection(connectionUrl, user, password);
        Statement statement = conn.createStatement();
        System.out.println(statusDescription + " = statusDescription");
        System.out.println(taskID + "= taskID");
        statement.executeUpdate(("UPDATE tasks SET status = " + statusDescription + " WHERE Id = " + taskID));
    } catch (SQLException e) {
        e.printStackTrace();
    }
}

Sometimes I also get this error from the same code:

java.sql.SQLSyntaxErrorException: Unknown column 'ea2ef485' in 'where clause'

The important values at this point were "Done = statusDescription" and "ea2ef485-0649-4e63-903b-e283b47dc8b0= taskID". But doing the query with those values afterwards in MySQL Workbench works fine. Just crashes in Eclipse

I tried running the SQL update statement in MySQL Workbench, and it works there (after I set ID to become the primary key in the Workbench. But it still doesnt work in Eclipse). I tried googeling but didnt find any solution to my problem. I also tried to add 2 more brackets to the sql statement, but it made no difference. I tried putting '' around Id like this 'Id' but it didnt change anything. Im quite at a loss at this point.

Any help will be hugely appreciated!


Solution

  • You are probably missing the string separators, try with this code:

    statement.executeUpdate(("UPDATE tasks SET status = '" + statusDescription + "' WHERE Id = " + taskID));
    

    hope it helps, let me know