Java 无法使用 executeQuery() 发出数据操作语句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1905607/
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-08-12 23:43:11  来源:igfitidea点击:

Cannot issue data manipulation statements with executeQuery()

javamysqljdbc

提问by silverkid

In MySQL I have two tables, tableAand tableB. I am trying to execute two queries:

在 MySQL 中,我有两个表,tableAtableB. 我正在尝试执行两个查询:

executeQuery(query1) 
executeQuery(query2)

But I get the following error:

但我收到以下错误:

can not issue data manipulation statements with executeQuery().

What does this mean?

这是什么意思?

采纳答案by BalusC

To manipulate data you actually need executeUpdate()rather than executeQuery().

要操作您实际需要的数据executeUpdate()而不是executeQuery().

Here's an extract from the executeUpdate()javadoc which is already an answer at its own:

这是executeUpdate()javadoc的摘录,它本身已经是一个答案:

Executes the given SQL statement, which may be an INSERT, UPDATE, or DELETE statement or an SQL statement that returns nothing, such as an SQL DDL statement.

执行给定的 SQL 语句,它可以是 INSERT、UPDATE 或 DELETE 语句,也可以是不返回任何内容的 SQL 语句,例如 SQL DDL 语句。

回答by Carl Smotricz

That's what executeUpdateis for.

executeUpdate就是为了。

Here's a very brief summary of the difference: http://www.coderanch.com/t/301594/JDBC/java/Difference-between-execute-executeQuery-executeUpdate

这是差异的一个非常简短的总结:http: //www.coderanch.com/t/301594/JDBC/java/Difference-between-execute-executeQuery-executeUpdate

回答by Neil N

ExecuteQuery expects a result set. I'm not as familiar with Java/MySQL, but to create indexes you probably want a ExecuteUpdate().

ExecuteQuery 需要一个结果集。我不太熟悉 Java/MySQL,但要创建索引,您可能需要 ExecuteUpdate()。

回答by OMG Ponies

Use executeUpdate()to issue data manipulation statements. executeQuery()is only meant for SELECT queries (i.e. queries that return a result set).

使用executeUpdate()发布数据操作语句。executeQuery()仅用于 SELECT 查询(即返回结果集的查询)。

回答by veloz

Besides executeUpdate() on the parentheses, you must also add a variable to use an SQL statement.

除了括号上的 executeUpdate() 之外,您还必须添加一个变量才能使用 SQL 语句。

For example:

例如:

PreparedStatement pst =  connection.prepareStatement(sql);
int numRowsChanged = pst.executeUpdate(sql);

回答by Jaskey

When executing DML statement , you should use executeUpdate/executerather than executeQuery.

执行 DML 语句时,应使用executeUpdate/execute而不是executeQuery

Here is a brief comparison :

这是一个简短的比较:

executeQueryVSexecuteUpdateVSexecute

执行查询VS执行更新VS执行

回答by Mati

This code works for me: I set values whit an INSERT and get the LAST_INSERT_ID() of this value whit a SELECT; I use java NetBeans 8.1, MySql and java.JDBC.driver

这段代码对我有用:我通过 INSERT 设置值并通过 SELECT 获取该值的 LAST_INSERT_ID();我使用 java NetBeans 8.1、MySql 和 java.JDBC.driver

                try {

        String Query = "INSERT INTO `stock`(`stock`, `min_stock`,   
                `id_stock`) VALUES ("

                + "\"" + p.get_Stock().getStock() + "\", "
                + "\"" + p.get_Stock().getStockMinimo() + "\","
                + "" + "null" + ")";

        Statement st = miConexion.createStatement();
        st.executeUpdate(Query);

        java.sql.ResultSet rs;
        rs = st.executeQuery("Select LAST_INSERT_ID() from stock limit 1");                
        rs.next(); //para posicionar el puntero en la primer fila
        ultimo_id = rs.getInt("LAST_INSERT_ID()");
        } catch (SqlException ex) { ex.printTrace;}

回答by Forrest

If you're using spring boot, just add an @Modifying annotation.

如果您使用的是 spring boot,只需添加一个 @Modifying 注释。

@Modifying
@Query
(value = "UPDATE user SET middleName = 'Mudd' WHERE id = 1", nativeQuery = true)
void updateMiddleName();

回答by Nitin Nanda

For Delete query - Use @Modifyingand @Transactionalbefore the @Querylike:-

对于删除查询 - 使用@Modifying@Transactional之前@Query类似: -

@Repository
public interface CopyRepository extends JpaRepository<Copy, Integer> {

    @Modifying
    @Transactional
    @Query(value = "DELETE FROM tbl_copy where trade_id = ?1 ; ", nativeQuery = true)
    void deleteCopyByTradeId(Integer id);

}

It won't give the java.sql.SQLException: Can not issue data manipulation statements with executeQuery()error.

它不会给出java.sql.SQLException: Can not issue data manipulation statements with executeQuery()错误。

回答by Sunil

@Modifying
@Transactional
@Query(value = "delete from cart_item where cart_cart_id=:cart", nativeQuery = true)
public void deleteByCart(@Param("cart") int cart); 

Do not forget to add @Modifying and @Transnational before @query. it works for me.

不要忘记在@query 之前添加@Modifying 和@Transnational。这个对我有用。

To delete the record with some condition using native query with JPA the above mentioned annotations are important.

要使用带有 JPA 的本机查询删除具有某些条件的记录,上述注释很重要。