×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
1
Language: Java
Posted by: Minh Pham
Added: Oct 18, 2015 5:36 PM
Views: 9
Display data into table
  1. public void showTableMetaData() throws SQLException {
  2.     stTable = conn.createStatement();
  3.     String classSelected = ComboBox_Class.getSelectedItem().toString();
  4.     String sqlTable = "";
  5.     rsTable = stTable.executeQuery(sqlTable);
  6.     ResultSetMetaData rsMeta = rsTable.getMetaData();
  7.     DefaultTableModel tableModel = new DefaultTableModel();
  8.     for (int i = 1; i <= rsMeta.getColumnCount(); ++i){
  9.         String columnName = rsMeta.getColumnName(i);
  10.         tableModel.addColumn(columnName);
  11.     }
  12.     while (rsTable.next()){
  13.         Object[] rowData = new Object[rsMeta.getColumnCount()];
  14.         for (int i = 0; i < rowData.length; i++){
  15.             rowData[i] = rsTable.getObject(i+1);
  16.         }
  17.         tableModel.addRow(rowData);
  18.     }
  19.     tableView.setModel(tableModel);
  20. }