Database SQL JDBC Java

/*
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("Database Server Information");
      out.println("");
      out.println("

General Source Information

");
      out.println("getURL() - " + md.getURL() + "
");
      out.println("getUserName() - " + md.getUserName() + "
");
      out.println("getDatabaseProductVersion - "
          + md.getDatabaseProductVersion() + "
");
      out.println("getDriverMajorVersion - " + md.getDriverMajorVersion()
          + "
");
      out.println("getDriverMinorVersion - " + md.getDriverMinorVersion()
          + "
");
      out.println("nullAreSortedHigh - " + md.nullsAreSortedHigh()
          + "
");
      out.println("

Feature Support

");
      out.println("supportsAlterTableWithDropColumn - "
          + md.supportsAlterTableWithDropColumn() + "
");
      out.println("supportsBatchUpdates - " + md.supportsBatchUpdates()
          + "
");
      out.println("supportsTableCorrelationNames - "
          + md.supportsTableCorrelationNames() + "
");
      out.println("supportsPositionedDelete - "
          + md.supportsPositionedDelete() + "
");
      out.println("supportsFullOuterJoins - "
          + md.supportsFullOuterJoins() + "
");
      out.println("supportsStoredProcedures - "
          + md.supportsStoredProcedures() + "
");
      out.println("supportsMixedCaseQuotedIdentifiers - "
          + md.supportsMixedCaseQuotedIdentifiers() + "
");
      out.println("supportsANSI92EntryLevelSQL - "
          + md.supportsANSI92EntryLevelSQL() + "
");
      out.println("supportsCoreSQLGrammar - "
          + md.supportsCoreSQLGrammar() + "
");
      out.println("

Data Source Limits

");
      out.println("getMaxRowSize - " + md.getMaxRowSize() + "
");
      out.println("getMaxStatementLength - " + md.getMaxStatementLength()
          + "
");
      out.println("getMaxTablesInSelect - " + md.getMaxTablesInSelect()
          + "
");
      out.println("getMaxConnections - " + md.getMaxConnections()
          + "
");
      out.println("getMaxCharLiteralLength - "
          + md.getMaxCharLiteralLength() + "
");
      out.println("

SQL Object Available

");
      out.println("getTableTypes()
    ");
          rs = md.getTableTypes();
          while (rs.next()) {
            out.println("
  • " + rs.getString(1));
          }
          out.println("
");
      out.println("getTables()
    ");
          rs = md.getTables("accounts", "", "%", new String[0]);
          while (rs.next()) {
            out.println("
  • " + rs.getString("TABLE_NAME"));
          }
          out.println("
");
      out.println("

Transaction Support

");
      out.println("getDefaultTransactionIsolation() - "
          + md.getDefaultTransactionIsolation() + "
");
      out.println("dataDefinitionIgnoredInTransactions() - "
          + md.dataDefinitionIgnoredInTransactions() + "
");
      out.println("

General Source Information

");
      out.println("getMaxTablesInSelect - " + md.getMaxTablesInSelect()
          + "
");
      out.println("getMaxColumnsInTable - " + md.getMaxColumnsInTable()
          + "
");
      out.println("getTimeDateFunctions - " + md.getTimeDateFunctions()
          + "
");
      out.println("supportsCoreSQLGrammar - "
          + md.supportsCoreSQLGrammar() + "
");
      out.println("getTypeInfo()
    ");
          rs = md.getTypeInfo();
          while (rs.next()) {
            out.println("
  • " + rs.getString(1));
          }
          out.println("
");
      out.println("");
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  public void doPost(HttpServletRequest inRequest,
      HttpServletResponse outResponse) throws ServletException,
      IOException {
    doGet(inRequest, outResponse);
  }
}