Trouble executing a MySQL delete statement in Java

后端 未结 6 1804
旧时难觅i
旧时难觅i 2021-01-14 12:41

I am trying to have this code run and delete a certain record in a MySQL database but I get this error:

SQLException: Can not issue data manipulation stateme         


        
相关标签:
6条回答
  • 2021-01-14 12:59

    Change

    ResultSet rs = statement.executeQuery("DELETE FROM content_resource WHERE RESOURCE_ID LIKE '%Hollow%'");
    

    To

    int deletedRows = statement.executeUpdate("DELETE FROM content_resource WHERE RESOURCE_ID LIKE '%Hollow%'");
    

    As others have said, executeQuery() should be used for statements that return data, typically a select statement. For insert / update / delete statements you should use executeUpdate() instead.

    0 讨论(0)
  • 2021-01-14 13:01

    Use execute instead of executeQuery.

    As far as I know, executeQuery must be used if you are executing a query that returns a resultset (select for example).

    0 讨论(0)
  • 2021-01-14 13:05

    To execute a DML statement (insert, create or delete), you must use executeUpdate(). Not executeQuery().

    0 讨论(0)
  • 2021-01-14 13:16

    You use executeUpdate() for that instead.

    executeQuery() is only for statements that return data. executeUpdate is for ones that won't return date (update, insert, delete, and I believe things like adding/dropping tables, constraints, triggers, and the like as well).

    0 讨论(0)
  • 2021-01-14 13:19

    Use executeUpdate instead of executeQuery. JDBC is bummed because the delete statement does not return a record set, as executeQuery expects.

    0 讨论(0)
  • 2021-01-14 13:21

    Ensure that you have set permissions for DELETE statements. Certain users will have certain commands disallowed for security purposes.

    0 讨论(0)
提交回复
热议问题