Database Info : Metadata DB Info « Database SQL JDBC « Java






Database Info

  
/*

MySQL and Java Developer's Guide

Mark Matthews, Jim Cole, Joseph D. Gradecki
Publisher Wiley,
Published February 2003, 
ISBN 0471269239

*/


import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

public class DatabaseInfo extends HttpServlet {

  public void doGet(HttpServletRequest inRequest,
      HttpServletResponse outResponse) throws ServletException,
      IOException {

    PrintWriter out = null;
    Connection connection = null;
    Statement statement;
    ResultSet rs;

    outResponse.setContentType("text/html");
    out = outResponse.getWriter();

    try {
      Context ctx = new InitialContext();
      DataSource ds = (DataSource) ctx
          .lookup("java:comp/env/jdbc/AccountsDB");
      connection = ds.getConnection();

      DatabaseMetaData md = connection.getMetaData();
      statement = connection.createStatement();

      out
          .println("<HTML><HEAD><TITLE>Database Server Information</TITLE></HEAD>");
      out.println("<BODY>");
      out.println("<H1>General Source Information</H1>");
      out.println("getURL() - " + md.getURL() + "<BR>");
      out.println("getUserName() - " + md.getUserName() + "<BR>");
      out.println("getDatabaseProductVersion - "
          + md.getDatabaseProductVersion() + "<BR>");
      out.println("getDriverMajorVersion - " + md.getDriverMajorVersion()
          + "<BR>");
      out.println("getDriverMinorVersion - " + md.getDriverMinorVersion()
          + "<BR>");
      out.println("nullAreSortedHigh - " + md.nullsAreSortedHigh()
          + "<BR>");

      out.println("<H1>Feature Support</H1>");
      out.println("supportsAlterTableWithDropColumn - "
          + md.supportsAlterTableWithDropColumn() + "<BR>");
      out.println("supportsBatchUpdates - " + md.supportsBatchUpdates()
          + "<BR>");
      out.println("supportsTableCorrelationNames - "
          + md.supportsTableCorrelationNames() + "<BR>");
      out.println("supportsPositionedDelete - "
          + md.supportsPositionedDelete() + "<BR>");
      out.println("supportsFullOuterJoins - "
          + md.supportsFullOuterJoins() + "<BR>");
      out.println("supportsStoredProcedures - "
          + md.supportsStoredProcedures() + "<BR>");
      out.println("supportsMixedCaseQuotedIdentifiers - "
          + md.supportsMixedCaseQuotedIdentifiers() + "<BR>");
      out.println("supportsANSI92EntryLevelSQL - "
          + md.supportsANSI92EntryLevelSQL() + "<BR>");
      out.println("supportsCoreSQLGrammar - "
          + md.supportsCoreSQLGrammar() + "<BR>");

      out.println("<H1>Data Source Limits</H1>");
      out.println("getMaxRowSize - " + md.getMaxRowSize() + "<BR>");
      out.println("getMaxStatementLength - " + md.getMaxStatementLength()
          + "<BR>");
      out.println("getMaxTablesInSelect - " + md.getMaxTablesInSelect()
          + "<BR>");
      out.println("getMaxConnections - " + md.getMaxConnections()
          + "<BR>");
      out.println("getMaxCharLiteralLength - "
          + md.getMaxCharLiteralLength() + "<BR>");

      out.println("<H1>SQL Object Available</H1>");
      out.println("getTableTypes()<BR><UL>");
      rs = md.getTableTypes();
      while (rs.next()) {
        out.println("<LI>" + rs.getString(1));
      }
      out.println("</UL>");

      out.println("getTables()<BR><UL>");
      rs = md.getTables("accounts", "", "%", new String[0]);
      while (rs.next()) {
        out.println("<LI>" + rs.getString("TABLE_NAME"));
      }
      out.println("</UL>");

      out.println("<H1>Transaction Support</H1>");
      out.println("getDefaultTransactionIsolation() - "
          + md.getDefaultTransactionIsolation() + "<BR>");
      out.println("dataDefinitionIgnoredInTransactions() - "
          + md.dataDefinitionIgnoredInTransactions() + "<BR>");

      out.println("<H1>General Source Information</H1>");
      out.println("getMaxTablesInSelect - " + md.getMaxTablesInSelect()
          + "<BR>");
      out.println("getMaxColumnsInTable - " + md.getMaxColumnsInTable()
          + "<BR>");
      out.println("getTimeDateFunctions - " + md.getTimeDateFunctions()
          + "<BR>");
      out.println("supportsCoreSQLGrammar - "
          + md.supportsCoreSQLGrammar() + "<BR>");

      out.println("getTypeInfo()<BR><UL>");
      rs = md.getTypeInfo();
      while (rs.next()) {
        out.println("<LI>" + rs.getString(1));
      }
      out.println("</UL>");

      out.println("</BODY></HTML>");
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  public void doPost(HttpServletRequest inRequest,
      HttpServletResponse outResponse) throws ServletException,
      IOException {
    doGet(inRequest, outResponse);
  }
}

           
         
    
  








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.DatabaseMetaData class to obtain information about the
9.JDBC Performance
10.Driver Property Info
11.If database support transaction
12.If database support scrollable result sets
13.Get database product information
14.Get data types supported by database
15.If database support batch update
16.Get database maximum table name length
17.Get numeric functions supported by database
18.Get JDBC driver information
19.Get system functions supported by database?
20.Get the max concurrent connection to a database?
21.Get date time functions supported by database
22.Get column names of a table using ResultSetMetaData
23.Get column's precision and scale value?
24.Get string functions supported by database?
25.JDBC Version App
26.Listing All Non-SQL92 Keywords Used by a Database
27.Listing the String Functions Supported by a Database: retrieves a list of string functions that a database supports.
28.Listing the Numeric Functions Supported by a Database
29.Listing the System Functions Supported by a Database
30.Listing the Time and Date Functions Supported by a Database
31.Getting the Maximum Table Name Length allowed in a Database
32.Detect if a table exists
33.This program uses metadata to display arbitrary tables in a database.