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);
}
}
}
类列,Ctor:
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];
}
我希望它可以工作,因为这段代码是一个更大的代码库的一部分,而且很久以前我就使用它了。