summaryrefslogtreecommitdiff
path: root/myapp
diff options
context:
space:
mode:
Diffstat (limited to 'myapp')
-rw-r--r--myapp/src/main/java/io/trygvis/container/myapp/AddressBook.java47
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 {