 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Mahmood Guest
|
Posted: Thu Apr 21, 2005 4:18 pm Post subject: Problem with displaying ResultSet in JTable |
|
|
Hi All,
I am new to java and databases both. So, it may be a very basic
question.
I am trying to display a ResultSet in a JTable. The code I am working
with is downloaded from a site and is pretty general. There is a
chance that I am missing something while I adapt the code for my use.
I proceed as follows:
1. The (experimental) class ResultSetTableModel implements the
TableModel interface.
2. The class ResultSetTableModelFactory uses this class to obtain a
ResultSetTableModel object for a ResultSet returned for a query.
3. The class QueryFrame then is expected to display results taken into
a ResultSetTableModel object by ResultSetTableModelFactory object to a
JTable object.
Problem: I do see the column names (headings) in the table but no
data, and I want to understanding why it is so.
For simplicity sake, I paste only first two classes. The
ResultSetTbaleModel class is supposed to query records, and not to
update them.
import java.sql.*;
import javax.swing.table.*;
import javax.swing.event.*;
/**
* This class takes a JDBC ResultSet object and implements the
TableModel
* interface in terms of it so that a Swing JTable component can
display the
* contents of the ResultSet. Note that it requires a scrollable JDBC
2.0
* ResultSet. Also note that it provides read-only access to the
results
*
*
*
*/
public class ResultSetTableModel implements TableModel
{
ResultSet results; // The ResultSet to interpret
ResultSetMetaData metadata; // Additional information about the
results
int numcols, numrows; // How many rows and columns in the
table
/**
* This constructor creates a TableModel from a ResultSet. It is
package
* private because it is only intended to be used by
* ResultSetTableModelFactory, which is what you should use to
obtain a
* ResultSetTableModel
**/
ResultSetTableModel(ResultSet results) throws SQLException {
this.results = results; // Save the results
metadata = results.getMetaData(); // Get metadata on them
numcols = metadata.getColumnCount(); // How many columns?
results.last(); // Move to last row
numrows = results.getRow(); // How many rows?
}
/**
* Call this when done with the table model. It closes the
ResultSet and
* the Statement object used to create it.
**/
public void close() {
try { results.getStatement().close(); }
catch(SQLException e) {}
}
/** Automatically close when we're garbage collected */
protected void finalize() { close(); }
// These two TableModel methods return the size of the table
public int getColumnCount() { return numcols; }
public int getRowCount() { return numrows; }
// This TableModel method returns columns names from the
ResultSetMetaData
public String getColumnName(int column) {
try {
return metadata.getColumnLabel(column+1);
} catch (SQLException e) { return e.toString(); }
}
// This TableModel method specifies the data type for each column.
// We could map SQL types to Java types, but for this example,
we'll just
// convert all the returned data to strings.
public Class getColumnClass(int column) { return String.class; }
/**
* This is the key method of TableModel: it returns the value at
each cell
* of the table. We use strings in this case. If anything goes
wrong, we
* return the exception as a string, so it will be displayed in
the table.
* Note that SQL row and column numbers start at 1, but TableModel
column
* numbers start at 0.
**/
public Object getValueAt(int row, int column) {
try {
results.absolute(row+1); // Go to the specified
row
Object o = results.getObject(column+1); // Get value of the
column
if (o == null) return null;
else return o.toString(); // Convert it to a
string
} catch (SQLException e) { return e.toString(); }
}
// Our table isn't editable
public boolean isCellEditable(int row, int column) { return false;
}
// Since its not editable, we don't need to implement these
methods
public void setValueAt(Object value, int row, int column) {}
public void addTableModelListener(TableModelListener l) {}
public void removeTableModelListener(TableModelListener l) {}
}
As I understand, getValueAt function updates the table at a typical
(row, col) location. To update full table, perhaps I thought I needed
to append the following code (ideally to class constructor).
Object o = new Object();
results.first();
for(int i = 0; i < numrows; i++) {
for(int j = 0; j < numcols; j++) {
o = getValueAt(i, j);
}
}
But no improvement...
Secondly, my ResultSetTableModelFactory class:
public class ResultSetTableModelFactory
{
Connection connection; // Holds the connection to the database
ResultSetTableModel rstm;
String driverClassName = "oracle.jdbc.driver.OracleDriver";
String dbname = "jdbc:oracle:thin:@lunaleka.cs.bris.ac.uk:1521:cs2000";
String username = "ma4410";
String password = "Khocha68";
/** The constructor method uses the arguments to create db
Connection */
public ResultSetTableModelFactory()
throws ClassNotFoundException, SQLException
{
// Look up the JDBC driver by class name. When the class loads,
it
// automatically registers itself with the DriverManager used in
// the next step.
Class driver = Class.forName(driverClassName);
// Now use that driver to connect to the database
connection = DriverManager.getConnection(dbname, username,
password);
}
/**
* This method takes a SQL query, passes it to the database,
obtains the
* results as a ResultSet, and returns a ResultSetTableModel
object that
* holds the results in a form that the Swing JTable component can
use.
**/
public void setResultSetTableModel(String query)
throws SQLException
{
// If we've called close(), then we can't call this method
if (connection == null)
throw new IllegalStateException("Connection already closed.");
// Create a Statement object that will be used to excecute the
query.
// The arguments specify that the returned ResultSet will be
// scrollable, read-only, and insensitive to changes in the db.
Statement statement =
connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
// Run the query, creating a ResultSet
ResultSet r = statement.executeQuery(query);
// Create and return a TableModel for the ResultSet
rstm = new ResultSetTableModel(r);
}
public ResultSetTableModel getResultSetTableModel()
{
return rstm;
}
/**
* Call this method when done with the factory to close the DB
connection
**/
public void close() {
try { connection.close(); } // Try to close the connection
catch (Exception e) {} // Do nothing on error. At least we
tried.
connection = null;
}
/** Automatically close the connection when we're garbage
collected */
protected void finalize() { close(); }
}
I can paste the QueryFrame class if required.
Grateful for any suggestion and useful ideas,
Mahmood
|
|
| Back to top |
|
 |
