Hello , Can anyone please give me a hint on how to use the ResultSet to populate the JTable from the database , I have my ResultSet ready but I'm confused about how to use it , do I have to create a String Array to hold each row , and another to hold the column names , or are there special methods in the ResultSet Class that I can use ?
I created an inner class to be the Table Model for my MyTable class , but here the IDE underlines the columnName,rset variables in the methods ( getColumnCount() , getRowCount() ...) saying : "Cannot find symbol" ...Why so?
class MyTableModel extends AbstractTableModel {
public MyTableModel(){
String columnName;
ResultSet rset = MyTable.useConnAndGetData();
try
{
ResultSetMetaData rsmd = rset.getMetaData();
int numColumns = rsmd.getColumnCount();
// Get the column names; column indices start from 1
for (int i=1; i<numColumns+1; i++) {
columnName = rsmd.getColumnName(i);
// Get the name of the column's table name
//String tableName = rsmd.getTableName(i);
}
}
catch (SQLException e)
{
System.out.println(e.getMessage());
}
}
publicint getColumnCount() {
return columnName.getMetaData();
}
publicint getRowCount() {
return rset.getFetchSize();
}
public String getColumnName(int col) { // confused about this..
return columnName[col];
}
public Object getValueAt(int row, int col) {
return rset[row][col];
}
}
Okay , I got the answer from another thread , but now when I run this code the IDE says "running" and nothing is happening , no excpetions thrown or anything ,
the progress bar just keeps on going , Why is that happening ?
import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.sql.*;
import javax.swing.table.AbstractTableModel;
publicclass MyTable extends JPanel {
public MyTable()
{
final ResultSetTableModel model = new ResultSetTableModel();
JTable myTable = new JTable(model);
myTable.setPreferredScrollableViewportSize(new Dimension(500, 70));
myTable.setFillsViewportHeight(true);
JScrollPane pane = new JScrollPane(myTable);
JPanel mainPanel = new JPanel();
mainPanel.add(pane);
mainPanel.setOpaque(true);
JFrame frame = new JFrame("My Table Demo");
frame.setSize(400, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// frame.setContentPane(new MyTable());
frame.pack();
frame.add(mainPanel);
try
{
model.setResultSet(useConnAndGetData());
}
catch(SQLException e)
{
e.getMessage();
}
}
privatestatic Connection getConnection()
{
Connection con = null;
try
{
String driverName = "oracle.jdbc.driver.OracleDriver";
Class.forName(driverName); //registering JDBC Driver
String serverName = "localhost";
String portNumber = "1521";
String sid = "mydb";
String url = "jdbc:oracle:thin:@" + serverName + ":" + portNumber + ":" + sid;
String user = "fundinfo";
String pw = "tadapps";
con = DriverManager.getConnection(url, user, pw);
}
catch (ClassNotFoundException e) {
System.out.println(e.getMessage());
System.exit(0);
}
catch (SQLException e) {
System.out.println(e.getMessage());
System.exit(0);
}
return con;
}
publicstatic ResultSet useConnAndGetData()
{
Connection con = getConnection();
try
{
Statement stmt = con.createStatement();
String select = "select title,year,price from movie order by year";
ResultSet rows = stmt.executeQuery(select);
return rows;
}
catch(SQLException e) {
System.out.println(e.getMessage());
}
returnnull;
}
publicstaticvoid main (String[] args)
{
try
{
new MyTable().setVisible(true);
}
catch (Exception ex)
{
System.err.println(ex.getMessage());
ex.printStackTrace();
}
}
}
class ResultSetTableModel extends AbstractTableModel {
// Currently displayed result set
private ResultSet rs;
// Associated metadata
private ResultSetMetaData rsMeta;
// Column count
privateint columnCount;
// Column nmaes
privatefinal Vector<String> columnNames = new Vector<String>();
// Vector of rows
privatefinal Vector<Object[]> cache = new Vector<Object[]>();
/** Set new result set */
publicvoid setResultSet(ResultSet rs)
throws SQLException
{
if (this.rs != null)
this.rs.close();
cache.clear();
// 'Cache' some metadata data
rsMeta = rs.getMetaData();
columnCount = rsMeta.getColumnCount();
columnNames.clear();
for(int col = columnCount; col > 0; col--) {
columnNames.add(rsMeta.getColumnName(col));
}
// 'Cache' new resultest data
while(rs.next()) {
Object rowData[] = new Object[columnCount];
for(int col = columnCount; col > 0; col--)
rowData[col - 1] = rs.getObject(col);
cache.add(rowData);
}
// Reload table structure
fireTableStructureChanged();
}
/** Close result set */
publicvoid close() throws SQLException {
rs.close();
}
/** ## TableModel interface ## */
public String getColumnName(int column) {
return columnNames.get(column);
}
/** ## TableModel interface ## */
public Object getValueAt(int rowIndex, int columnIndex) {
Object[] row = cache.get(rowIndex);
return row[columnIndex];
}
/** ## TableModel interface ## */
publicint getRowCount() {
return cache.size();
}
/** ## TableModel interface ## */
publicint getColumnCount() {
return columnCount;
}
}
HeavenBoy wrote:
Okay , I got the answer from another thread , but now when I run this code the IDE says "running" and nothing is happening , no excpetions thrown or anything ,
the progress bar just keeps on going , Why is that happening ?
import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.sql.*;
import javax.swing.table.AbstractTableModel;
publicclass MyTable extends JPanel {
public MyTable()
{
final ResultSetTableModel model = new ResultSetTableModel();
JTable myTable = new JTable(model);
myTable.setPreferredScrollableViewportSize(new Dimension(500, 70));
myTable.setFillsViewportHeight(true);
JScrollPane pane = new JScrollPane(myTable);
JPanel mainPanel = new JPanel();
mainPanel.add(pane);
mainPanel.setOpaque(true);
JFrame frame = new JFrame("My Table Demo");
frame.setSize(400, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// frame.setContentPane(new MyTable());
frame.pack();
frame.add(mainPanel);
try
{
model.setResultSet(useConnAndGetData());
}
catch(SQLException e)
{
e.getMessage();
}
}
privatestatic Connection getConnection()
{
Connection con = null;
try
{
String driverName = "oracle.jdbc.driver.OracleDriver";
Class.forName(driverName); //registering JDBC Driver
String serverName = "localhost";
String portNumber = "1521";
String sid = "mydb";
String url = "jdbc:oracle:thin:@" + serverName + ":" + portNumber + ":" + sid;
String user = "fundinfo";
String pw = "tadapps";
con = DriverManager.getConnection(url, user, pw);
}
catch (ClassNotFoundException e) {
System.out.println(e.getMessage());
System.exit(0);
}
catch (SQLException e) {
System.out.println(e.getMessage());
System.exit(0);
}
return con;
}
publicstatic ResultSet useConnAndGetData()
{
Connection con = getConnection();
try
{
Statement stmt = con.createStatement();
String select = "select title,year,price from movie order by year";
ResultSet rows = stmt.executeQuery(select);
return rows;
}
catch(SQLException e) {
System.out.println(e.getMessage());
}
returnnull;
}
publicstaticvoid main (String[] args)
{
try
{
new MyTable().setVisible(true);
}
catch (Exception ex)
{
System.err.println(ex.getMessage());
ex.printStackTrace();
}
}
}
class ResultSetTableModel extends AbstractTableModel {
// Currently displayed result set
private ResultSet rs;
// Associated metadata
private ResultSetMetaData rsMeta;
// Column count
privateint columnCount;
// Column nmaes
privatefinal Vector<String> columnNames = new Vector<String>();
// Vector of rows
privatefinal Vector<Object[]> cache = new Vector<Object[]>();
/** Set new result set */
publicvoid setResultSet(ResultSet rs)
throws SQLException
{
if (this.rs != null)
this.rs.close();
cache.clear();
// 'Cache' some metadata data
rsMeta = rs.getMetaData();
columnCount = rsMeta.getColumnCount();
columnNames.clear();
for(int col = columnCount; col > 0; col--) {
columnNames.add(rsMeta.getColumnName(col));
}
// 'Cache' new resultest data
while(rs.next()) {
Object rowData[] = new Object[columnCount];
for(int col = columnCount; col > 0; col--)
rowData[col - 1] = rs.getObject(col);
cache.add(rowData);
}
// Reload table structure
fireTableStructureChanged();
}
/** Close result set */
publicvoid close() throws SQLException {
rs.close();
}
/** ## TableModel interface ## */
public String getColumnName(int column) {
return columnNames.get(column);
}
/** ## TableModel interface ## */
public Object getValueAt(int rowIndex, int columnIndex) {
Object[] row = cache.get(rowIndex);
return row[columnIndex];
}
/** ## TableModel interface ## */
publicint getRowCount() {
return cache.size();
}
/** ## TableModel interface ## */
publicint getColumnCount() {
return columnCount;
}
}