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
How Can One Detect Airplane Mode on Android
Sending a JSON Http Post Request from Android
Create a New Textview Programmatically Then Display It Below Another Textview
Convert JSONarray to String Array
How to Create the Directory Error
Library to Read/Write Pbxproj/Xcodeproj Files
How to Add a Shortcut Key for a Jbutton in Java
How to Convert a JSON String to a Map<String, String> with Jackson JSON
What Does Statement.Setfetchsize(Nsize) Method Really Do in SQL Server Jdbc Driver
How to Store More Than One String in a Map
Who Sets Response Content-Type in Spring MVC (@Responsebody)
How to Set a Jvm Timezone Properly
Run Piece of Code Contained in a String
Different Between Parseint() and Valueof() in Java
How to Create a Zip File in Java
Why Does Java Implicitly (Without Cast) Convert a 'Long' to a 'Float'