I am trying to have this code run and delete a certain record in a MySQL database but I get this error:
SQLException: Can not issue data manipulation statements with executeQuery().
SQLState: S1009
VendorError: 0
This is the code I currently have:
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
}
}
I was able to run a select * query no problem, but when I try and run a DELETE query it doesn’t let me. I’ve ran this command in MySQL workbench and it works, it just doesn’t work when I’m using Java.
You use
executeUpdate()for that instead.executeQuery()is only for statements that return data.executeUpdateis for ones that won’t return date (update, insert, delete, and I believe things like adding/dropping tables, constraints, triggers, and the like as well).