在 Java 中执行 MySQL 删除语句时遇到问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11726607/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 06:09:28  来源:igfitidea点击:

Trouble executing a MySQL delete statement in Java

javamysqljdbc

提问by Zeratas

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

我正在尝试运行此代码并删除 MySQL 数据库中的某个记录,但出现此错误:

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.

我能够运行 select * 查询没问题,但是当我尝试运行 DELETE 查询时,它不允许我。我已经在 MySQL 工作台中运行了这个命令并且它有效,但当我使用 Java 时它不起作用。

回答by gsilvestrin

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.

正如其他人所说,executeQuery() 应该用于返回数据的语句,通常是 select 语句。对于插入/更新/删除语句,您应该使用 executeUpdate() 代替。

回答by corsiKa

You use executeUpdate()for that instead.

你用executeUpdate()它来代替。

executeQuery()is only for statements that return data. executeUpdateis 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).

executeQuery()仅用于返回数据的语句。executeUpdate用于那些不会返回日期的(更新、插入、删除,我相信添加/删除表、约束、触发器等)。

回答by JB Nizet

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

要执行 DML 语句(插入、创建或删除),您必须使用executeUpdate(). 不是executeQuery()

回答by Andrew Lazarus

Use executeUpdateinstead of executeQuery. JDBC is bummed because the delete statement does not return a record set, as executeQueryexpects.

使用executeUpdate代替executeQuery。JDBC 很郁闷,因为删除语句没有像executeQuery预期的那样返回记录集。

回答by Barranka

Use executeinstead of executeQuery.

使用execute代替executeQuery

As far as I know, executeQuerymust be used if you are executing a query that returns a resultset (selectfor example).

据我所知,executeQuery如果您正在执行返回结果集的查询(select例如),则必须使用。

回答by Kym Linares

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

确保您已设置DELETE语句的权限。出于安全目的,某些用户将禁止某些命令。