我想有这样的代码运行在一个MySQL数据库中删除某条记录,但我得到这个错误:
SQLException: Can not issue data manipulation statements with executeQuery().
SQLState: S1009
VendorError: 0
这是我目前拥有的代码:
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
}
}
我能够运行SELECT *查询没有问题,但是当我尝试和运行删除查询它不会让我。 我已经跑在MySQL工作台这个命令和它的作品,它只是当我使用Java不起作用。