使用以下代码,我尝试从数据库(Javadb)中删除一些行。
system.out.println()显示正确的行,但是当我在此之后对其进行检查时,它们仍保留在数据库中。
我尝试使用ps.execute()和ps.executeUpdate()。
public class DeleteListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent ev) {
try {
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
con3 = DriverManager.getConnection("jdbc:derby://localhost:1527/TESTTABLE","me","1234");
con3.commit();
con4 = DriverManager.getConnection("jdbc:derby://localhost:1527/TESTTABLE","me","1234");
con4.commit();
String sql = "DELETE FROM ME.SOMETABLE WHERE DATA1=?";
String sql2 = "DELETE FROM ME.SOMETABLE WHERE DATA2=?";
int row = t.getSelectedRow();
int column = t.getColumnCount();
// might use also without loop
for(int i = 0; i < column; i++) {
row2Delete = t.getValueAt(row, 0);
row2Delete2 = t.getValueAt(row, 1);
}
// printing out the row to delete
System.out.println("Row 1 to delete is: " + " " + row2Delete);
System.out.println("Row 2 to delete is: " + " " + row2Delete2);
// prepare the queries
ps3 = con3.prepareStatement(sql);
ps4 = con3.prepareStatement(sql2);
ps3.setString(1, "+row2Delete+");
ps4.setString(1, "+row2Delete2+");
// executing the command
ps3.execute();
ps4.execute();
//checking the amount of rows
int rowsDeleted = ps3.executeUpdate();
System.out.println("Row deleted: " + " " + rowsDeleted);
if (rowsDeleted > 0) {
System.out.println(" delete successfully!");
} ps3.clearParameters(); ps4.clearParameters();
} catch (SQLException ex) {
Logger.getLogger(DBtest.class.getName()).log(Level.SEVERE, null, ex);
} catch (ClassNotFoundException ex) {
Logger.getLogger(DBtest.class.getName()).log(Level.SEVERE, null, ex);
} finally{try{if(ps3!=null)con3.close(); if(con3!=null)con3.close(); if(ps4!=null)con4.close(); if(con4!=null)con4.close();}
catch (SQLException ex) {
Logger.getLogger(DBtest.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}