After you have setup your clojure project, change the
project.clj
file to look something like the example. Note the jdbc and postgresql dependencies.
(defproject elephantsql-test "0.1.0-SNAPSHOT"
:description "FIXME: write description"
:url "http://example.com/FIXME"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.5.1"],
[org.clojure/java.jdbc "0.2.3"]
[postgresql "9.2-902.jdbc4"]])
When you have added the dependencies you can now interact with the database the following way:
ns elephantsql-test.db
(:import com.mchange.v2.c3p0.ComboPooledDataSource)
(:import java.net.URI)
(:import java.util.UUID)
(:require '[clojure.java.jdbc :as sql]
'[clojure.string :as string])
(def url (get (System/getenv) "ELEPHANTSQL_URL"
"postgres://localhost/contacts"))
(defn pool [url]
(let [uri (java.net.URI. url)
host (.getHost uri)
port (if (pos? (.getPort uri)) (.getPort uri) 5432)
path (.getPath uri)
user-info (or (.getUserInfo uri) ":")
[user password] (clojure.string/split user-info #":")]
{:datasource
(doto (com.mchange.v2.c3p0.ComboPooledDataSource.)
(.setDriverClass "org.postgresql.Driver")
(.setJdbcUrl (str "jdbc:postgresql://" host ":" port path))
(.setInitialPoolSize 3)
(.setMaxPoolSize 10)
(.setUser user)
(.setPassword password))}))
(def pooled-db (delay (pool url)))
(defn contacts []
(sql/with-connection @pooled-db
(sql/with-query-results results
["SELECT * FROM people"]
(into [] results))))