Hi
I have two classes the first inherits the second which should retrieve records of a table found in an oracle database as a jtable but it display table header only!

can anyone help me to know the problem please ?

import java.sql.*;
import java.util.*;
import javax.swing.table.*;


public class ResultSetTableModel extends AbstractTableModel
{
private Connection connection;
private Statement statement;
private ResultSet resultSet;
private ResultSetMetaData metaData;
private int numberOfRows;
private boolean connectedToDatabase=false;
public ResultSetTableModel (String driver,String url,String query)throws
SQLException,ClassNotFoundException
{

   Class.forName(driver);
   Connection	connection=DriverManager.getConnection(url,"system","overmars");
	statement=connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
	ResultSet.CONCUR_READ_ONLY);
	connectedToDatabase=true;
	//
	setQuery(query );
}

public Class  getColumnClass(int column)throws IllegalStateException
{
if(!connectedToDatabase)
throw new IllegalStateException("not connected to database");
//determine java class of column
try
{
String className=metaData.getColumnClassName(column+1);
return Class.forName(className);
}
catch(Exception exception)
{
exception.printStackTrace();
}
return Object.class;
}
public int getColumnCount() throws IllegalStateException
{
	if(!connectedToDatabase)
	throw new IllegalStateException(" not connected to database");
	try
	{
		return metaData.getColumnCount();
	}
	catch(SQLException sqlexception)
	{
	sqlexception.printStackTrace();
}
return 0;
}
public String getColumnName(int column) throws IllegalStateException
{
	if(!connectedToDatabase)
	throw new IllegalStateException("not connected to database");
	try {
return metaData.getColumnName(column+1);

}
	catch(SQLException sqlexception)
	{
	sqlexception.printStackTrace();
}

return "";
}
public int getRowCount() throws IllegalStateException
{
	if(!connectedToDatabase)
	throw new IllegalStateException("not connected to database");
return numberOfRows;
}
public Object getValueAt(int row,int column) throws IllegalStateException
{
	if(!connectedToDatabase)
	throw new IllegalStateException("not connected to database");
	try
	{

	resultSet.absolute(row+1);
	return resultSet.getObject(column+1);
}
catch(SQLException sqlexception)
	{
		sqlexception.printStackTrace();
}
return "";
}
public void setQuery(String query) throws IllegalStateException,ClassNotFoundException,
SQLException
{
	if(!connectedToDatabase)
	throw new IllegalStateException("not connected to database");
ResultSet resultSet=statement.executeQuery(query);
metaData= resultSet.getMetaData();
//

numberOfRows=resultSet.getRow();
fireTableStructureChanged();
}
public void disconnectFromDatabase()
{
	try
	{
statement.close();
connection.close();
}
catch(SQLException sqlexception)
	{
		sqlexception.printStackTrace();
}
finally
{
connectedToDatabase=false;

}}}

DislayQueryResults Class

import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
public class DislayQueryResults extends JFrame
{

static final String JDBC_DRIVER="oracle.jdbc.driver.OracleDriver";
static final String DATABASE_URL="jdbc:oracle:thin:@user-1234965d:1521:orcl";
static final String DEFAULT_QUERY="SELECT std_id,std_name from student";
private ResultSetTableModel tableModel;
private JTextArea queryArea;
public DislayQueryResults()
{
super("Student table");
try
{
	JPanel panel =new JPanel();
	panel.setBackground(Color.white);
tableModel=new ResultSetTableModel(JDBC_DRIVER,DATABASE_URL,DEFAULT_QUERY);
queryArea=new JTextArea(DEFAULT_QUERY,3,100);
queryArea.setWrapStyleWord(true);
queryArea.setLineWrap(true);
JScrollPane scrollPane=
new JScrollPane(queryArea,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
JButton submitButton=new JButton("Submit Query");
submitButton.setBackground(Color.blue);
Box box=Box.createHorizontalBox();
box.add(scrollPane);
box.add(submitButton);
JTable resultTable=new JTable(tableModel);
 JTableHeader header = resultTable.getTableHeader();
    header.setBackground(Color.blue);
Container c=getContentPane();
c.setBackground(Color.blue);
c.add(panel);
c.add(box,BorderLayout.NORTH);
c.add(new JScrollPane(resultTable),BorderLayout.CENTER);
submitButton.addActionListener(
	new ActionListener(){
	//pass query to table model
	public void actionPerformed(ActionEvent event)
{
try
{
tableModel.setQuery(queryArea.getText());
queryArea.setText(DEFAULT_QUERY);
}
catch(SQLException sqlException2)
{
JOptionPane.showMessageDialog(null,sqlException2.getMessage(),
"Database error",JOptionPane.ERROR_MESSAGE);
tableModel.disconnectFromDatabase();
System.exit(1);
}
catch(ClassNotFoundException classnotfound)
{
JOptionPane.showMessageDialog(null,classnotfound.getMessage(),
"Database error",JOptionPane.ERROR_MESSAGE);
tableModel.disconnectFromDatabase();
System.exit(1);
}
}
}


);
setSize(500,500);
setVisible(true);
}
catch(ClassNotFoundException classNotFound)
{
JOptionPane.showMessageDialog(null,"oracle driver not found",
"Driver not found",JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
catch(SQLException sqlException)
{
JOptionPane.showMessageDialog(null, sqlException.getMessage(),
"Driver not found",JOptionPane.ERROR_MESSAGE);
tableModel.disconnectFromDatabase();
System.exit(0);
}
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
addWindowListener(new WindowAdapter(){
public void windowClosed(WindowEvent event)
{
tableModel.disconnectFromDatabase();
System.exit(0);
}
}
);
}
public static void main(String args[]) throws SQLException,ClassNotFoundException,NullPointerException
{
new DislayQueryResults();
}
}

Dani AI

Generated

— The JTable header-only symptom usually means the table model reports zero rows or the model's ResultSet is never available when getValueAt runs. Two common mistakes in this pattern are (1) accidentally shadowing the class fields with local variables so the model's connection / resultSet remain null, and (2) reading row count before moving the ResultSet cursor (so getRow() returns 0). Fix both and the table will populate.

Key fixes to apply in the model (concise):

  • Assign the ResultSet and Connection to the class fields (avoid Connection connection = ... and ResultSet resultSet = ... locally).
  • After executing the query move the cursor to the last row before reading the row number.
  • Fire the correct table change event when only data changes.

Example of the essential parts (replace the model's current logic with the equivalent below):

this.resultSet = statement.executeQuery(query);
this.metaData = this.resultSet.getMetaData();
if (this.resultSet.last()) {
  this.numberOfRows = this.resultSet.getRow();
} else {
  this.numberOfRows = 0;
}
fireTableDataChanged();

Extra tips and cautions:

  • Close resultSet, statement, and connection safely (check for null and catch SQLException) and null-out fields after closing to avoid later NPEs.
  • Construct and update Swing components on the Event Dispatch Thread (use SwingUtilities.invokeLater) and run blocking DB work off the EDT (use SwingWorker) so the UI stays responsive.
  • Use fireTableStructureChanged() only if column count or names change; otherwise fireTableDataChanged() is the right call.
  • If debugging, log metaData.getColumnCount() and the computed numberOfRows immediately after the query to confirm the model has rows.

Useful references: JavaDoc for ResultSet and for AbstractTableModel:

What is the problem?
Any suggestion please.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.