import java.sql.*;
import java.util.Scanner;

// Most useful for reading material from a database
// On the command line: java -cp .:smallsql.jar SmallSQLRead
// Make sure that smallsql.jar is in the proper directory
public class SmallSQLRead {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        Class.forName("smallsql.database.SSDriver");
        Connection con = DriverManager.getConnection("jdbc:smallsql:testdb");
        Statement stat = con.createStatement();

        boolean done = false;
        Scanner input = new Scanner(System.in);
        while (!done) {
            System.out.print("Enter command: ");
            String cmd = input.nextLine();
            if (cmd.equals("quit")) {
                done = true;
            } else {
                System.out.print("Enter # of columns expected: ");
                int columns = input.nextInt();
                input.nextLine();
                if (stat.execute(cmd)) {
                    ResultSet results = stat.getResultSet();
                    while (results.next()) {
                        for (int c = 1; c <= columns; ++c) {
                            System.out.print(results.getString(c) + "\t");
                        }
                        System.out.println();
                    } 
                }
            }
        }
    }
}
