Mossen
Mossen

Reputation: 604

Get information of Mysql table's structure in Java

I am doing a project and I need to find some information of any given table of mysql. I need names of columns and their attributes such if any of them is index or not. I'm doing this in Java, how can I get this piece of info about any table?

thanks

Upvotes: 2

Views: 4368

Answers (2)

Mossen
Mossen

Reputation: 604

I think it's good as an example: http://www.herongyang.com/JDBC/sqljdbc-jar-Column-List.html

Upvotes: 0

Paul Vargas
Paul Vargas

Reputation: 42060

Using JDBC, you can get that with:

String tableNamePattern = "customer";
DatabaseMetaData databaseMetaData = conn.getMetaData();

ResultSet columns = 
       databaseMetaData.getColumns(null, null, tableNamePattern, null);

ResultSet primaryKeys = 
       databaseMetaData.getPrimaryKeys(null, null, tableNamePattern);

ResultSet indexInfo = 
       databaseMetaData.getIndexInfo(null, null, tableNamePattern, false, false);

You can see the information, e.g. using Most simple code to populate JTable from ResultSet.

Upvotes: 2

Related Questions