开发者

how to get the column names(dynamically without hardcoding the columnheaders) of a table from database using collections in java

开发者 https://www.devze.com 2023-01-20 05:18 出处:网络
how to get 开发者_如何学Gothe column names(dynamically without hardcoding the columnheaders)of a table from database using collections in javaWhat do you use for? Basically you\'re always able to get

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消