Database connection examples

354 views
Skip to first unread message

Sean

unread,
Feb 24, 2009, 8:05:52 AM2/24/09
to Clojure
Hi,
I'm trying to connect to a MYSQL instance. I need a little help
making the intellectual connection from Clojure to JDBC.
Specifically, I was hoping someone could post/link to an working
example.

Bonus Points: Provide a second example for Postgres.

Negative Points: Provide an example for SQL Server or Oracle :)

Onorio Catenacci

unread,
Feb 24, 2009, 8:09:12 AM2/24/09
to Clojure
You may find this a good starting point:

<https://groups.google.com/group/clojure/browse_thread/thread/
5b377466ca44886a/16dc0572844a974e?lnk=gst&q=jdbc#16dc0572844a974e>

--
Onorio Catenacci III

Mark Volkmann

unread,
Feb 24, 2009, 8:40:19 AM2/24/09
to clo...@googlegroups.com

I haven't done this with MySQL yet, but I can claim the bonus points
for Postgres! ;-)

The sql library in Clojure Contrib simplifies accessing relational
databases. It supports transactions with commit and rollback, prepared
statements, creating/dropping tables, inserting/updating/deleting
rows, and running queries. The following example connects to a
Postgres database and runs a query.

(use 'clojure.contrib.sql)

(let [db-host "localhost"
db-port 5432
db-name "ct"]

(def db {:classname "org.postgresql.Driver" ; must be in classpath
:subprotocol "postgresql"
:subname (str "//" db-host ":" db-port "/" db-name)
; Any additional keys are passed to the driver
; as driver-specific properties.
:user "mvolkmann"
:password ""})

(with-connection db
(with-query-results rs ["select * from Employee"]
; rs will be a sequence of maps,
; one for each record in the result set.
(dorun (map #(println (:lastname %)) rs)))))

--
R. Mark Volkmann
Object Computing, Inc.

Reply all
Reply to author
Forward
0 new messages