Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

refresh java program at running time

I have java program doing that : fetching data from database and append them into JTable.

when I add data into database, I cant refresh JTable to view the new data and I should to close program and run it again.

how can I refresh/reload the program to view data

like image 861
user1625324 Avatar asked Apr 02 '26 02:04

user1625324


2 Answers

If using DefaultTableModel see the addRow methods. Else simply create a new model and call JTable.setModel(TableModel).

like image 73
Andrew Thompson Avatar answered Apr 08 '26 06:04

Andrew Thompson


Ok, I will just give you a quick solution:

package stack;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;


public class Test {
    JTable table = new JTable();

    public void selectFrom(){
        Connection conn = null;
        Statement st = null;
        ResultSet rs = null;
        DefaultTableModel model=(DefaultTableModel)table.getModel();
        int rc= model.getRowCount();
        for(int i = 0;i<rc;i++){
            model.removeRow(0);
        }
        try{
            Class.forName("org.postgresql.Driver");
            conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/mydb","postgres","123");
            st = conn.createStatement();
            rs = st.executeQuery("select * from mytable");
            while(rs.next()){
                //Populate table
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                st.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    private void insertInto(){
        Connection conn = null;
        PreparedStatement pst = null;
            try{
                Class.forName("org.postgresql.Driver");
                conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/mydb","postgres","123");
                pst = conn.prepareStatement("INSERT INTO mytable(id,first_column) VALUES(?,?)");
                //Insert into table using pst
                pst.execute();

                selectFrom();
            }catch(Exception e){
                e.printStackTrace();
            }finally{
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                try {
                    pst.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
        }
    }
}
like image 37
Branislav Lazic Avatar answered Apr 08 '26 04:04

Branislav Lazic



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!