大家帮忙!我想编写一个表格模版用于将任意ResuleSet放入JTable中,而该对象构造函数需要接收ResultSet
只要该类被实例化就可以直接将数据填入表格。
现在代码写出来了!可是调试过程中却没有任何效果。(连接类以及界面类没有问题)import javax.swing.JTable;
import java.sql.*;public class JTableModel extends JTable {  /*创建接收数据库记录集的JTable模版*/
    private ResultSet rs = null;
    private int row = 0;
    private int col = 0;
    private String[] head = null;
    private String[][] content = null;
    private int count = 0;
    public JTableModel(String[][] content,String [] head){  
        super(content,head); /*调用JTableModel类的父类JTable的构造方法*/
    }
    public JTableModel(ResultSet rs) {/*此构造方法用于接收ResultSet结果集,并将结果集放入表格中*/
        try {
            this.rs = rs;
            rs.last();
            row = rs.getRow(); /*分别获得记录集的行数,列数*/
            col = rs.getMetaData().getColumnCount();
            head = new String[col];
            content = new String[row][col];
            for (int i = 0; i < col; i++) {
                head[i] = rs.getMetaData().getColumnName(i + 1); /*获取记录集中的表头信息*/
            }
            rs.first();
            while (rs.next()) {
                for (int i = 0; i < col; i++) {
                    content[count][i] = rs.getString(i + 1); /*获取记录集中的内容信息*/
                }
                count++;
            }
/*this(content,head);这句话必须放在构造方法的第一句,郁闷啊*/             new JTableModel(content,head);  /*调用该类中另一构造方法,将参数传递给另一构造方法*/
        } catch (Exception e) {
            e.printStackTrace();
        } 
    }
}

解决方案 »

  1.   

    看看http://www.finereport.com 不仅做到了,而且做得很完美啊
      

  2.   

    哈哈哈,你只是在投机取巧啊,你认为这样写构造函数就能绕开super语句必须写在第一行的规则么?其实就算你不把super()写在第一行(甚至你就算不写),类也会在最开始自动调用父类的构造函数创建返回的生成对象,所以编译器之所以要求super必须写在第一行,使为了形式上也要与实质保持一致。所以你最后用new JTableModel(content,head)生成的对象,只是你的一个刚生成的对象再生成的新对象,而且由于没有相关引用,没多久它就被JVM作为垃圾回收了。
      

  3.   

    错了错了拉,你先把table new出来,在从ResultSet里取数据, 再table.getModel()得到model后,再用model.setValueAt()设table的值就可以了
      

  4.   

    /**
       @version 1.1 2004-08-22
       @author Cay Horstmann
    */import com.sun.rowset.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import java.sql.*;
    import java.util.*;
    import javax.swing.*;
    import javax.swing.table.*;
    import javax.sql.rowset.*;/**
       This program shows how to display the result of a 
       database query in a table.
    */
    public class ResultSetTable
    {  
       public static void main(String[] args)
       {  
          JFrame frame = new ResultSetFrame();
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setVisible(true);
       }
    }/**
       This frame contains a combo box to select a database table
       and a table to show the data stored in the table
    */
    class ResultSetFrame extends JFrame
    {  
       public ResultSetFrame()
       {  
          setTitle("ResultSet");
          setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);      /* find all tables in the database and add them to
             a combo box
          */      tableNames = new JComboBox();
          tableNames.addActionListener(new
             ActionListener()
             {
                public void actionPerformed(ActionEvent event)
                {
                   try
                   {  
                      if (scrollPane != null) remove(scrollPane);
                      String tableName = (String) tableNames.getSelectedItem();
                      if (rs != null) rs.close();
                      String query = "SELECT * FROM " + tableName;
                      rs = stat.executeQuery(query);
                      if (scrolling)
                         model = new ResultSetTableModel(rs);
                      else
                      {
                         CachedRowSet crs = new CachedRowSetImpl();
                         crs.populate(rs);
                         model = new ResultSetTableModel(crs);
                      }                  JTable table = new JTable(model);
                      scrollPane = new JScrollPane(table);
                      add(scrollPane, BorderLayout.CENTER);
                      validate();
                   }            
                   catch (SQLException e)
                   {  
                      e.printStackTrace();
                   }
                }
             });
          JPanel p = new JPanel();
          p.add(tableNames);
          add(p, BorderLayout.NORTH);      try
          {  
             conn = getConnection();
             DatabaseMetaData meta = conn.getMetaData();
             if (meta.supportsResultSetType(ResultSet.TYPE_SCROLL_INSENSITIVE))
             {
                scrolling = true;
                stat = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
                   ResultSet.CONCUR_READ_ONLY);
             }
             else
             {
                stat = conn.createStatement();
                scrolling = false;
             }
             ResultSet tables = meta.getTables(null, null, null, new String[] { "TABLE" });
             while (tables.next())
                tableNames.addItem(tables.getString(3));
             tables.close();
          }
          catch (IOException e)
          {  
             e.printStackTrace();
          }
          catch (SQLException e)
          {  
             e.printStackTrace();
          }      addWindowListener(new
             WindowAdapter()
             {
                public void windowClosing(WindowEvent event)
                {
                   try
                   {
                      if (conn != null) conn.close();
                   }
                   catch (SQLException e)
                   {
                      e.printStackTrace();
                   }              
                }
             });
       }   /**
          Gets a connection from the properties specified in
          the file database.properties.
          @return the database connection
        */
       public static Connection getConnection()
          throws SQLException, IOException
       {  
          Properties props = new Properties();
          FileInputStream in = new FileInputStream("database.properties");
          props.load(in);
          in.close();      String drivers = props.getProperty("jdbc.drivers");
          if (drivers != null) System.setProperty("jdbc.drivers", drivers);
          String url = props.getProperty("jdbc.url");
          String username = props.getProperty("jdbc.username");
          String password = props.getProperty("jdbc.password");      return DriverManager.getConnection(url, username, password);
       }   private JScrollPane scrollPane;
       private ResultSetTableModel model;
       private JComboBox tableNames;
       private ResultSet rs;
       private Connection conn;
       private Statement stat;
       private boolean scrolling;   private static final int DEFAULT_WIDTH = 400;
       private static final int DEFAULT_HEIGHT = 300;
    }/** 
       This class is the superclass for the scrolling and the
       caching result set table model. It stores the result set
       and its metadata.
    */
    class ResultSetTableModel extends AbstractTableModel
    {  
       /**
          Constructs the table model.
          @param aResultSet the result set to display.
       */
       public ResultSetTableModel(ResultSet aResultSet)
       {  
          rs = aResultSet;
          try
          {  
             rsmd = rs.getMetaData();
          }
          catch (SQLException e)
          {  
             e.printStackTrace();
          }
       }   public String getColumnName(int c)
       {  
          try
          {  
             return rsmd.getColumnName(c + 1);
          }
          catch (SQLException e)
          {  
             e.printStackTrace();
             return "";
          }
       }   public int getColumnCount()
       {  
          try
          {  
             return rsmd.getColumnCount();
          }
          catch (SQLException e)
          {  
             e.printStackTrace();
             return 0;
          }
       }   public Object getValueAt(int r, int c)
       {  
          try
          {  
             rs.absolute(r + 1);
             return rs.getObject(c + 1);
          }
          catch(SQLException e)
          {  
             e.printStackTrace();
             return null;
          }
       }   public int getRowCount()
       {  
          try
          {  
             rs.last();
             return rs.getRow();
          }
          catch(SQLException e)
          {  
             e.printStackTrace();
             return 0;
          }
       }   private ResultSet rs;
       private ResultSetMetaData rsmd;
    }