0

This is a JDBC project. Data from a MySQL database on a WAMP server is displayed in a JTable. I want a user-entered ID on my JSpinner to delete the row with that ID from the table. I made a SQL Query and everything works, but the data on the my JTable doesn't refresh when my query is executed. I click my JNazad button (my back button), and reenter that window so that my JTable shows refreshed data. I don't implemented FireTableModel in my NapraviTablicu method, because its DefaultTableModel, and updates are automatically done with it . I don't know what I did wrong:

public class GUIBDelete extends JFrame{

    private SpinnerModel SM;
    private JSpinner Spinner;
    private JLabel LUnos;
    private JButton BNazad, BIzvrsi;
    private String ID, SqlQuery;
    private Vector NaziviKolona = new Vector();
    private Vector Podaci = new Vector();
    private JTable Tablica=new JTable();
    private JScrollPane ScrollPane;
    private DefaultTableModel model;


    private JTable NapraviTablicu(){
        try {
            String SqlQuery = "SELECT * FROM `nfc_baza`";
            Podaci.clear();
            NaziviKolona.clear();
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://"
                    + "localhost:3306/nfc", "root", "");

            Statement Stat = con.createStatement();
            ResultSet Rez = Stat.executeQuery(SqlQuery);
            ResultSetMetaData md = Rez.getMetaData();
            int columns = md.getColumnCount();
            for (int i = 1; i <= columns; i++) {
                NaziviKolona.addElement(md.getColumnName(i));
            }
            while (Rez.next()) {
                Vector red = new Vector(columns);
                for (int i = 1; i <= columns; i++) {
                    red.addElement(Rez.getObject(i));
                }
                Podaci.addElement(red);
            }
            Rez.close();
            Stat.close();
            con.close();
        } catch (Exception e) {
            System.out.println(e);
        }
        model = new DefaultTableModel(Podaci, NaziviKolona);
        JTable table = new JTable(model);

        return table;

    }

    ActionListener a1 = new ActionListener() {
        public void actionPerformed(ActionEvent a) {
            dispose();
            new GUIIzbornik();
        }
    };


    ActionListener a2 = new ActionListener() {
        public void actionPerformed(ActionEvent a) {
            ID=null;
            SqlQuery = "DELETE FROM `nfc`.`nfc_baza` WHERE `nfc_baza`.`ID` = ";
            IzvrsiQuery();
        }

        private void IzvrsiQuery() {
            Object sp = Spinner.getValue();
            ID = sp.toString();
            SqlQuery=SqlQuery+ID;
            try {
                Class.forName("com.mysql.jdbc.Driver");
                Connection con2 = DriverManager.getConnection(
                        "jdbc:mysql://" + "localhost:3306/nfc", "root", "");
                Statement Stat = con2.createStatement();
                int Rez = Stat.executeUpdate(SqlQuery);
                Stat.close();
                con2.close();
                JOptionPane.showMessageDialog(null, "Uspješno izvrseno!",
                        "Poruka!", JOptionPane.INFORMATION_MESSAGE);

            } catch (Exception e) {
                System.out.println(e);
            }

        }
    };


    GUIBDelete(){
        setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();

        Tablica=NapraviTablicu();
        ScrollPane = new JScrollPane(Tablica);
        c.fill = GridBagConstraints.BOTH;
        c.insets = new Insets(2, 2, 2, 2);
        c.weightx = 0.1;
        c.weighty = 0.1;
        c.gridwidth = 4;
        c.gridheight = 2;
        c.gridx = 0;
        c.gridy = 0;
        add(ScrollPane, c);


        LUnos= new JLabel("<html><br>Unesite ID elementa</br> kojeg želite obrisati:<html>");
        c.gridx = 0;
        c.gridy = 2;
        c.gridwidth = 1;
        c.gridheight = 1;
        add(LUnos, c);

        SM = new SpinnerNumberModel(1, 1, 1000, 1);
        Spinner = new JSpinner(SM);
        c.gridx = 2;
        c.gridy = 2;
        c.gridwidth = 2;
        add(Spinner, c);

        BNazad = new JButton("Nazad");
        c.gridx = 0;
        c.gridy = 3;
        c.gridwidth = 1;
        BNazad.addActionListener(a1);
        add(BNazad, c);

        BIzvrsi = new JButton("Izvrši");
        c.gridx = 2;
        c.gridy = 3;
        BIzvrsi.addActionListener(a2);
        add(BIzvrsi, c);

        setSize(400, 500);
        setTitle("Brisanje podataka");
        setVisible(true);
        setLocationRelativeTo(null);

    }


    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                GUIBDelete i = new GUIBDelete();
            }
        });
    }
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045

1 Answers1

1

It looks like you're deleting a row from a database table, and you want the JTable to reflect the change. In izvrsiQuery() update your TableModel and the JTable will update itself. For example,

...
con2.close();
model.removeRow(((Number)(spinner.getValue())).intValue() - 1);
JOptionPane.showMessageDialog(...);
...

Note that row numbers start at 0, and they must be translated when using a RowSorter.

As an aside, your code will be easier for others to read if you use common Java naming conventions and factor out constants.

Addendum: The example just removes a row by number. You'll have to search your TableModel for the row that corresponds to the deleted ID.

Addendum: Although less practical, you may want to refresh the entire table from the database after a DML operation using setModel(), as shown here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Sorry but u don't understand me. I don't want that my table data change when JSpinner change value. I want that my jtable change data when my query is executed, and that happen when user enter ID of row he want to delete on Jspinner, and after that he click on Jbutton "Izvrsi" (my query is executed), THEN Jtable must change data... – Mensur Duraković Mar 01 '13 at 19:58
  • OK, change `napraviTablicu()` to return a `TableModel` and do `tablica.setModel()` to refresh the table, as shown [here](http://stackoverflow.com/a/8260663/230513). – trashgod Mar 02 '13 at 00:05