Get Table and View Names from a Database : DatabaseMetadata « Database « Java Tutorial

Java Tutorial
1. Language
2. Data Type
3. Operators
4. Statement Control
5. Class Definition
6. Development
7. Reflection
8. Regular Expressions
9. Collections
10. Thread
11. File
12. Generics
13. I18N
14. Swing
15. Swing Event
16. 2D Graphics
17. SWT
18. SWT 2D Graphics
19. Network
20. Database
21. Hibernate
22. JPA
23. JSP
24. JSTL
25. Servlet
26. Web Services SOA
27. EJB3
28. Spring
29. PDF
30. Email
31. J2ME
32. J2EE Application
33. XML
34. Design Pattern
35. Log
36. Security
37. Apache Common
38. Ant
39. JUnit
Java
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Tutorial » Database » DatabaseMetadata 
20. 26. 4. Get Table and View Names from a Database
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class Main {
  public static void main(String[] argsthrows Exception {
    Connection conn = getConnection();
    Statement st = conn.createStatement();

    st.executeUpdate("create table survey (id int,myURL CHAR);");
    st.executeUpdate("create view surveyview as (select * from survey);");
    getTables(conn);
   
    st.close();
    conn.close();
  }

  public static void getTables(Connection connthrows Exception {
    String TABLE_NAME = "TABLE_NAME";
    String TABLE_SCHEMA = "TABLE_SCHEM";
    String[] TABLE_AND_VIEW_TYPES = {"TABLE","VIEW"};
    DatabaseMetaData dbmd = conn.getMetaData();

    ResultSet tables = dbmd.getTables(null, null, null, TABLE_AND_VIEW_TYPES );
    while (tables.next()) {
      System.out.println(tables.getString(TABLE_NAME));
          System.out.println(tables.getString(TABLE_SCHEMA));
    }
  }

  private static Connection getConnection() throws Exception {
    Class.forName("org.hsqldb.jdbcDriver");
    String url = "jdbc:hsqldb:mem:data/tutorial";

    return DriverManager.getConnection(url, "sa""");
  }
}
20. 26. DatabaseMetadata
20. 26. 1. Database Metadata
20. 26. 2. Get Table Names from a Database
20. 26. 3. Get View Names from a Database
20. 26. 4. Get Table and View Names from a Database
20. 26. 5. DatabaseMetaData Methods for Database Information
20. 26. 6. Get Database information from DatabaseMetaData
20. 26. 7. DatabaseMetaData Methods for Information on the Database's Supported Features
20. 26. 8. DatabaseMetaData Methods for Information on the Database's Limitations
20. 26. 9. Display Database Limitation for your connection
20. 26. 10. DatabaseMetaData Methods for Information on the Database's Contents
20. 26. 11. Display Database contents for current connection
20. 26. 12. Get table information from DatabaseMetaData
20. 26. 13. Get procedure information from DatabaseMetaData
w__w___w._j__a__v_a2_s.co__m | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.