clojure.java.jdbc

https://github.com/clojure/java.jdbc.git

git clone 'https://github.com/clojure/java.jdbc.git'

(ql:quickload :clojure.java.jdbc)
621

clojure.java.jdbc

A low-level Clojure wrapper for JDBC-based access to databases. This project is “Stable” (no longer “Active”). It has effectively been superseded by seancorfield/next.jdbc.

For higher level DSLs and migration libraries that are compatible, see the documentation.

Formerly known as clojure.contrib.sql.

This library is mature and stable. It is widely used and its use is described in many books and tutorials. It will continue to get bug fixes and minor releases. Based on my experience using and maintaining this library, I've created a faster, more modern JDBC wrapper called next.jdbc. I consider it to be the “next generation” of clojure.java.jdbc but it exposes a different API – a better API, I think.

Documentation

Support

Releases and Dependency Information

Latest stable release: 0.7.10 – requires Clojure 1.7 or later!

Leiningen dependency information: clojure [org.clojure/java.jdbc "0.7.10"] Maven dependency information: xml <dependency> <groupId>org.clojure</groupId> <artifactId>java.jdbc</artifactId> <version>0.7.10</version> </dependency> Note: Earlier versions of Clojure are supported by older versions of clojure.java.jdbc: e.g., version 0.6.1 supports Clojure 1.4 and later.

You will also need to add dependencies for the JDBC driver you intend to use. Here are links (to Maven Central) for each of the common database drivers that clojure.java.jdbc is known to be used with:

Note: different versions of various database drivers have different Java/JVM version requirements. In particular, recent versions of Apache Derby require at least Java 8 and recent versions of H2 require at least Java 7. Clojure's Continuous Integration system uses older versions so tests can be run on Java 6 (see pom.xml); local testing is done with more recent versions on Java 8.

Example Usage

(require '[clojure.java.jdbc :as j])

;; there are many ways to write a db-spec but the easiest way is to
;; use :dbtype and then provide the :dbname and any of :user, :password,
;; :host, :port, and other options as needed:
(def mysql-db {:dbtype "mysql"
               :dbname "clojure_test"
               :user "clojure_test"
               :password "clojure_test"})

(def pg-db {:dbtype "postgresql"
            :dbname "mypgdatabase"
            :host "mydb.server.com"
            :user "myuser"
            :password "secret"
            :ssl true
            :sslfactory "org.postgresql.ssl.NonValidatingFactory"})

;; if the dbtype is not known to clojure.java.jdbc, or you want to override the
;; default choice of JDBC driver class name, you can provide :classname and the
;; name of the class to use:

(def redshift42 {:dbtype "redshift"
                 :dbname "myredstore"
                 :classname "com.amazon.redshift.jdbc42.Driver"
                 ...})

;; you can also specify a full connection string if you'd prefer:
(def pg-uri
  {:connection-uri (str "postgresql://myuser:secret@mydb.server.com:5432/mypgdatabase"
                        "?ssl=true&sslfactory=org.postgresql.ssl.NonValidatingFactory")})

(j/insert-multi! mysql-db :fruit
  [{:name "Apple" :appearance "rosy" :cost 24}
   {:name "Orange" :appearance "round" :cost 49}])
;; ({:generated_key 1} {:generated_key 2})

(j/query mysql-db
  ["select * from fruit where appearance = ?" "rosy"]
  {:row-fn :cost})
;; (24)

For more detail see the API reference or documentation.

Developer Information

Change Log

Copyright and License

Copyright (c) Sean Corfield, Stephen Gilardi, 2011-2014. All rights reserved. The use and distribution terms for this software are covered by the Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) which can be found in the file epl-v10.html at the root of this distribution. By using this software in any fashion, you are agreeing to be bound by the terms of this license. You must not remove this notice, or any other, from this software.