DatabaseMetaData class to obtain information about the : Metadata DB Info « Database SQL JDBC « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Collections Data Structure
8. Database SQL JDBC
9. Design Pattern
10. Development Class
11. Email
12. Event
13. File Input Output
14. Game
15. Hibernate
16. J2EE
17. J2ME
18. JDK 6
19. JSP
20. JSTL
21. Language Basics
22. Network Protocol
23. PDF RTF
24. Regular Expressions
25. Security
26. Servlets
27. Spring
28. Swing Components
29. Swing JFC
30. SWT JFace Eclipse
31. Threads
32. Tiny Application
33. Velocity
34. Web Services SOA
35. XML
Microsoft Office Word 2007 Tutorial
Java Tutorial
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
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Java » Database SQL JDBC » Metadata DB InfoScreenshots 
DatabaseMetaData class to obtain information about the


/*
 * Copyright (c) 2000 David Flanagan.  All rights reserved.
 * This code is from the book Java Examples in a Nutshell, 2nd Edition.
 * It is provided AS-IS, WITHOUT ANY WARRANTY either expressed or implied.
 * You may study, use, and modify it for any non-commercial purpose.
 * You may distribute it non-commercially as long as you retain this notice.
 * For a commercial use license, or to purchase the book (recommended),
 * visit http://www.davidflanagan.com/javaexamples2.
 */

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

/**
 * This class uses the DatabaseMetaData class to obtain information about the
 * database, the JDBC driver, and the tables in the database, or about the
 * columns of a named table.
 */
public class GetDBInfo {
  public static void main(String[] args) {
    Connection c = null// The JDBC connection to the database server
    try {
      // Look for the properties file DB.props in the same directory as
      // this program. It will contain default values for the various
      // parameters needed to connect to a database
      Properties p = new Properties();
      try {
        p.load(GetDBInfo.class.getResourceAsStream("DB.props"));
      catch (Exception e) {
      }

      // Get default values from the properties file
      String driver = p.getProperty("driver")// Driver class name
      String server = p.getProperty("server""")// JDBC URL for server
      String user = p.getProperty("user""")// db user name
      String password = p.getProperty("password""")// db password

      // These variables don't have defaults
      String database = null// The db name (appended to server URL)
      String table = null// The optional name of a table in the db

      // Parse the command-line args to override the default values above
      for (int i = 0; i < args.length; i++) {
        if (args[i].equals("-d"))
          driver = args[++i]//-d <driver>
        else if (args[i].equals("-s"))
          server = args[++i];//-s <server>
        else if (args[i].equals("-u"))
          user = args[++i]//-u <user>
        else if (args[i].equals("-p"))
          password = args[++i];
        else if (database == null)
          database = args[i]// <dbname>
        else if (table == null)
          table = args[i]// <table>
        else
          throw new IllegalArgumentException("Unknown argument: "
              + args[i]);
      }

      // Make sure that at least a server or a database were specified.
      // If not, we have no idea what to connect to, and cannot continue.
      if ((server.length() == 0&& (database.length() == 0))
        throw new IllegalArgumentException("No database specified.");

      // Load the db driver, if any was specified.
      if (driver != null)
        Class.forName(driver);

      // Now attempt to open a connection to the specified database on
      // the specified server, using the specified name and password
      c = DriverManager.getConnection(server + database, user, password);

      // Get the DatabaseMetaData object for the connection. This is the
      // object that will return us all the data we're interested in here
      DatabaseMetaData md = c.getMetaData();

      // Display information about the server, the driver, etc.
      System.out.println("DBMS: " + md.getDatabaseProductName() " "
          + md.getDatabaseProductVersion());
      System.out.println("JDBC Driver: " + md.getDriverName() " "
          + md.getDriverVersion());
      System.out.println("Database: " + md.getURL());
      System.out.println("User: " + md.getUserName());

      // Now, if the user did not specify a table, then display a list of
      // all tables defined in the named database. Note that tables are
      // returned in a ResultSet, just like query results are.
      if (table == null) {
        System.out.println("Tables:");
        ResultSet r = md.getTables("""""%"null);
        while (r.next())
          System.out.println("\t" + r.getString(3));
      }

      // Otherwise, list all columns of the specified table.
      // Again, information about the columns is returned in a ResultSet
      else {
        System.out.println("Columns of " + table + ": ");
        ResultSet r = md.getColumns("""", table, "%");
        while (r.next())
          System.out.println("\t" + r.getString(4" : "
              + r.getString(6));
      }
    }
    // Print an error message if anything goes wrong.
    catch (Exception e) {
      System.err.println(e);
      if (instanceof SQLException)
        System.err.println(((SQLExceptione).getSQLState());
      System.err.println("Usage: java GetDBInfo [-d <driver] "
          "[-s <dbserver>]\n"
          "\t[-u <username>] [-p <password>] <dbname>");
    }
    // Always remember to close the Connection object when we're done!
    finally {
      try {
        c.close();
      catch (Exception e) {
      }
    }
  }
}

           
       
Related examples in the same category
1. Get all key words in database
2. Get Database Schema From MetaData
3. Get Catalog From Database Metadata
4. Is statement pooling supported?
5. Database MetaData: Database version
6. Type info in database metadata
7. A database MetaData query
8. Database Info
9. JDBC Performance
10. Driver Property Info
11. JDBC Version App
w___w_w___._j___av__a2__s___._c___om | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.