private void getColumnsFromDB (Connection connection, String tname) throws SQLException
{
String query = "SELECT * FROM " + tname;
Statement stmt = connection.createStatement();
ResultSet res = stmt.executeQuery (query);
ResultSetMetaData rsmd = res.getMetaData();
int numberOfColumns = rsmd.getColumnCount();
boolean searchable = rsmd.isSearchable (1);
if (searchable)
{
for (int j = 1; j <= numberOfColumns; ++j)
{
Column col = new Column (tname, rsmd, j);
// do something with Column (col);
}
}
}
類列,構造函數:
public Column (String t, ResultSetMetaData rsmd, int j) throws SQLException
{
table = t;
name = rsmd.getColumnName (j);
schema = rsmd.getSchemaName (j);
precision = -1;
try
{
precision = rsmd.getPrecision (j);
}
catch (NumberFormatException nfe)
{
System.err.println ("nfe[gtd]: " + nfe + " " + t.getName() + "." + name);
}
scale = rsmd.getScale (j);
catName = rsmd.getCatalogName (j);
coltype = rsmd.getColumnType (j);
coltypeName = rsmd.getColumnTypeName (j);
int nulling = rsmd.isNullable (j);
nullable = NULLTYP [nulling];
}
我希望它能夠工作,因爲這個代碼是一個更大的代碼庫的一部分,而且很久以前我就使用它。
當前顯示的列標題是什麼?默認的A,B,C ..等等? – 2011-01-29 21:07:45
我沒有任何列,它是所有行... – LuckyLuke 2011-01-29 21:16:02