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();
}
}