how to get 开发者_如何学Gothe column names(dynamically without hardcoding the columnheaders) of a table from database using collections in java
What do you use for? Basically you're always able to get table's meta from DB. If you only need for process the result, maybe something like this.
public static void getResultSetMetaData(Connection con) {
try {
String SQL = "SELECT TOP 10 * FROM Person.Contact";
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(SQL);
ResultSetMetaData rsmd = rs.getMetaData();
// Display the column name and type.
int cols = rsmd.getColumnCount();
for (int i = 1; i <= cols; i++) {
System.out.println("NAME: " + rsmd.getColumnName(i) + " " + "TYPE: " + rsmd.getColumnTypeName(i));
}
rs.close();
stmt.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
You can get database columns via interface DatabaseMetaData getColumns()
After reading save names from returned resultset in some collection.
精彩评论