Hi!

My JTable is populated from remote DB. I would like to set column names from the DB table column "of_shortTitle". However, if I run query "select * from OrgFolders" then column names are taken from "of_title" by default. The table model is given below. How could I set column names from the DB table column "of_shortTitle"?. I must somehow fill String[] headers , however how could I do this? Should I use some method setColumnNames()? Thanks a lot!

class QueryTableModel extends AbstractTableModel {
  Vector cache;
  int colCount;
  String[] headers;
  String url; String databasename;
  String login; String pass;
  Connection db;
  static Statement statement;

  public QueryTableModel(String url, String databasename, String login, String pass) {
    cache = new Vector();
    initDB(url, databasename, login, pass);
  }

  public static Statement getStatement() {
      return statement;
  }

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

  public int getColumnCount() {
    return colCount;
  }

  @Override
  public Class getColumnClass(int col) {
      return cache.size()>0 ? getValueAt(0, col).getClass() : Object.class; // null;
  }

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

  public void deleteData() {
    int rows = getRowCount();
    if (rows == 0) {
        return;
    }
    cache.clear();
    fireTableRowsDeleted(0, rows - 1);
  }

  @Override
  public Object getValueAt(int row, int col) {
    Object str = ((Object[]) cache.elementAt(row))[col];
    if (str != null)
        return str;
    else return "";
  }

  public void initDB(String url, String databasename, String login, String pass) {
    try {
      Class.forName("com.mysql.jdbc.Driver");
      db =  (Connection) DriverManager.getConnection("jdbc:mysql://" + url + "/" + databasename + "?useUnicode=true&characterEncoding=UTF8",login, pass);
      statement = (Statement) db.createStatement();
    } catch (Exception e) {
      System.out.println("Could not initialize the database.");
      e.printStackTrace();
    }
  }

  public void setQuery(String q) {
    cache = new Vector();
    try {
      ResultSet rs = statement.executeQuery(q);
      ResultSetMetaData meta = (ResultSetMetaData) 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();
    }
  }
}

I propose to create a new class to work with SQL (from class QueryTableModel move part of sql code)

DataBase db = new DataBase(url, databasename, login, pass);

each query returns "new table model"

QueryTableModel qtm = db.setQuery(String q)

method setQuery(String q) should return

public QueryTableModel setQuery(String q) {
   //... fill cache, headers, (and attrTitle, for future use in method getAttrTitle())
   return new QueryTableModel(cache, headers);
}

method in class DataBase

public List<String> getAttrTitle() {
        return attrTitle;
    }

set "new table model" to jTable

jTable.setModel(qtm);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.