Cannot Issue Data Manipulation Statements with Executequery()

Cannot issue data manipulation statements with executeQuery()

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

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

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.

Can not issue data manipulation statements with executeQuery

You have to use this method PreparedStatement#executeUpdate() instead of executeQuery

Executes the SQL statement in this PreparedStatement object, which
must be an SQL Data Manipulation Language (DML) statement, such as
INSERT, UPDATE or DELETE; or an SQL statement that returns nothing,
such as a DDL statement.

java.sql.SQLException: Can not issue data manipulation statements with executeQuery(). 23

You should really consider what @OllieJones says in the comments about using prepared statements. @Rimas already gave you the solution so I will simply provide an example:

Connection con = DriverManager.getConnection(path, Username, Password);

String rGanTz = "UPDATE info SET Firstname = ?, Lastname = ?, Contact = ? " +
"WHERE EDPNO = ?";

PreparedStatement ps = con.prepareStatement(rGanTz);
ps.setString(1, txt_Firstname.getText());
ps.setString(2, txt_Lastname.getText());
ps.setString(3, txt_Contact.getText());
ps.setString(4, txt_Edpno.getText());

ps.executeUpdate();

SQLException: Can not issue data manipulation statements with executeQuery()

Instead of:

stmt.executeQuery(queryAccount);

use:

stmt.executeUpdate(queryAccount);

Can not issue data manipulation statements with executeQuery()

    Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet generatedKeys = null;

try {
connection = m_Connection;
preparedStatement = (PreparedStatement) connection.prepareStatement(qString, Statement.RETURN_GENERATED_KEYS);

// ...

int affectedRows = preparedStatement.executeUpdate();
if (affectedRows == 0) {
throw new SQLException("Creating user failed, no rows affected.");
}

generatedKeys = preparedStatement.getGeneratedKeys();
int id = -1;
if (generatedKeys.next()) {
id = generatedKeys.getInt(1);
id = -1;
} else {
throw new SQLException("Creating user failed, no generated key obtained.");
}
} finally {

}

Spring Jpa Update: Can not issue data manipulation statements with executeQuery()

Use annotation @Modifying.

This will trigger the query annotated to the method as updating query
instead of a selecting one.

From 2.2.6 Modifying queries https://docs.spring.io/spring-data/jpa/docs/1.3.4.RELEASE/reference/html/jpa.repositories.html



Related Topics



Leave a reply



Submit