package io.trygvis.container.myapp; import java.io.EOFException; import java.sql.Connection; import java.sql.DriverManager; public class AddressBook { public static void main(String[] args) throws Exception { try { new AddressBook().work(); } catch (EOFException ignore) { } } private void work() throws Exception { boolean done = false; while (!done) { String cmd = Character.toString(menu()); switch (cmd) { case "l": run(new ListCommand()); break; case "q": done = true; break; default: System.out.println("Unknown command: " + cmd); } } } public static interface Command { void run(Connection c) throws Exception; } public void run(Command command) throws Exception { try (Connection c = DriverManager.getConnection("jdbc:h2:mem")) { c.setAutoCommit(false); command.run(c); } } private char menu() throws Exception { System.out.println("Menu:"); System.out.println("l List"); System.out.println("a Add"); System.out.println("q Quit"); int read = System.in.read(); if (read == -1) { throw new EOFException(); } return (char) read; } public static class ListCommand implements Command { @Override public void run(Connection c) throws Exception { PersonDao.insertInto(null, new Person()); } } }