【问题标题】:Trouble executing a MySQL delete statement in Java在 Java 中执行 MySQL 删除语句时遇到问题
【发布时间】:2012-07-30 17:35:08
【问题描述】:

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

SQLException: Can not issue data manipulation statements with executeQuery().
SQLState:     S1009
VendorError:  0

这是我目前拥有的代码:

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

    }

}

我能够运行 select * 查询没有问题,但是当我尝试运行 DELETE 查询时它不会让我这样做。我已经在 MySQL 工作台中运行了这个命令,它可以工作,但当我使用 Java 时它就不起作用了。

【问题讨论】:

    标签: java mysql jdbc


    【解决方案1】:

    您使用 executeUpdate() 代替。

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

    【讨论】:

      【解决方案2】:

      改变

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

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

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

      【讨论】:

        【解决方案3】:

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

        【讨论】:

          【解决方案4】:

          使用executeUpdate 代替executeQuery。 JDBC 很沮丧,因为 delete 语句没有像executeQuery 预期的那样返回记录集。

          【讨论】:

            【解决方案5】:

            使用execute 而不是executeQuery

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

            【讨论】:

              【解决方案6】:

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

              【讨论】:

              • 权限问题会导致不同的异常
              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2012-07-19
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2019-11-13
              • 1970-01-01
              相关资源
              最近更新 更多