我得到一個「無法找到符號」錯誤的rowData和columnLabels。是我得到這個錯誤的原因,因爲這兩個變量沒有值嗎?我想,一旦我創建了數組,它們將爲null,直到函數被ResultSet對象調用。ResultSet to JTable - 找不到符號
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
請改善您的代碼格式以發佈到此論壇的代碼。既然你要求志願者來幫助你,那麼不要求你太多讓我們難以做到這一點,對吧? – 2012-04-01 17:24:31