clojure.java.jdbc

A low-level Clojure wrapper for JDBC-based access to databases.

For DSLs that are compatible with this library, consider:

Formerly known as clojure.contrib.sql.

Additional documentation can be found in the java.jdbc section of clojure-doc.org and there is a dedicated java.jdbc mailing list

Releases and Dependency Information

Latest stable release: 0.7.0 -- requires Clojure 1.7 or later!

Leiningen dependency information: clojure [org.clojure/java.jdbc "0.7.0"] Maven dependency information: xml <dependency> <groupId>org.clojure</groupId> <artifactId>java.jdbc</artifactId> <version>0.7.0</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.

clojure.java.jdbc is also tested against Microsoft's own JDBC4 Driver 4.0 but that has to be downloaded manually and placed in a Maven repository accessible to your system. For testing, it was installed locally as: clojure ;; Microsoft SQL Server JDBC4 Driver 4.0 [sqljdbc4/sqljdbc4 "4.0"]

Example Usage

```clojure (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 generated documentation on github.

Developer Information

Change Log

Release 0.7.0 on 2017-07-16

Release 0.7.0-beta5 on 2017-07-05

Release 0.7.0-beta4 on 2017-07-04

Release 0.7.0-beta3 on 2017-07-04

Release 0.7.0-beta2 on 2017-06-30 (a.k.a The Reducible Saga, Part 2) * Support for Clojure 1.5 and 1.6 has been dropped -- breaking change. * Or, put another way, clojure.java.jdbc now requires Clojure 1.7 or later! * All public functions now have specs in the optional clojure.java.jdbc.spec namespace (requires clojure.spec.alpha). * reducible-query and reducible-result-set use IReduce and correctly support the no-init arity of reduce by using the first row of the ResultSet, if present, as the (missing) init value, and only calling f with no arguments if the ResultSet is empty. The init arity of reduce only ever calls f with two arguments.

Release 0.7.0-beta1 on 2017-06-29 * Support for Clojure 1.4.0 has been dropped -- breaking change. * Optional spec support now uses clojure.spec.alpha. * reducible-query accepts a db-spec and a SQL/parameters vector and returns a reducible (IReduce on Clojure 1.7 or later; CollReduce on Clojure 1.5/1.6): when reduced, it runs the query, obtains a reducible result set, and then reduces that. A reducible query will run the query each time it is reduced. The helper function reducible-result-set is public: it accepts a ResultSet and produces a reducible that offers a single pass reduce over the rows. Both functions honor reduced values to short-circuit the process JDBC-99.

Release 0.7.0-alpha3 on 2017-03-23 * classname is now accepted with dbtype / dbname so you can easily specify a JDBC driver class name for a database type that is not known JDBC-151. * redshift has been added as a dbtype with com.amazon.redshift.jdbc.Driver as the driver name.

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.