Jelle Siderius
Jelle Siderius

Reputation: 176

Delete selected row in database

Why is this not working for me? I keep getting the error:

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

My code:

private void speler_deleteActionPerformed(java.awt.event.ActionEvent evt) {
    int row = tbl_spelers.getSelectedRow();
    int SpelerID = (int) tbl_spelers.getValueAt(row, 0);
    Speler speler = new Speler();

    try {
    DBClass databaseClass = new DBClass();
    Connection connectie = databaseClass.getConnection();
    // NOG ONVEILIG - WACHTEN OP DB SELECT IN DBCLASS!!!
    String deleteQry = "DELETE FROM `Speler` WHERE SpelerID = " + SpelerID + ";";
    ResultSet rs = databaseClass.GetFromDB(deleteQry);
    } catch (SQLException ex) {
        Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex);
    }
}

Upvotes: 2

Views: 2038

Answers (4)

Dark Knight
Dark Knight

Reputation: 8337

executeQuery() method of jdbc is to select records, you can use executeUpdate() for update operations. Please refer to the documentation for the purpose/ intent of each method:

boolean execute()

Executes the SQL statement in this PreparedStatement object, which may be any kind of SQL statement.

ResultSet executeQuery()

Executes the SQL query in this PreparedStatement object and returns the ResultSet object generated by the query.

int executeUpdate()

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

Upvotes: 0

P_Fitz
P_Fitz

Reputation: 839

You need to execute you query using executeUpdate()

Also, you just need to make a slight adjustment to your String deleteQry as follows:

String deleteQry = "DELETE FROM Speler WHERE SpelerID = " + SpelerID;

Hope this helps...

Upvotes: 0

Suresh Atta
Suresh Atta

Reputation: 121998

You have to use excuteUpdate() for delete.

Docs of excuteUpdate()

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.

Where as executeQuery()

Executes the given SQL statement, which returns a single ResultSet object.

Upvotes: 4

Rahul
Rahul

Reputation: 45060

Firstly, you need to use excuteUpdate() for the delete, and next

String deleteQry = "DELETE FROM `Speler` WHERE SpelerID = " + SpelerID + ";";

remove the semi-colon and the "`" which encloses the table name "Speler", from the query.

Upvotes: 0

Related Questions