how to get table structre of a database in java? [closed]
Asked Answered
S

3

5

how to get table structre of a database in java?

Sjambok answered 15/12, 2010 at 11:34 Comment(3)
You need to add context and additional detail to this question. As-is, it's not possible to answer.Fennelly
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?Sjambok
You could use SHOW TABLES, then each result will have a line containing table name. Use SHOW CREATE TABLE <table_name> to get table creation code.Leaf
L
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(); 
Lundeen answered 15/12, 2010 at 11:37 Comment(2)
And there is ResultSetMetaData: download.oracle.com/javase/6/docs/api/java/sql/…Slavin
+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.Glazed
O
-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

Origami answered 15/12, 2010 at 13:56 Comment(0)
A
-2

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

Alurta answered 15/12, 2010 at 11:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.