开发者

Obtaining metadata from MySQL in Java [duplicate]

This question already has answers here: Closed 12 years ago.

Po开发者_运维知识库ssible Duplicate:

How to get equivalent of ResultSetMetaData without ResultSet

StringBuffer queryBuffer=new StringBuffer("SELECT * FROM "+tableName);
PreparedStatement preparedStatement=getConnection("root", "").prepareStatement(queryBuffer.toString());
ResultSet resultSet=preparedStatement.executeQuery();
ResultSetMetaData resultMetaData=resultSet.getMetaData();

from the upper code I got ResultSetMetaData instance and from that I can get any information about table.

but I don't want to enter select query like above, and want to get information about table.


If you want to get the columns in a table you can do this:

DatabaseMetaData md = connection.getMetaData();
ResultSet rs = dbmd.getColumns(null, null, tableName, "%"); 
while(rs.next()) { 
    String column = rs.getString("COLUMN_NAME"); 
    System.out.println(column); 
} 


You can get table level metadata from various methods on DatabaseMetaData

Connection c = ...;
DatabaseMetaData dmd = c.getMetaData();
ResultSet set = c.getColumns(catalogue, schema, table, columnPattern);

MySQL also supports INFORMATION_SCHEMA tables which you can also query for metadata. See MySQL 5.1 - INFORMATION_SCHEMA Tables

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