extends AbstractTableModel (read database table to a JTable) : AbstractTableModel « javax.swing.table « Java by API






extends AbstractTableModel (read database table to a JTable)

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.AbstractTableModel;

public class MainClass extends JFrame {

  JTextField hostField;

  JTextField queryField;

  QueryTableModel qtm = new QueryTableModel();

  public MainClass() {
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setSize(350, 200);

    JTable table = new JTable(qtm);
    JScrollPane scrollpane = new JScrollPane(table);
    JPanel commandPanel = new JPanel();
    commandPanel.setLayout(new GridLayout(3, 2));
    commandPanel.add(new JLabel("Enter the Host URL: "));
    commandPanel.add(hostField = new JTextField());
    commandPanel.add(new JLabel("Enter your query: "));
    commandPanel.add(queryField = new JTextField());
    commandPanel.add(new JLabel("Click here to send: "));

    JButton jb = new JButton("Search");
    jb.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        qtm.setHostURL(hostField.getText().trim());
        qtm.setQuery(queryField.getText().trim());
      }
    });
    commandPanel.add(jb);
    getContentPane().add(commandPanel, BorderLayout.NORTH);
    getContentPane().add(scrollpane, BorderLayout.CENTER);
  }

  public static void main(String args[]) {
    MainClass tt = new MainClass();
    tt.setVisible(true);
  }
}

class QueryTableModel extends AbstractTableModel {
  Vector cache;

  int colCount;

  String[] headers;

  Connection db;

  Statement statement;

  String currentURL;

  public QueryTableModel() {
    cache = new Vector();
  }

  public String getColumnName(int i) {
    return headers[i];
  }

  public int getColumnCount() {
    return colCount;
  }

  public int getRowCount() {
    return cache.size();
  }

  public Object getValueAt(int row, int col) {
    return ((String[]) cache.elementAt(row))[col];
  }

  public void setHostURL(String url) {
    if (url.equals(currentURL)) {
      return;
    }
    closeDB();
    initDB(url);
    currentURL = url;
  }

  public void setQuery(String q) {
    cache = new Vector();
    try {
      ResultSet rs = statement.executeQuery(q);
      ResultSetMetaData meta = rs.getMetaData();
      colCount = meta.getColumnCount();

      headers = new String[colCount];
      for (int h = 1; h <= colCount; h++) {
        headers[h - 1] = meta.getColumnName(h);
      }

      while (rs.next()) {
        String[] record = new String[colCount];
        for (int i = 0; i < colCount; i++) {
          record[i] = rs.getString(i + 1);
        }
        cache.addElement(record);
      }
      fireTableChanged(null);
    } catch (Exception e) {
      cache = new Vector();
      e.printStackTrace();
    }
  }

  public void initDB(String url) {
    try {
      Class.forName("your driver class");
      db = DriverManager.getConnection(url);
      statement = db.createStatement();
    } catch (Exception e) {
      System.out.println("Could not initialize the database.");
      e.printStackTrace();
    }
  }

  public void closeDB() {
    try {
      if (statement != null) {
        statement.close();
      }
      if (db != null) {
        db.close();
      }
    } catch (Exception e) {
      System.out.println("Could not close the current connection.");
      e.printStackTrace();
    }
  }
}

           
       








Related examples in the same category

1.extends AbstractTableModel (Custom class type)
2.extends AbstractTableModel
3.AbstractTableModel: fireTableRowsUpdated(int firstRow, int lastRow)