为什么这对我不起作用?我一直收到错误:
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);
}
}发布于 2013-10-28 22:57:57
对于删除,您必须使用excuteUpdate()。
执行给定的SQL语句,该语句可以是INSERT、UPDATE或DELETE语句,也可以是不返回任何内容的SQL语句,如SQL语句。
Where as executeQuery()
执行给定的SQL语句,该语句返回单个ResultSet对象。
发布于 2013-10-28 22:58:24
jdbc的executeQuery()方法是选择记录,可以使用executeUpdate()进行更新操作。有关每种方法的用途/意图,请参阅the documentation:
布尔execute()
执行此PreparedStatement对象中的SQL语句,该对象可以是任何类型的SQL语句。
ResultSet executeQuery()
执行此PreparedStatement对象中的SQL查询,并返回查询生成的ResultSet对象。
int executeUpdate()
执行此PreparedStatement对象中的SQL语句,该对象必须是SQL INSERT、UPDATE或DELETE语句;或者是不返回任何内容的SQL语句,如DDL语句。
发布于 2013-10-28 22:59:52
首先,您需要使用excuteUpdate()进行删除,然后
String deleteQry = "DELETE FROM `Speler` WHERE SpelerID = " + SpelerID + ";";从查询中删除semi-colon和包含表名"Speler“的"`”。
https://stackoverflow.com/questions/19638090
复制相似问题