Reputation: 992
I am getting a "Cannot Find Symbol" error for rowData, and columnLabels. Is the reason I am getting this error because there is no value yet for the two variables? I figured that once I created arrays they would be null until the function was called with a ResultSet object.
import javax.swing.*;
import java.awt.*;
import java.sql.*;
public class Testing
{
public static JTable getTable(ResultSet rs)
{
//get data from the resultSet using metaData and place into the arrays
try
{
ResultSetMetaData metaData = rs.getMetaData();
int numberOfColumns = metaData.getColumnCount();
int numberOfRows = rs.getRow();
String[] columnLabels = new String[numberOfColumns];
Object[][] rowData = new Object[numberOfRows][numberOfColumns];
for (int column = 0; column < numberOfColumns; column++) {
columnLabels[column]= metaData.getColumnLabel(column + 1);
}//end of for loop
for(int x = 1; x <= numberOfRows; x++)
{
for(int y = 1; y<=numberOfColumns;y++){
rs.absolute(x);
rowData[x][y] = rs.getObject(y);}
}//end of loop
}catch(SQLException sqlException){
sqlException.printStackTrace();
}//end of catch
JTable table = new JTable(rowData,columnLabels);
return table;
}//end of getTable;
}//end of testing
Upvotes: 0
Views: 1672
Reputation: 285405
You've got a scope problem. rowData etc are declared inside of the try block and are visible only in the try block. A possible solution: declare the variable before the try block.
Upvotes: 1