CCSU Spring 2000 CS 407 Java Database Connectivity TableExample.txt Here are some examples of code using swing tables in Java 1.2.2 Swing tables are easiest way to display database results in a Java GUI application, as most of the formatting work is already done for you. But before we get to the code, you need to understand a bit about JTables in order to use them. First, a JTable is just a viewing device or object. The actual table, that is the data, is contained in a TableModel. Contrary to the advice given in "The Java Tutorial" from Sun Microsystems, the easiest way to deal with general data, as any general database query and display application must, is to use Sun's DefaultTableModel class. To display new query results, which will have new numbers of columns and rows, you will actually have to send messages directly to the DefaultTableModel object contained in your JTable. Thus, you will need to reference both a DefaultTableModel variable and a JTable variable. Now, let's go to the code: // variable declarations private DefaultTableModel mdlResult; private JTable tblResult; private JScrollPane scpResult; // in the constructor mdlResult = new DefaultTableModel(4, 2); // 4 rows, 2 cols tblResult = new JTable (mdlResult); // have to put this next in or no horizontal // scroll bar will show up tblResult.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); tblResult.setColumnSelectionAllowed (true); tblResult.setCellSelectionEnabled (true); // have to put the table in a JScrollPane or you can't // scroll back and forth to view the whole table scpResult = new JScrollPane(tblResult); scpResult.setBorder(new LineBorder (Color.blue)); getContentPane ().add (scpResult); scpResult.setBounds (90, 210, 330, 118); // insert new data into the table and display it // first create an array of arrays to hold the data String[] myColumns = {"Name", "Owner", "Species", "Sex", "Birth"}; String[][] myData = { {"Fluffy", "Mary", "Cat", "F", "97-4-9"}, {"Bowser", "Sam", "Dog", "M", "95-11-2"}, {"Fluffy", "Mary", "Cat", "F", "97-4-9"}, {"Bowser", "Sam", "Dog", "M", "95-11-2"}, {"Fluffy", "Mary", "Cat", "F", "97-4-9"}, {"Bowser", "Sam", "Dog", "M", "95-11-2"}, {"Fluffy", "Mary", "Cat", "F", "97-4-9"}, {"Bowser", "Sam", "Dog", "M", "95-11-2"} }; // you can also create a Vector of Vectors to hold the data, // an example will be given later // the easiest way to actually insert the data is to create a // new data vector for the TableModel mdlResult.setDataVector(myData, myColumns); // the new data will automatically be displayed in your table. You // don't need to call mdlResult.fireTableStructureChanged() (which is // kind of the JTable version of repaint()) or any of the other event // notification methods connected with JTables and TableModels. Here is some code to create a Vector of Vectors from a query result set: // variable declarations -- some global, // some local to an appropriate method ResultSet rs; int numRows, numCols; Vector colLabels = new Vector(), data = new Vector(), // a vector of row vectors rowVector; ResultSetMetaData rsmd; try { rsmd = rs.getMetaData(); // number and names of columns numCols = rsmd.getColumnCount(); // build colLabels vector -- remember columns are numbered // -- starting from 1 for (int col = 1; col <= numCols; col++) { colLabels.addElement ( rsmd.getColumnLabel(col) ); } // for // build the data vector while (rs.next()) { rowVector = new Vector(numCols); for (int col = 1; col <= numCols; col++) { rowVector.addElement (rs.getString(col)); } // for data.addElement (rowVector); } // while } // try catch (SQLException e) { System.out.println("SQLException: " + e.getMessage()); System.out.println("SQLState: " + e.getSQLState()); System.out.println("VendorError: " + e.getErrorCode()); return; } // catch // to display the query results in the JTable, the only // other code you have to write is this: // insert the data into the table model mdlResult.setDataVector(data, colLabels); // this is the promised example promised earlier of creating // a Vector of Vectors to hold the data