Guys,
I want to update the JTABLE with contents of database using swing worker . I have a code below,but it doesnt show the data in table ?
There are no errors, please help me with rewriting the code.
snapshot included..1.jpg
import java.awt.BorderLayout; import java.awt.EventQueue; import java.awt.GridLayout; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.Statement; import java.util.Vector; import java.util.concurrent.ExecutionException; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.SwingWorker; import javax.swing.table.DefaultTableModel; import javax.swing.table.TableModel; public class Ser { static JTable table = new JTable(); public static void main(String[] args) throws Exception { EventQueue.invokeLater(new Runnable() { @Override public void run() { init(); } }); } private static void init() { execTask(); JFrame frame = new JFrame("Random Data Demo"); frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); frame.setSize(600,600); DefaultTableModel model = new DefaultTableModel(); //JTable table = new JTable(model); table.setModel(model); JScrollPane scrollPane = new JScrollPane(table); JPanel panel = new JPanel(new BorderLayout()); //Swingworker mainWorker = new Swingworker(table); // mainWorker.execute(); panel.add(table.getTableHeader(), BorderLayout.PAGE_START); panel.add(scrollPane, BorderLayout.CENTER); //getContentPane().add(panel); frame.setVisible(true); } private static void execTask() { Swingworke mainWorker = new Swingworke(table); mainWorker.execute(); } } class Swingworke extends SwingWorker<TableModel, TableModel> { private final JTable table; public Swingworke(JTable table) { this.table = table; } @Override protected TableModel doInBackground() throws Exception { Vector data = new Vector(); Vector columns = new Vector(); PreparedStatement ps = null; ResultSet rs = null; try { Class.forName("com.mysql.jdbc.Driver").newInstance(); Connection con = DriverManager.getConnection("jdbc:mysql://localhost/moxa?zeroDateTimeBehavior=convertToNull", "root", "root"); String sql = "Select * from alarmval1"; Statement statement = con.createStatement(); ResultSet resultSet = statement.executeQuery(sql); ResultSetMetaData metaData = resultSet.getMetaData(); int columnCount = metaData.getColumnCount(); //store column names for (int i = 1; i <= columnCount; i++) { columns.add(metaData.getColumnName(i)); } columns.ensureCapacity(columnCount); Vector row; while (resultSet.next()) { row = new Vector(columnCount); for (int i = 1; i <= columnCount; i++) { row.add(resultSet.getString(i)); System.out.println(resultSet.getString(i)); } data.add(row); //Debugging } // List.setModel(tableModel); } finally { try { ps.close(); } catch (Exception e) { } try { rs.close(); } catch (Exception e) { } } DefaultTableModel tableModel = new DefaultTableModel(data, columns); return tableModel; } @Override protected void done() { try { TableModel model = get(); table.setModel(model); } catch (InterruptedException | ExecutionException ex) { ex.printStackTrace(); } } }
Thanks...