5

how to get table structre of a database in java?

The Archetypal Paul
  • 41,321
  • 20
  • 104
  • 134
adnan
  • 67
  • 1
  • 2
  • 2
    You need to add context and additional detail to this question. As-is, it's not possible to answer. – Cam Dec 15 '10 at 11:35
  • Actually i want to take backup of sql database. please help in this regard.i-e what will be algorithm,or steps involve to do so? – adnan Dec 15 '10 at 13:11
  • You could use SHOW TABLES, then each result will have a line containing table name. Use SHOW CREATE TABLE to get table creation code. – Felype Jul 04 '13 at 15:18

3 Answers3

13

Use DatabaseMetaData to get the table information.

You can use the getTablexxx() and getColumnxx() methods to get the table information.

Connection conn = DriverManager.getConnection(.....);
DatabaseMetaData dbmd = conn.getMetaData();
dbmd.getxxxx(); 
jmj
  • 237,923
  • 42
  • 401
  • 438
  • And there is ResultSetMetaData: http://download.oracle.com/javase/6/docs/api/java/sql/ResultSetMetaData.html – Tim Büthe Dec 15 '10 at 11:38
  • 3
    +1. This is the official interface, but it is a terrible one. It returns ResultSets that you have to parse. They could have thrown in some proper interfaces for these data structures in one of the JDBC revs. – Thilo Dec 15 '10 at 11:39
-1

If you just care about being able to recreate the table on a different server, you can use SHOW TABLES to get a list of tables, then SHOW CREATE TABLE foo to get the CREATE TABLE command. You might also look at the mysqldump program to see if it better suits your needs.

Joshua

Joshua Martell
  • 7,074
  • 2
  • 30
  • 37
-2

One way is to use hibernate, reverse engineer and generate entity beans according to your existing database.

Shervin Asgari
  • 23,901
  • 30
  • 103
  • 143