⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 serializedtablemodel.java

📁 报表设计软件,很好的
💻 JAVA
字号:
/**
 * ========================================================================================
 * JFreeReport Designer : a graphical designer for JFreeReport - a free Java report library
 * ========================================================================================
 *
 * Project Info:  http://www.jfree.org/jfreereport/index.html
 * Project Lead:  Thomas Morgner (taquera@sherito.org);
 *
 * (C) Copyright 2003, by Heiko Evermann (heiko@evermann.de) and Contributors.
 *
 * This library is free software; you can redistribute it and/or modify it under the terms
 * of the GNU Lesser General Public License as published by the Free Software Foundation;
 * either version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along with this
 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 */

package org.jfree.designer.db;

import java.io.Serializable;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;

import org.jfree.report.util.Log;

/**
 * @author Heiko Evermann
 *         <p/>
 *         To change this generated comment edit the template variable "typecomment":
 *         Window>Preferences>Java>Templates. To enable and disable the creation of type
 *         comments go to Window>Preferences>Java>Code Generation.
 */
public final class SerializedTableModel
        extends AbstractTableModel
        implements Serializable
{

  /**
   * Storage for the data.
   */
  private final Object[][] m_resultSetData;
  private final String[] m_columnNames;
  private final Class[] m_columnTypes;

  public SerializedTableModel (final ResultSet rs)
          throws SQLException
  {
    final ResultSetMetaData rsmd = rs.getMetaData();
    final int colcount = rsmd.getColumnCount();
    // save column names
    m_columnNames = new String[colcount];
    m_columnTypes = new Class[colcount];
    for (int i = 0; i < colcount; i++)
    {
      m_columnNames[i] = rsmd.getColumnName(i + 1);
      final String columnType = rsmd.getColumnClassName(i + 1);
      try
      {
        m_columnTypes[i] = Class.forName(columnType);
      }
      catch (Exception e)
      {
        Log.warn("Class " + columnType + " could not be loaded. Defaulting to Object.class");
        m_columnTypes[i] = Object.class;
      }
    }

    final ArrayList resultSetData = new ArrayList();
    while (rs.next())
    {
      final Object[] row = new Object[colcount];
      for (int i = 0; i < colcount; i++)
      {
        final Object val = rs.getObject(i + 1);
        row[i] = (val);
      }
      resultSetData.add(row);
    }

    m_resultSetData = new Object[resultSetData.size()][];
    resultSetData.toArray(m_resultSetData);
  }

  /**
   * Warning, the data is referenced, not copied! Do not modify the arrays afther this
   * point, or you will get trouble.
   *
   * @param m_resultSetData
   * @param m_columnNames
   * @param m_columnTypes
   */
  public SerializedTableModel (final Object[][] m_resultSetData,
                               final String[] m_columnNames, final Class[] m_columnTypes)
  {
    this.m_resultSetData = m_resultSetData;
    this.m_columnNames = m_columnNames;
    this.m_columnTypes = m_columnTypes;
  }

  /**
   * Returns the number of rows in the table model.
   */
  public final int getRowCount ()
  {
    return m_resultSetData.length;
  }

  /**
   * Returns the number of columns in the table model.
   */
  public final int getColumnCount ()
  {
    return m_columnNames.length;
  }

  /**
   * Returns the class of the data in the specified column.
   */
  public final Class getColumnClass (final int columnIndex)
  {
    return m_columnTypes[columnIndex];
  }

  /**
   * Returns the name of the specified column.
   */
  public final String getColumnName (final int columnIndex)
  {
    return m_columnNames[columnIndex];
  }

  /**
   * Returns the data value at the specified row and column.
   */
  public final Object getValueAt (final int row, final int column)
  {
    return m_resultSetData[row][column];
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -