3

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 statements with executeQuery().
SQLState:     S1009
VendorError:  0

This is the code I currently have:

package stringStuff;

import java.io.File;
import java.util.regex.*;
import java.sql.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class REGGY {

    /**
     * @param args
     */

    Connection connection;

    public REGGY() {
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
        } catch (Exception e) {
            System.err.println("Unable to find and load driver");
            System.exit(1);
        }
    }

    private void displaySQLErrors(SQLException e) {
        System.out.println("SQLException: " + e.getMessage());
        System.out.println("SQLState:     " + e.getSQLState());
        System.out.println("VendorError:  " + e.getErrorCode());
    }

    public void connectToDB() {
        try {
            connection = DriverManager
                    .getConnection("the connection works :P");
        } catch (SQLException e) {
            displaySQLErrors(e);
        }
    }

    public void executeSQL() {
        try {
            Statement statement = connection.createStatement();

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



            rs.close();
            statement.close();
            connection.close();
        } catch (SQLException e) {
            displaySQLErrors(e);
        }
    }

    public static void main(String[] args) {

        String cool = new File(
                "/group/a45dea5c-ea09-487f-ba1c-be74b781efb1/Lessons/Hollowbody 5.gif")
                .getName();

        System.out.println(cool);

        REGGY hello = new REGGY();

        hello.connectToDB();
        hello.executeSQL();

        // TODO Auto-generated method stub

    }

}

I was able to run a select * query no problem, but when I try and run a DELETE query it doesn't let me. I've ran this command in MySQL workbench and it works, it just doesn't work when I'm using Java.

0

6 Answers 6

8

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).

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

Comments

7

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.

Comments

3

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

Comments

2

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

Comments

1

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).

Comments

0

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

1 Comment

A permission problem would have resulted in a different exception

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.