Trouble executing a MySQL delete statement in Java

2020-03-26 07:32发布

问题:

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.

回答1:

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



回答2:

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.



回答3:

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



回答4:

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



回答5:

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



回答6:

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



标签: java mysql jdbc