Package org.h2.tools

Class Shell

All Implemented Interfaces:
Runnable

public class Shell extends Tool implements Runnable
Interactive command line tool to access a database using JDBC.
  • Field Details

  • Constructor Details

    • Shell

      public Shell()
  • Method Details

    • main

      public static void main(String... args) throws SQLException
      Options are case sensitive.
      Supported options
      [-help] or [-?] Print the list of options
      [-url "<url>"] The database URL (jdbc:h2:...)
      [-user <user>] The user name
      [-password <pwd>] The password
      [-driver <class>] The JDBC driver class to use (not required in most cases)
      [-sql "<statements>"] Execute the SQL statements and exit
      [-properties "<dir>"] Load the server properties from this directory
      If special characters don't work as expected, you may need to use -Dfile.encoding=UTF-8 (Mac OS X) or CP850 (Windows).
      Parameters:
      args - the command line arguments
      Throws:
      SQLException - on failure
    • setErr

      public void setErr(PrintStream err)
      Sets the standard error stream.
      Parameters:
      err - the new standard error stream
    • setIn

      public void setIn(InputStream in)
      Redirects the standard input. By default, System.in is used.
      Parameters:
      in - the input stream to use
    • setInReader

      public void setInReader(BufferedReader reader)
      Redirects the standard input. By default, System.in is used.
      Parameters:
      reader - the input stream reader to use
    • runTool

      public void runTool(String... args) throws SQLException
      Run the shell tool with the given command line settings.
      Specified by:
      runTool in class Tool
      Parameters:
      args - the command line settings
      Throws:
      SQLException - on failure
    • runTool

      public void runTool(Connection conn, String... args) throws SQLException
      Run the shell tool with the given connection and command line settings. The connection will be closed when the shell exits. This is primary used to integrate the Shell into another application.

      Note: using the "-url" option in args doesn't make much sense since it will override the conn parameter.

      Parameters:
      conn - the connection
      args - the command line settings
      Throws:
      SQLException - on failure
    • showHelp

      private void showHelp()
    • promptLoop

      private void promptLoop()
    • connect

      private void connect() throws IOException, SQLException
      Throws:
      IOException
      SQLException
    • connectH2

      private Connection connectH2(String driver, String url, String user) throws IOException, SQLException
      Throws:
      IOException
      SQLException
    • print

      protected void print(String s)
      Print the string without newline, and flush.
      Parameters:
      s - the string to print
    • println

      private void println(String s)
    • readPassword

      private String readPassword() throws IOException
      Throws:
      IOException
    • run

      public void run()
      INTERNAL. Hides the password by repeatedly printing backspace, backspace, >, <.
      Specified by:
      run in interface Runnable
    • readLine

      private String readLine(String defaultValue) throws IOException
      Throws:
      IOException
    • readLine

      private String readLine() throws IOException
      Throws:
      IOException
    • execute

      private void execute(String sql)
    • printResult

      private int printResult(ResultSet rs, boolean asList) throws SQLException
      Throws:
      SQLException
    • printResultAsTable

      private int printResultAsTable(ResultSet rs) throws SQLException
      Throws:
      SQLException
    • loadRow

      private boolean loadRow(ResultSet rs, int len, ArrayList<String[]> rows) throws SQLException
      Throws:
      SQLException
    • printRows

      private int[] printRows(ArrayList<String[]> rows, int len)
    • printResultAsList

      private int printResultAsList(ResultSet rs) throws SQLException
      Throws:
      SQLException