Java JDBC Derby Connection printStats(String statstring)

Here you can find the source of printStats(String statstring)

Description

Executes an arbitrary SQL query and returns the result.

License

Open Source License

Declaration

public static ResultSet printStats(String statstring) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.sql.Connection;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

public class Main {
    private static String dbName;
    private static String lasterror = "";
    private static Connection conn = null;
    private static ArrayList<Statement> statements = new ArrayList<Statement>();
    private static ResultSet rs = null;
    private static Statement s = null;

    /** Executes an arbitrary SQL query and returns the result.
     *  @returns rs - contains ResultSet if sucessful. Null if not.
     */// ww  w  .  j  a  v  a 2 s .c  o m
    public static ResultSet printStats(String statstring) {
        boolean successful = true;
        try {
            System.out.println("Connected to database " + dbName);

            s = conn.createStatement(); // create statement
            statements.add(s);

            System.out.println("---Reading Statistics---");

            rs = s.executeQuery(statstring);

        } catch (SQLException sqle) {
            printSQLException(sqle);
            successful = false;
            return null;
        }

        return rs;
    }

    /**
     * Prints details of an SQLException chain to <code>System.err</code>.
     * Details included are SQL State, Error code, Exception message.
     *
     * @param e the SQLException from which to print details.
     */
    public static void printSQLException(SQLException e) {
        // Unwraps the entire exception chain to unveil the real cause of the
        // Exception.
        while (e != null) {
            System.err.println("\n----- SQLException -----");
            System.err.println("  SQL State:  " + e.getSQLState());
            System.err.println("  Error Code: " + e.getErrorCode());
            System.err.println("  Message:    " + e.getMessage());
            // for stack traces, refer to derby.log or uncomment this:
            //e.printStackTrace(System.err);
            lasterror = e.getMessage();
            e = e.getNextException();
        }
    }
}

Related

  1. getLocalConnection()
  2. getRowsFromDatabase(Connection con, int numberOfRows, boolean reuseConnection, String driver, String dsn, String user, String password, String tableName, String whereString, String orderByString, String groupByString)
  3. getSessionIds()
  4. getSize2()
  5. getTables(File database)
  6. removeUnitDB()
  7. saveContact(String fName, String lName)
  8. startDerby()
  9. startDerbyInEmbeddedMode()