我正在尝试从数据库和表中删除数据。我已经制作了三个文件。其中一个用于连接数据库和CRUD,一个用于windowbuilder,最后一个用于数据库的联系信息。但是,当运行该软件并尝试删除行时,我可以从表中删除行,但不能从数据库中删除行。有人能帮助我吗?谢谢!
Rubrica.java(windowbuilder)
public void actionPerformed(ActionEvent e) {
DefaultTableModel dm = (DefaultTableModel) listaContatti.getModel();
if(listaContatti.getSelectedRowCount() == 1) {
dm.removeRow(listaContatti.getSelectedRow());
try {
RubricaBusiness.getInstance().eliminaContatto(listaContatti.getSelectedRow());
} catch (SQLException e1) {
e1.printStackTrace();
}
} else {
if(listaContatti.getSelectedRowCount() == 0) {
JOptionPane.showMessageDialog(null, "Seleziona un elemento nella tabella.");
} else{
JOptionPane.showMessageDialog(null, "Nessun elemento selezionato!");
}
}}
});
RubricaBusiness(连接和CRUD DB)
public void eliminaContatto(int id) throws SQLException {
String sql = "DELETE FROM contatti WHERE id=?";
PreparedStatement ps = getConnection().prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
ps.executeUpdate();
ps.setInt(1, id);
}
错误:
java.sql.SQLException: No value specified for parameter 1
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:964)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:897)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:886)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:860)
at com.mysql.jdbc.PreparedStatement.checkAllParametersSet(PreparedStatement.java:2211)
at com.mysql.jdbc.PreparedStatement.fillSendPacket(PreparedStatement.java:2191)
at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2058)
at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2013)
at com.mysql.jdbc.PreparedStatement.executeLargeUpdate(PreparedStatement.java:5104)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1998)
at it.progetto.rubrica.business.RubricaBusiness.eliminaContatto(RubricaBusiness.java:67)
at it.progetto.rubrica.view.Rubrica$6.actionPerformed(Rubrica.java:262)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6533)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6298)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2746)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
更新SQLEXEPTION(切换),谢谢用户“ mikeb”:
ps.setInt(1, id);
ps.executeUpdate();
用于从数据库删除数据的更新:
public void actionPerformed(ActionEvent e) {
DefaultTableModel dm = (DefaultTableModel) listaContatti.getModel();
int row = listaContatti.getSelectedRow();
int id =(int) listaContatti.getValueAt(row, 0);
dm.removeRow(row);
try {
RubricaBusiness.getInstance().eliminaContatto(id);
} catch (SQLException e1) {
e1.printStackTrace();
}
}
});
您打错电话的顺序。您需要在调用语句之前为语句设置值。切换:
ps.executeUpdate(); // Wrong, because you have not set the id for the statement yet
ps.setInt(1, id); // Useless, because you have already run the statement!
to
ps.setInt(1, id);
ps.executeUpdate();