On Tue, Feb 24, 2009 at 7:05 AM, Sean <francoisdev...@gmail.com> wrote:
>
> 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 :)

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.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To post to this group, send email to clojure@googlegroups.com
To unsubscribe from this group, send email to 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to