解决了 - 从数据库和自表中删除行。

问题描述 投票:-1回答:1

我想从数据库和表中删除数据,我做了3个文件,一个用于连接数据库和CRUD,一个用于windowbuilder,最后一个用于数据库的联系信息。一个用于连接数据库和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);

}

ERROR.UPDATE FOR SQLEXEPTION (switch)感谢用户 "mikeb":

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)

UPDATE FOR SQLEXEPTION (switch) thanks by user "mikeb":

ps.setInt(1, id);  
ps.executeUpdate();

UPDATE for delete data from Database:

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();
                    }
                }
            });
java mysql crud sql-delete delete-row
1个回答
1
投票

你调用东西的顺序不对。你需要在调用之前设置语句的值。Switch:

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!

ps.setInt(1, id);
ps.executeUpdate();
© www.soinside.com 2019 - 2024. All rights reserved.