From 054cfb131292893d100a43ea2cb20c591c17c810 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Sat, 3 Aug 2013 16:39:50 +0200 Subject: wip --- .../io/trygvis/container/myapp/AddressBook.java | 61 ++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 myapp/src/main/java/io/trygvis/container/myapp/AddressBook.java (limited to 'myapp/src/main/java/io/trygvis/container/myapp/AddressBook.java') diff --git a/myapp/src/main/java/io/trygvis/container/myapp/AddressBook.java b/myapp/src/main/java/io/trygvis/container/myapp/AddressBook.java new file mode 100644 index 0000000..218f25f --- /dev/null +++ b/myapp/src/main/java/io/trygvis/container/myapp/AddressBook.java @@ -0,0 +1,61 @@ +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()); + } + } +} -- cgit v1.2.3