С помощью приведенного ниже кода я пытаюсь удалить некоторые строки из базы данных (Javadb).
system.out.println () показывает правильные строки, однако они остаются в базе данных, когда я проверяю ее послеthis.
Я пытался использовать 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);
}
}
}
}