Rhino Guest
|
Posted: Thu Apr 21, 2005 4:58 pm Post subject: Re: Problem with displaying ResultSet in JTable |
|
|
"Mahmood" <siddiqi.mahmood (AT) gmail (DOT) com> wrote
| Quote: | Hi All,
I am new to java and databases both. So, it may be a very basic
question.
I am trying to display a ResultSet in a JTable. The code I am working
with is downloaded from a site and is pretty general. There is a
chance that I am missing something while I adapt the code for my use.
I proceed as follows:
1. The (experimental) class ResultSetTableModel implements the
TableModel interface.
2. The class ResultSetTableModelFactory uses this class to obtain a
ResultSetTableModel object for a ResultSet returned for a query.
3. The class QueryFrame then is expected to display results taken into
a ResultSetTableModel object by ResultSetTableModelFactory object to a
JTable object.
Problem: I do see the column names (headings) in the table but no
data, and I want to understanding why it is so.
For simplicity sake, I paste only first two classes. The
ResultSetTbaleModel class is supposed to query records, and not to
update them.
import java.sql.*;
import javax.swing.table.*;
import javax.swing.event.*;
/**
* This class takes a JDBC ResultSet object and implements the
TableModel
* interface in terms of it so that a Swing JTable component can
display the
* contents of the ResultSet. Note that it requires a scrollable JDBC
2.0
* ResultSet. Also note that it provides read-only access to the
results
*
*
*
*/
public class ResultSetTableModel implements TableModel
{
ResultSet results; // The ResultSet to interpret
ResultSetMetaData metadata; // Additional information about the
results
int numcols, numrows; // How many rows and columns in the
table
/**
* This constructor creates a TableModel from a ResultSet. It is
package
* private because it is only intended to be used by
* ResultSetTableModelFactory, which is what you should use to
obtain a
* ResultSetTableModel
**/
ResultSetTableModel(ResultSet results) throws SQLException {
this.results = results; // Save the results
metadata = results.getMetaData(); // Get metadata on them
numcols = metadata.getColumnCount(); // How many columns?
results.last(); // Move to last row
numrows = results.getRow(); // How many rows?
}
/**
* Call this when done with the table model. It closes the
ResultSet and
* the Statement object used to create it.
**/
public void close() {
try { results.getStatement().close(); }
catch(SQLException e) {}
}
/** Automatically close when we're garbage collected */
protected void finalize() { close(); }
// These two TableModel methods return the size of the table
public int getColumnCount() { return numcols; }
public int getRowCount() { return numrows; }
// This TableModel method returns columns names from the
ResultSetMetaData
public String getColumnName(int column) {
try {
return metadata.getColumnLabel(column+1);
} catch (SQLException e) { return e.toString(); }
}
// This TableModel method specifies the data type for each column.
// We could map SQL types to Java types, but for this example,
we'll just
// convert all the returned data to strings.
public Class getColumnClass(int column) { return String.class; }
/**
* This is the key method of TableModel: it returns the value at
each cell
* of the table. We use strings in this case. If anything goes
wrong, we
* return the exception as a string, so it will be displayed in
the table.
* Note that SQL row and column numbers start at 1, but TableModel
column
* numbers start at 0.
**/
public Object getValueAt(int row, int column) {
try {
results.absolute(row+1); // Go to the specified
row
Object o = results.getObject(column+1); // Get value of the
column
if (o == null) return null;
else return o.toString(); // Convert it to a
string
} catch (SQLException e) { return e.toString(); }
}
// Our table isn't editable
public boolean isCellEditable(int row, int column) { return false;
}
// Since its not editable, we don't need to implement these
methods
public void setValueAt(Object value, int row, int column) {}
public void addTableModelListener(TableModelListener l) {}
public void removeTableModelListener(TableModelListener l) {}
}
As I understand, getValueAt function updates the table at a typical
(row, col) location.
|
No, that's incorrect. The getValueAt() method (not function) does what all
getXXX methods do, it returns information to you. It does NOT update
anything. The getValueAt() method returns the data value that is in a
specified row and column.
| Quote: | To update full table, perhaps I thought I needed
to append the following code (ideally to class constructor).
Object o = new Object();
results.first();
for(int i = 0; i < numrows; i++) {
for(int j = 0; j < numcols; j++) {
o = getValueAt(i, j);
}
}
But no improvement...
Okay, I'm confused now. At the top of this post, you said you were having |
trouble *displaying* data. Now you are talking about updating. Which is it?
Or are you trying to do both, updating data and then seeing the updates in
your JTable?
It's hard to help if the problem isn't clearly stated.
| Quote: | Grateful for any suggestion and useful ideas,
Mahmood
|
Rhino
|
|
| Back to top |
|
 |
