Hello Friends...
Following is the code that i write to convert the Data returned from the ResultSet of an SQL query, to be filled into a JTable.
This code works fine for the normal query....but in the case of a SQL query containing a UNION clause For eg. :
SELECT * FROM Table_1
UNION
SELECT * FROM Table_2
it only returns the results of the first part of the above query..i.e. for
SELECT * FROM Table_1
Code:/* * JDBCTableModel.java * * Created on March 1, 2006, 8:51 PM * * To change this template, choose Tools | Template Manager * and open the template in the editor. */ package tradetrakker; import java.sql.*; import javax.swing.table.AbstractTableModel; /** * Class for generating a JTableModel for a given Relational Database * @author Vaibhav Pingle */ public class JDBCTableModel extends AbstractTableModel { private int colCount = 0; private int rowCount = 0; /** * Integer to store rowData */ private Object rowData[][]; /** * Integer to store colData */ private String columnNames[]; /** * Creates a new JDBCTableModel with * @param rs - ResultSet Object */ public JDBCTableModel(ResultSet rs) throws SQLException { ResultSetMetaData rsmd = rs.getMetaData(); colCount = rsmd.getColumnCount(); columnNames = new String[colCount]; int i=0, j=0; while(i<rsmd.getColumnCount()) { columnNames[i] = rsmd.getColumnName(i+1); i++; } rowCount = 0; while(rs.next()) rowCount++; rowData = new Object[rowCount][colCount]; i=0; rs.beforeFirst(); while(rs.next()) { j=1; while(j <= colCount) { Object obj = rs.getObject(j); rowData[i][j-1] = obj; j++; } i++; } } /** * Returns the object located at (rowIndex, columnIndex) * @param rowIndex - Integer value to select row * @param columnIndex - Integer value to select column * @return the value at (rowIndex, columnIndex) */ public Object getValueAt(int rowIndex, int columnIndex) { return rowData[rowIndex][columnIndex]; } /** * Returns the Number of Rows in the JTable. * @return the number of rows. */ public int getRowCount() { return rowCount; } /** * Returns the Number of Columns in the JTable. * @return the number of Columns. */ public int getColumnCount() { return colCount; } /** * Returns the Name of the Column at index. * @param column - Integer value to select Column. * @return the name of the Column. */ public String getColumnName(int column) { return columnNames[column]; } }
Please tell me if there are any changes required in this above code OR if there is any SQL driver problem...If its a driver problem, then please tell me what i am suppose to do (I am using MS Access as my database)


Reply With Quote


Bookmarks