diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2013-08-03 18:00:10 +0200 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2013-08-03 18:00:10 +0200 |
commit | e1cf8889628d2d31cf7067b8c002f229fc22007d (patch) | |
tree | b6fda3f7f23f445ca0030e88368cb39e27badb96 /myapp/src/main/java | |
parent | 054cfb131292893d100a43ea2cb20c591c17c810 (diff) | |
download | container-playground-e1cf8889628d2d31cf7067b8c002f229fc22007d.tar.gz container-playground-e1cf8889628d2d31cf7067b8c002f229fc22007d.tar.bz2 container-playground-e1cf8889628d2d31cf7067b8c002f229fc22007d.tar.xz container-playground-e1cf8889628d2d31cf7067b8c002f229fc22007d.zip |
wip
Diffstat (limited to 'myapp/src/main/java')
-rw-r--r-- | myapp/src/main/java/io/trygvis/container/myapp/AddressBook.java | 47 |
1 files changed, 42 insertions, 5 deletions
diff --git a/myapp/src/main/java/io/trygvis/container/myapp/AddressBook.java b/myapp/src/main/java/io/trygvis/container/myapp/AddressBook.java index 218f25f..0d05989 100644 --- a/myapp/src/main/java/io/trygvis/container/myapp/AddressBook.java +++ b/myapp/src/main/java/io/trygvis/container/myapp/AddressBook.java @@ -1,10 +1,16 @@ package io.trygvis.container.myapp; +import java.io.BufferedReader; import java.io.EOFException; +import java.io.InputStreamReader; import java.sql.Connection; import java.sql.DriverManager; +import java.sql.Statement; public class AddressBook { + + private BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); + public static void main(String[] args) throws Exception { try { new AddressBook().work(); @@ -17,6 +23,12 @@ public class AddressBook { while (!done) { String cmd = Character.toString(menu()); switch (cmd) { + case "c": + run(new CreateTablesCommand()); + break; + case "d": + run(new DropTablesCommand()); + break; case "l": run(new ListCommand()); break; @@ -34,22 +46,47 @@ public class AddressBook { } public void run(Command command) throws Exception { - try (Connection c = DriverManager.getConnection("jdbc:h2:mem")) { + try (Connection c = DriverManager.getConnection("jdbc:h2:mem:address-book;DB_CLOSE_DELAY=-1")) { c.setAutoCommit(false); command.run(c); + c.commit(); + System.out.println("OK"); } } private char menu() throws Exception { System.out.println("Menu:"); + System.out.println("c Create"); + System.out.println("d Drop"); 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(); + while (true) { + String read = reader.readLine(); + if (read == null) { + throw new EOFException(); + } + read = read.trim(); + if (read.length() != 0) { + return read.charAt(0); + } + } + } + + public static class CreateTablesCommand implements Command { + @Override + public void run(Connection c) throws Exception { + Statement statement = c.createStatement(); + statement.executeUpdate(PersonDao.createTableSql); + } + } + + public static class DropTablesCommand implements Command { + @Override + public void run(Connection c) throws Exception { + Statement statement = c.createStatement(); + statement.executeUpdate(PersonDao.dropTableSql); } - return (char) read; } public static class ListCommand implements Command { |