Cannot perform data manipulation operations with executeQuery ()

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

 executeQuery(query1) executeQuery(query2) 

But I get the following error:

 can not issue data manipulation statements with executeQuery(). 

What does it mean?

+83
java mysql jdbc
Dec 15 '09 at 6:44
source share
8 answers

To manage the data you really need executeUpdate() , not executeQuery() .

Here's an excerpt from executeUpdate() javadoc, which is already an answer in itself:

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

+164
Dec 15 '09 at 6:46
source share

When executing a DML statement, you should use executeUpdate / execute , not executeQuery .

Here is a brief comparison:

executeQueryVSexecuteUpdateVSexecute

+21
Apr 22 '16 at 8:43
source share

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

+15
Dec 15 '09 at 6:47
source share

This is what executeUpdate .

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

+5
Dec 15 '09 at 6:46
source share

If you are using spring loading, just add the @Modifying annotation.

 @Modifying @Query (value = "UPDATE user SET middleName = 'Mudd' WHERE id = 1", nativeQuery = true) void updateMiddleName(); 
+5
Apr 15 '19 at 3:46
source share

This code works for me: I set values ​​with INSERT and get LAST_INSERT_ID () of this value with SELECT; I am using java NetBeans 8.1, MySql and 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;} 
+4
May 17 '17 at 18:29
source share

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

+3
Dec 15 '09 at 6:47
source share

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

For example:

 PreparedStatement pst = connection.prepareStatement(sql); int numRowsChanged = pst.executeUpdate(sql); 
-one
Mar 23 '15 at 20:13
source share



All Articles