// Purpose.  Issue SQL commands interactively 

import java.sql.*;
import java.io.*;

public class InteractiveSQL {
   public static void main( String[] args ) {
      Connection conn = null;
      try {
         Class.forName( "jdbc.idbDriver" );
         // Class.forName( "jdbc.idbDriver" ).newInstance();  // not necessary
         conn = DriverManager.getConnection( "jdbc:idb:c:\\jdk1.2\\InstantDB\\vlh\\vlhdb.prp" );
         Statement      s  = conn.createStatement();
         BufferedReader in = new BufferedReader( new InputStreamReader( System.in ) );

         while (true) {
            System.out.print("sql> ");
            String sql = in.readLine();

            if (sql.equals("quit")) break;
            if (sql.length() == 0)  continue;  // ignore blank lines
        
            try {
               boolean query = s.execute(sql);
               if (query)
                  printResultsTable( s.getResultSet(), System.out );
               else if (s.getUpdateCount() < 0)
                  System.out.println( "  statement completed" );
               else
                  System.out.println( "  " + s.getUpdateCount() + " rows affected" );
            } catch (SQLException ex) {
               System.err.println( "SQLexception -- " + ex.getMessage() );
            }
         }
      } catch (Exception ex) {
         System.err.println( ex );
      } finally {
         try { conn.close(); } catch (Exception ex) { }
      }
   }
   static void printResultsTable( ResultSet rs, OutputStream os );
}

// C:> java InteractiveSQL
// InstantDB  - Version 1.91
// Copyright (c) 1997-1999 Instant Computer Solutions Ltd.
// sql> create table mytable (one char(5), two int)
// main create table mytable (one char(5), two int)
//   statement completed
// sql> insert into mytable values ("xyz", 24)
// main insert into mytable values ("xyz", 24)
//   1 rows affected
// sql> insert into mytable values ("rst", 12)
// main insert into mytable values ("rst", 12)
//   1 rows affected
// sql> insert into mytable values ("abc", 36)
// main insert into mytable values ("abc", 36)
//   1 rows affected
// sql> select * from mytable
// main select * from mytable
// +-----+-------+
// | one |  two  |
// +-----+-------+
// | xyz | 24    |
// | rst | 12    |
// | abc | 36    |
// +-----+-------+
// sql> update mytable set two = two / 2
// main update mytable set two = two / 2
//   3 rows affected
// sql> select * from mytable order by one
// main select * from mytable order by one
// +-----+-------+
// | one |  two  |
// +-----+-------+
// | abc | 18    |
// | rst | 6     |
// | xyz | 12    |
// +-----+-------+
