DatabaseInfo.java Source code

Java tutorial

Introduction

Here is the source code for DatabaseInfo.java

Source

/*
    
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);
    }
}