Ich möchte die Daten in der Datenbank von JTable entfernen. Ich habe 1 jTextField und 1 jButton. Wenn ich also auf diese ausgewählte Zeile in der Tabelle klicke, wird der Primärschlüssel in meinem jTextField nicht gesetzt.Wie entfernt man Daten in der Datenbank von jTable?
Ist es möglich, die Daten in der Datenbank von jtable ohne jTextField und nur eine Schaltfläche zu entfernen?
Heres mein Code
try {
int row = table.getSelectedRow();
String id_ = (table.getModel().getValueAt(row, 1)).toString();
String sql ="SELECT id FROM 'mycredentials.sales' WHERE id= "+id_+"'";
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mycredentials?autoReconnect=true&useSSL=false", username, password);
PreparedStatement pst = (PreparedStatement) connection.prepareStatement(sql);
ResultSet rs = pst.executeQuery();
while (rs.next()) {
jFrame_removeP.setText(rs.getString("id"));
}
} catch (SQLException e1) {
System.err.println(e1);
}
Zufalls-ID-Nummer erscheint in meinem JTextField. Und meine Tabellencode ist:
String name = jFrame_pName.getText().trim();
String price = jFrame_pPrice.getText().trim();
String quantity = jFrame_quantity.getText().trim();
String total = jFrame_total.getText().trim();
String st[] = {name, price, quantity, total};
model.addRow(st);
jFrame_gTotal.setText(Integer.toString(getSum()));
try {
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mycredentials?autoReconnect=true&useSSL=false", username, password);
Statement s = (Statement) connection.createStatement();
String sql = "INSERT INTO mycredentials.sales (name, price, quantity, total) VALUES ('"+jFrame_pName.getText()+"', '" + jFrame_pPrice.getText() + "','"+jFrame_quantity.getText()+"','"+jFrame_total.getText()+"')";
s.executeUpdate(sql);
} catch (SQLException e1) {
System.err.println(e1);
}
und meine Schaltfläche Entfernen ist:
DefaultTableModel model1 = (DefaultTableModel) table_1.getModel();
int selectedRowIndex = table_1.getSelectedRow();
model1.removeRow(selectedRowIndex);
try {
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mycredentials?autoReconnect=true&useSSL=false", username, password);
Statement ps = (Statement) connection.createStatement();
String sql = "DELETE from mycredentials.sales where id ='" + jFrame_removeP.getText() + "'";
ps.executeUpdate(sql);
} catch (Exception ex) {
System.err.println(ex);
}
* "Ist es möglich, die Daten in der Datenbank von jtable ohne jTextField und nur eine Schaltfläche zu entfernen?" * Im Allgemeinen ja. Beginnen Sie damit, ein Pojo zu erstellen, das eine Reihe von Daten darstellt, es wird das Leben einfacher machen, dann müssen Sie nur die Daten aus der Tabelle holen und sie löschen – MadProgrammer