Skip to content

Commit d6b58fd

Browse files
author
Rory Gibson
committed
Mods to import syntax to get it working; introduce h2 as database so no MySQL required
1 parent c6a7713 commit d6b58fd

File tree

4 files changed

+21
-10
lines changed

4 files changed

+21
-10
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
11
target
2+
.idea/
3+
greeter.h2.db
4+
greeter.trace.db
5+
src/greeter/Greeter.iml

project.clj

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
[mysql/mysql-connector-java "5.1.6"]
99
[org.slf4j/slf4j-nop "1.6.4"]
1010
[org.thymeleaf/thymeleaf "2.0.8"]
11+
[com.h2database/h2 "1.3.170"]
1112
[expectations "1.4.17"]]
1213

1314
:plugins [[lein-ring "0.7.3"]

src/greeter/db.clj

+7-1
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,18 @@
22
(:require [clojure.java.jdbc :as sql]))
33

44
;; Database creation and population
5-
(def db {:classname "com.mysql.jdbc.Driver"
5+
(def mysqldb {:classname "com.mysql.jdbc.Driver"
66
:subprotocol "mysql"
77
:subname "//localhost:3306/helloworld"
88
:user "root"
99
:password ""})
1010

11+
(def h2db {:classname "org.h2.Driver"
12+
:subprotocol "h2:file"
13+
:subname (str (System/getProperty "user.dir") "/" "greeter")
14+
:user "sa"
15+
:password "" })
16+
1117
(defn drop-table-from [conn]
1218
(try
1319
(sql/with-connection conn

src/greeter/svr.clj

+9-9
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,27 @@
44
(:use [ring.adapter.jetty :only [run-jetty]])
55
(:require [compojure.handler :as handler]
66
[compojure.route :as route]
7-
[greeter.util]
8-
[greeter.db :refer]
9-
[greeter.thymeleaf]))
10-
7+
[greeter.util :refer :all]
8+
[greeter.db :refer :all]
9+
[greeter.thymeleaf :refer :all]))
10+
1111

1212
;; Set up the DB
13-
(drop-table-from db)
14-
(create-table-from db)
15-
(insert-records-in db)
13+
(drop-table-from h2db)
14+
(create-table-from h2db)
15+
(insert-records-in h2db)
1616

1717
;; Route/handler bindings
1818
(defroutes app-routes
1919
(GET "/:forename" [forename]
2020
(let [engine (create-engine)
21-
surname (lookup-surname-of forename db)
21+
surname (lookup-surname-of forename h2db)
2222
context (create-context { "forename" forename "surname" surname })]
2323
(.process engine "greeting" context)))
2424

2525
(route/not-found
2626
(let [engine (create-engine)
27-
names (strip-keywords-from-array (all-names-from db))
27+
names (strip-keywords-from-array (all-names-from h2db))
2828
context (create-context { "allnames" names})]
2929
(.process engine "index" context))))
3030

0 commit comments

Comments
 (0)