summaryrefslogtreecommitdiff
path: root/myapp/src/main
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2013-08-10 15:44:58 +0200
committerTrygve Laugstøl <trygvis@inamo.no>2013-08-10 15:44:58 +0200
commit5a1256a8ed931f7a5ba05c4328353411bae31f2b (patch)
tree2d25ea2076d2dd229bfe7bac61b27977f11889eb /myapp/src/main
parent86c174ddf24b3e5ac8c8d45fa4d7dc6c5d5d3a3a (diff)
downloadcontainer-playground-5a1256a8ed931f7a5ba05c4328353411bae31f2b.tar.gz
container-playground-5a1256a8ed931f7a5ba05c4328353411bae31f2b.tar.bz2
container-playground-5a1256a8ed931f7a5ba05c4328353411bae31f2b.tar.xz
container-playground-5a1256a8ed931f7a5ba05c4328353411bae31f2b.zip
o Moving code out to separate generators.
o Creating SqlSession and SqlSessionFactory.
Diffstat (limited to 'myapp/src/main')
-rw-r--r--myapp/src/main/java/io/trygvis/container/myapp/AddressBookDirect.java69
1 files changed, 34 insertions, 35 deletions
diff --git a/myapp/src/main/java/io/trygvis/container/myapp/AddressBookDirect.java b/myapp/src/main/java/io/trygvis/container/myapp/AddressBookDirect.java
index df0f283..86e8641 100644
--- a/myapp/src/main/java/io/trygvis/container/myapp/AddressBookDirect.java
+++ b/myapp/src/main/java/io/trygvis/container/myapp/AddressBookDirect.java
@@ -1,16 +1,17 @@
package io.trygvis.container.myapp;
-import javax.persistence.TypedQuery;
+import io.trygvis.persistence.sql.SqlExecutor;
+
import java.io.BufferedReader;
import java.io.EOFException;
import java.io.InputStreamReader;
import java.sql.Connection;
-import java.sql.DriverManager;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
import java.sql.SQLException;
-import java.sql.Statement;
+import java.util.ArrayList;
import java.util.List;
-import static io.trygvis.container.myapp.CompanyDao.Utils.insertCompany;
import static io.trygvis.container.myapp.Contact.Gender.FEMALE;
import static io.trygvis.container.myapp.Contact.Gender.MALE;
@@ -18,10 +19,14 @@ public class AddressBookDirect {
private BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
- private Connection c;
+ private MyAppSqlSession session;
+
+ private static MyAppSqlSessionFactory sessionFactory;
public static void main(String[] args) throws Exception {
try {
+ String jdbcUrl = "jdbc:h2:mem:address-book;DB_CLOSE_DELAY=-1";
+ sessionFactory = new MyAppSqlSessionFactory(new DriverManagerDataSource(jdbcUrl));
new AddressBookDirect().work();
} catch (EOFException ignore) {
}
@@ -30,14 +35,13 @@ public class AddressBookDirect {
private void work() throws Exception {
boolean done = false;
while (!done) {
- c = DriverManager.getConnection("jdbc:h2:mem:address-book;DB_CLOSE_DELAY=-1");
try {
- c.setAutoCommit(false);
+ session = sessionFactory.newSession();
done = main();
- c.commit();
+ session.commit();
System.out.println("OK");
} finally {
- c.close();
+ session.close();
}
}
}
@@ -91,21 +95,19 @@ public class AddressBookDirect {
}
public void create() throws SQLException {
- Statement statement = c.createStatement();
- statement.executeUpdate(CompanyDao.createTableSql);
- statement.executeUpdate(ContactDao.createTableSql);
+ session.executeUpdate(CompanyDao.createTableSql);
+ session.executeUpdate(ContactDao.createTableSql);
for (String sql : Sequences.createSequences) {
- statement.executeUpdate(sql);
+ session.executeUpdate(sql);
}
}
public void drop() throws SQLException {
- Statement statement = c.createStatement();
for (String sql : Sequences.dropSequences) {
- statement.executeUpdate(sql);
+ session.executeUpdate(sql);
}
- statement.executeUpdate(ContactDao.dropTableSql);
- statement.executeUpdate(CompanyDao.dropTableSql);
+ session.executeUpdate(ContactDao.dropTableSql);
+ session.executeUpdate(CompanyDao.dropTableSql);
}
// -----------------------------------------------------------------------
@@ -115,13 +117,13 @@ public class AddressBookDirect {
private void company() throws Exception {
while (true) {
System.out.println("Company menu:");
- System.out.println("c Create");
- System.out.println("d Drop");
- System.out.println("d List");
+ System.out.println("a Add");
+ System.out.println("d Delete");
+ System.out.println("l List");
System.out.println("q Back");
String cmd = cmd();
switch (cmd) {
- case "c":
+ case "a":
addCompany();
break;
case "d":
@@ -143,16 +145,14 @@ public class AddressBookDirect {
String name = line();
Company company = new Company(name);
- insertCompany(c, company);
+ session.company.insert(company);
}
public void deleteCompany() {
}
- public void listCompanies() {
- TypedQuery<Company> p = CompanyDao.Utils.queryCompany(c);
-
- List<Company> resultList = p.getResultList();
+ public void listCompanies() throws SQLException {
+ List<Company> resultList = session.company.selectWhere("1=1");
for (Company company : resultList) {
System.out.println("=====================");
System.out.println("Id: " + company.getId());
@@ -168,12 +168,13 @@ public class AddressBookDirect {
private void contact() throws Exception {
while (true) {
System.out.println("Contact menu:");
- System.out.println("c Create");
- System.out.println("d Drop");
+ System.out.println("a Add");
+ System.out.println("d Delete");
+ System.out.println("l list");
System.out.println("q Back");
String cmd = cmd();
switch (cmd) {
- case "c":
+ case "a":
addContact();
break;
case "d":
@@ -215,18 +216,16 @@ public class AddressBookDirect {
Company company = null;
Contact o = new Contact(name, g, company);
- ContactDao.Utils.insertContact(c, o);
+ session.contact.insert(o);
}
public void deleteContact() {
}
- public void listContacts() {
- TypedQuery<Contact> p = ContactDao.Utils.queryContact(c);
-
- List<Contact> resultList = p.getResultList();
- for (Contact contact : resultList) {
+ public void listContacts() throws SQLException {
+ List<Contact> contacts = session.contact.selectWhere("1=1");
+ for (Contact contact : contacts) {
System.out.println("=====================");
System.out.println("Id: " + contact.getId());
System.out.println("Name: " + contact.name);