Beispiel #1
0
  public static void main(String[] args) throws Throwable {
    DatabaseUtil databaseUtil;
    ConsoleReader reader;
    String line;
    boolean quit;

    databaseUtil = null;

    try {
      System.out.printf("QueryDatabase 1.3%n");
      System.out.printf("-----------------%n%n");

      System.out.printf(
          "Initialising persistence context `%s'...%n", DatabaseConfig.PersistenceUnit);
      databaseUtil = new DatabaseUtil();

      reader = new ConsoleReader();

      do {
        line = reader.readCommand();
        quit = interpretLine(databaseUtil, line);
      } while (!quit);
    } catch (Throwable oops) {
      System.out.flush();
      System.err.printf("%n%s%n", oops.getLocalizedMessage());
      // oops.printStackTrace(System.out);
    } finally {
      databaseUtil.close();
    }
  }
Beispiel #2
0
  private static boolean interpretLine(DatabaseUtil databaseUtil, String line) {
    boolean result;
    String command;
    List<?> objects;
    int affected;

    result = false;
    try {
      command = StringUtils.substringBefore(line, " ");
      switch (command) {
        case "quit":
          result = true;
          break;
        case "begin":
          databaseUtil.openTransaction();
          System.out.println("Transaction started");
          break;
        case "commit":
          databaseUtil.commitTransaction();
          System.out.println("Transaction committed");
          break;
        case "rollback":
          databaseUtil.rollbackTransaction();
          System.out.println("Transaction rollbacked");
          break;
        case "update":
        case "delete":
          affected = databaseUtil.executeUpdate(line);
          System.out.printf("%d objects affected%n", affected);
          break;
        case "select":
          objects = databaseUtil.executeSelect(line);
          System.out.printf("%d objects found%n", objects.size());
          printResultList(objects);
          break;
        default:
          System.err.println("Command not understood");
      }
    } catch (Throwable oops) {
      System.err.println(oops.getMessage());
      // oops.printStackTrace(System.err);
    }

    return result;
  }