Mahmood Guest
|
Posted: Fri Apr 22, 2005 9:11 am Post subject: Re: Problem with displaying ResultSet in JTable |
|
|
Rhino, I am sorry I caused confusion by using word 'update'. I was
only making another (hap-hazard) attempt to display (and not update)
data in the table for the first time.
Mahmood
"Rhino" <rhino1 (AT) NOSPAMsympatico (DOT) ca> wrote
| Quote: | "Mahmood" <siddiqi.mahmood (AT) gmail (DOT) com> wrote in message
news:7cc5a279.0504210818.29385d5a (AT) posting (DOT) google.com...
Hi All,
I am new to java and databases both. So, it may be a very basic
question.
I am trying to display a ResultSet in a JTable. The code I am working
with is downloaded from a site and is pretty general. There is a
chance that I am missing something while I adapt the code for my use.
I proceed as follows:
1. The (experimental) class ResultSetTableModel implements the
TableModel interface.
2. The class ResultSetTableModelFactory uses this class to obtain a
ResultSetTableModel object for a ResultSet returned for a query.
3. The class QueryFrame then is expected to display results taken into
a ResultSetTableModel object by ResultSetTableModelFactory object to a
JTable object.
Problem: I do see the column names (headings) in the table but no
data, and I want to understanding why it is so.
For simplicity sake, I paste only first two classes. The
ResultSetTbaleModel class is supposed to query records, and not to
update them.
import java.sql.*;
import javax.swing.table.*;
import javax.swing.event.*;
/**
* This class takes a JDBC ResultSet object and implements the
TableModel
* interface in terms of it so that a Swing JTable component can
display the
* contents of the ResultSet. Note that it requires a scrollable JDBC
2.0
* ResultSet. Also note that it provides read-only access to the
results
*
*
*
*/
public class ResultSetTableModel implements TableModel
{
ResultSet results; // The ResultSet to interpret
ResultSetMetaData metadata; // Additional information about the
results
int numcols, numrows; // How many rows and columns in the
table
/**
* This constructor creates a TableModel from a ResultSet. It is
package
* private because it is only intended to be used by
* ResultSetTableModelFactory, which is what you should use to
obtain a
* ResultSetTableModel
**/
ResultSetTableModel(ResultSet results) throws SQLException {
this.results = results; // Save the results
metadata = results.getMetaData(); // Get metadata on them
numcols = metadata.getColumnCount(); // How many columns?
results.last(); // Move to last row
numrows = results.getRow(); // How many rows?
}
/**
* Call this when done with the table model. It closes the
ResultSet and
* the Statement object used to create it.
**/
public void close() {
try { results.getStatement().close(); }
catch(SQLException e) {}
}
/** Automatically close when we're garbage collected */
protected void finalize() { close(); }
// These two TableModel methods return the size of the table
public int getColumnCount() { return numcols; }
public int getRowCount() { return numrows; }
// This TableModel method returns columns names from the
ResultSetMetaData
public String getColumnName(int column) {
try {
return metadata.getColumnLabel(column+1);
} catch (SQLException e) { return e.toString(); }
}
// This TableModel method specifies the data type for each column.
// We could map SQL types to Java types, but for this example,
we'll just
// convert all the returned data to strings.
public Class getColumnClass(int column) { return String.class; }
/**
* This is the key method of TableModel: it returns the value at
each cell
* of the table. We use strings in this case. If anything goes
wrong, we
* return the exception as a string, so it will be displayed in
the table.
* Note that SQL row and column numbers start at 1, but TableModel
column
* numbers start at 0.
**/
public Object getValueAt(int row, int column) {
try {
results.absolute(row+1); // Go to the specified
row
Object o = results.getObject(column+1); // Get value of the
column
if (o == null) return null;
else return o.toString(); // Convert it to a
string
} catch (SQLException e) { return e.toString(); }
}
// Our table isn't editable
public boolean isCellEditable(int row, int column) { return false;
}
// Since its not editable, we don't need to implement these
methods
public void setValueAt(Object value, int row, int column) {}
public void addTableModelListener(TableModelListener l) {}
public void removeTableModelListener(TableModelListener l) {}
}
As I understand, getValueAt function updates the table at a typical
(row, col) location.
No, that's incorrect. The getValueAt() method (not function) does what all
getXXX methods do, it returns information to you. It does NOT update
anything. The getValueAt() method returns the data value that is in a
specified row and column.
To update full table, perhaps I thought I needed
to append the following code (ideally to class constructor).
Object o = new Object();
results.first();
for(int i = 0; i < numrows; i++) {
for(int j = 0; j < numcols; j++) {
o = getValueAt(i, j);
}
}
But no improvement...
Okay, I'm confused now. At the top of this post, you said you were having
trouble *displaying* data. Now you are talking about updating. Which is it?
Or are you trying to do both, updating data and then seeing the updates in
your JTable?
It's hard to help if the problem isn't clearly stated.
Grateful for any suggestion and useful ideas,
Mahmood
Rhino
|
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|