FileDocCategorySizeDatePackage
QueryTableModel.javaAPI DocExample2867Thu Oct 24 20:14:24 BST 2002None

QueryTableModel

public class QueryTableModel extends AbstractTableModel

Fields Summary
Vector
cache
int
colCount
String[]
headers
Connection
db
Statement
statement
String
currentURL
Constructors Summary
public QueryTableModel()

    cache = new Vector();
    new gsl.sql.driv.Driver();
  
Methods Summary
public voidcloseDB()

    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();
    }
  
public intgetColumnCount()

 return colCount; 
public java.lang.StringgetColumnName(int i)

 return headers[i]; 
public intgetRowCount()

 return cache.size();
public java.lang.ObjectgetValueAt(int row, int col)

 
    return ((String[])cache.elementAt(row))[col];
  
public voidinitDB(java.lang.String url)

    try {
      db = DriverManager.getConnection(url);
      statement = db.createStatement();
    }
    catch(Exception e) {
      System.out.println("Could not initialize the database.");
      e.printStackTrace();
    }
  
public voidsetHostURL(java.lang.String url)

    if (url.equals(currentURL)) {
      // same database, we can leave the current connection open
      return;
    }
    // Oops . . . new connection required
    closeDB();
    initDB(url);
    currentURL = url;
  
public voidsetQuery(java.lang.String q)

    cache = new Vector();
    try {
      // Execute the query and store the result set and its metadata
      ResultSet rs = statement.executeQuery(q);
      ResultSetMetaData meta = rs.getMetaData();
      colCount = meta.getColumnCount();

      // Now we must rebuild the headers array with the new column names
      headers = new String[colCount];
      for (int h=1; h <= colCount; h++) {
        headers[h-1] = meta.getColumnName(h);
      }

      // and file the cache with the records from our query.  This would not be
      // practical if we were expecting a few million records in response to our
      // query, but we aren't, so we can do this.
      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); // notify everyone that we have a new table.
    }
    catch(Exception e) {
      cache = new Vector(); // blank it out and keep going.
      e.printStackTrace();
    }