/** * Options are case sensitive. Supported options are: * <table> * <tr><td>[-help] or [-?]</td> * <td>Print the list of options</td></tr> * <tr><td>[-url "<url>"]</td> * <td>The database URL (jdbc:h2:...)</td></tr> * <tr><td>[-user <user>]</td> * <td>The user name</td></tr> * <tr><td>[-password <pwd>]</td> * <td>The password</td></tr> * <tr><td>[-driver <class>]</td> * <td>The JDBC driver class to use (not required in most cases)</td></tr> * <tr><td>[-sql "<statements>"]</td> * <td>Execute the SQL statements and exit</td></tr> * <tr><td>[-properties "<dir>"]</td> * <td>Load the server properties from this directory</td></tr> * </table> * If special characters don't work as expected, you may need to use * -Dfile.encoding=UTF-8 (Mac OS X) or CP850 (Windows). * @h2.resource * * @param args the command line arguments */ public static void main(String... args) throws SQLException { new Shell().runTool(args); }
private void promptLoop() { println(""); println("Welcome to H2 Shell " + Constants.getFullVersion()); println("Exit with Ctrl+C"); if (conn != null) { showHelp(); try { if (conn == null) { connect(); showHelp(); print("sql> "); } else { print("...> "); String line = readLine(); if (line == null) { break; break; } else if ("help".equals(lower) || "?".equals(lower)) { showHelp(); } else if ("list".equals(lower)) { listMode = !listMode; println("Result list mode is now " + (listMode ? "on" : "off")); } else if ("history".equals(lower)) { for (int i = 0, size = history.size(); i < size; i++) { String s = history.get(i); s = s.replace('\n', ' ').replace('\r', ' ');
private void execute(String sql) { if (sql.trim().length() == 0) { return; } long time = System.nanoTime(); try { ResultSet rs = null; try { if (stat.execute(sql)) { rs = stat.getResultSet(); int rowCount = printResult(rs, listMode); time = System.nanoTime() - time; println("(" + rowCount + (rowCount == 1 ? " row, " : " rows, ") + TimeUnit.NANOSECONDS.toMillis(time) + " ms)"); } else { int updateCount = stat.getUpdateCount(); time = System.nanoTime() - time; println("(Update count: " + updateCount + ", " + TimeUnit.NANOSECONDS.toMillis(time) + " ms)"); } } finally { JdbcUtils.closeSilently(rs); } } catch (SQLException e) { println("Error: " + e.toString()); if (listMode) { e.printStackTrace(err); } } }
private String readPassword() throws IOException { try { Object console = Utils.callStaticMethod("java.lang.System.console"); print("Password "); char[] password = (char[]) Utils.callMethod(console, "readPassword"); return password == null ? null : new String(password); } catch (Exception e) { // ignore, use the default solution } Thread passwordHider = new Thread(this, "Password hider"); stopHide = false; passwordHider.start(); print("Password > "); String p = readLine(); stopHide = true; try { passwordHider.join(); } catch (InterruptedException e) { // ignore } print("\b\b"); return p; }
private int printResultAsTable(ResultSet rs) throws SQLException { ResultSetMetaData meta = rs.getMetaData(); int len = meta.getColumnCount(); boolean truncated = false; ArrayList<String[]> rows = New.arrayList(); // buffer the header String[] columns = new String[len]; for (int i = 0; i < len; i++) { String s = meta.getColumnLabel(i + 1); columns[i] = s == null ? "" : s; } rows.add(columns); int rowCount = 0; while (rs.next()) { rowCount++; truncated |= loadRow(rs, len, rows); if (rowCount > MAX_ROW_BUFFER) { printRows(rows, len); rows.clear(); } } printRows(rows, len); rows.clear(); if (truncated) { println("(data is partially truncated)"); } return rowCount; }
serverPropertiesDir = args[++i]; } else if (arg.equals("-help") || arg.equals("-?")) { showUsage(); return; } else if (arg.equals("-list")) { listMode = true; } else { showUsageAndThrowUnsupportedOption(arg); promptLoop(); } else { ScriptReader r = new ScriptReader(new StringReader(sql)); break; execute(s);
println("[Enter] " + url); print("URL "); url = readLine(url).trim(); if (driver == null) { driver = JdbcUtils.getDriver(url); println("[Enter] " + driver); print("Driver "); driver = readLine(driver).trim(); println("[Enter] " + user); print("User "); user = readLine(user); println("[Enter] Hide"); print("Password "); String password = readLine(); if (password.length() == 0) { password = readPassword(); println("Connected");
out.println("Exit with Ctrl+C"); if (conn != null) { showHelp(); try { if (conn == null) { connect(); showHelp(); String line = readLine(); if (line == null) { break; break; } else if ("HELP".equals(upper) || "?".equals(upper)) { showHelp(); } else if ("LIST".equals(upper)) { listMode = !listMode; prep.setString(1, tableName.toUpperCase()); rs = prep.executeQuery(); printResult(rs, false); } catch (SQLException e) { out.println("Exception: " + e.toString()); "SELECT CAST(TABLE_SCHEMA AS VARCHAR(32)) \"Schema\", TABLE_NAME \"Table Name\" " + "FROM INFORMATION_SCHEMA.TABLES ORDER BY TABLE_SCHEMA, TABLE_NAME"); printResult(rs, false); } catch (SQLException e) {
showUsage(); return; stat = conn.createStatement(); promptLoop();
url = readLine(url); if (driver == null) { driver = JdbcDriverUtils.getDriver(url); driver = readLine(driver); out.println("[Enter] " + user); out.print("User "); user = readLine(user); out.println("[Enter] Hide"); out.print("Password "); String password = readLine(); if (password.length() == 0) { password = readPassword();
private String readLine(String defaultValue) throws IOException { String s = readLine(); return s.length() == 0 ? defaultValue : s; }
/** * The command line interface for this tool. The options must be split into * strings like this: "-user", "sa",... Options are case sensitive. The * following options are supported: * <ul> * <li>-help or -? (print the list of options) </li> * <li>-url jdbc:h2:... (database URL) </li> * <li>-user username </li> * <li>-password password </li> * <li>-driver driver the JDBC driver class name (not required for most * databases) </li> * </ul> * * @param args the command line arguments * @throws SQLException */ public static void main(String[] args) throws SQLException { new Shell().run(args); }
private void execute(String sql, boolean listMode) throws SQLException { long time = System.currentTimeMillis(); boolean result; try { result = stat.execute(sql); } catch (SQLException e) { out.println("Error: " + e.toString()); return; } ResultSet rs = null; try { if (result) { rs = stat.getResultSet(); int rowCount = printResult(rs, listMode); time = System.currentTimeMillis() - time; out.println("(" + rowCount + (rowCount == 1 ? " row, " : " rows, ") + time + " ms)"); } else { int updateCount = stat.getUpdateCount(); time = System.currentTimeMillis() - time; out.println("(Update count: " + updateCount + ", " + time + " ms)"); } } catch (SQLException e) { out.println("Error: " + e.toString()); e.printStackTrace(); } finally { JdbcUtils.closeSilently(rs); } }
serverPropertiesDir = args[++i]; } else if (arg.equals("-help") || arg.equals("-?")) { showUsage(); return; } else if (arg.equals("-list")) { listMode = true; } else { showUsageAndThrowUnsupportedOption(arg); promptLoop(); } else { ScriptReader r = new ScriptReader(new StringReader(sql)); break; execute(s);
println("[Enter] " + url); print("URL "); url = readLine(url).trim(); if (driver == null) { driver = JdbcUtils.getDriver(url); println("[Enter] " + driver); print("Driver "); driver = readLine(driver).trim(); println("[Enter] " + user); print("User "); user = readLine(user); println("[Enter] Hide"); print("Password "); String password = readLine(); if (password.length() == 0) { password = readPassword(); println("Connected");
private int printResultAsTable(ResultSet rs) throws SQLException { ResultSetMetaData meta = rs.getMetaData(); int len = meta.getColumnCount(); boolean truncated = false; ArrayList<String[]> rows = New.arrayList(); // buffer the header String[] columns = new String[len]; for (int i = 0; i < len; i++) { String s = meta.getColumnLabel(i + 1); columns[i] = s == null ? "" : s; } rows.add(columns); int rowCount = 0; while (rs.next()) { rowCount++; truncated |= loadRow(rs, len, rows); if (rowCount > MAX_ROW_BUFFER) { printRows(rows, len); rows.clear(); } } printRows(rows, len); rows.clear(); if (truncated) { println("(data is partially truncated)"); } return rowCount; }
private String readPassword() throws IOException { try { Object console = Utils.callStaticMethod("java.lang.System.console"); print("Password "); char[] password = (char[]) Utils.callMethod(console, "readPassword"); return password == null ? null : new String(password); } catch (Exception e) { // ignore, use the default solution } Thread passwordHider = new Thread(this, "Password hider"); stopHide = false; passwordHider.start(); print("Password > "); String p = readLine(); stopHide = true; try { passwordHider.join(); } catch (InterruptedException e) { // ignore } print("\b\b"); return p; }
private String readLine(String defaultValue) throws IOException { String s = readLine(); return s.length() == 0 ? defaultValue : s; }
private void promptLoop() { println(""); println("Welcome to H2 Shell " + Constants.getFullVersion()); println("Exit with Ctrl+C"); if (conn != null) { showHelp(); try { if (conn == null) { connect(); showHelp(); print("sql> "); } else { print("...> "); String line = readLine(); if (line == null) { break; break; } else if ("help".equals(lower) || "?".equals(lower)) { showHelp(); } else if ("list".equals(lower)) { listMode = !listMode; println("Result list mode is now " + (listMode ? "on" : "off")); } else if ("history".equals(lower)) { for (int i = 0, size = history.size(); i < size; i++) { String s = history.get(i); s = s.replace('\n', ' ').replace('\r', ' ');
serverPropertiesDir = args[++i]; } else if (arg.equals("-help") || arg.equals("-?")) { showUsage(); return; } else if (arg.equals("-list")) { listMode = true; } else { showUsageAndThrowUnsupportedOption(arg); promptLoop(); } else { ScriptReader r = new ScriptReader(new StringReader(sql)); break; execute(s);