I'm looking for a way to display a java.sql.ResultSet on the screen. preferably built-into java or swing. If neither of those have a decent method that's simple, I'd consider spring.
How?
println("Total number of rows in ResultSet object = "+rowCount); Simply iterate on ResultSet object and increment rowCount to obtain size of ResultSet object in java. rowCount = rs. getRow();
You can get the name of a particular column using the getColumnName() method of the ResultSetMetadata interface. This method accepts an integer value representing the index of a column and returns a String value representing the name of the specified column.
ResultSet is obtained by calling the executeQuery method on Statement instance. Initially, the cursor of ResultSet points to the position before the first row. The method next of ResultSet moves the cursor to the next row. It returns true if there is further row otherwise it returns false.
Loop over the results of you ResultSet and put in into a TableModel.
DefaultTableModel resultSetToTableModel(
DefaultTableModel model,
ResultSet row) throws SQLException
{
ResultSetMetaData meta= row.getMetaData();
if(model==null) model= new DefaultTableModel();
String cols[]=new String[meta.getColumnCount()];
for(int i=0;i< cols.length;++i)
{
cols[i]= meta.getColumnLabel(i+1);
}
model.setColumnIdentifiers(cols);
while(row.next())
{
Object data[]= new Object[cols.length];
for(int i=0;i< data.length;++i)
{
data[i]=row.getObject(i+1);
}
model.addRow(data);
}
return model;
}
Then you can do something like
JOptionPane.showMessageDialog(null,new JScrollPane(new JTable(model)));
Hopt it helps
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With