2013-10-28 84 views
2

为什么这不适合我?我不断收到错误:删除数据库中的选定行

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

我的代码:

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); 
    } 
} 
+1

使用executeUpdate – constantlearner

回答

4

你必须使用excuteUpdate()删除

文档的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.

凡为executeQuery()

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

0

的executeQuery()JDBC的方法是选择的记录,你可以使用的executeUpdate()用于更新操作。请参考the documentation每种方法的目的/意图:

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.

0

首先,你需要使用excuteUpdate()进行删除,而旁边

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

删除semi-colon和“`”包围来自查询的表名称“Speler”。

0

您需要在查询中使用executeUpdate()

而且执行,你只需要稍作调整,您的字符串deleteQry如下:

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

希望这有助于...

相关问题