Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Assumption: you already have both Clojure and MySQL running on your machine.</p> <ol> <li><p>checkout and build <a href="http://code.google.com/p/clojure-contrib/" rel="nofollow noreferrer">clojure-contrib</a>:</p> <pre><code>git clone git://github.com/richhickey/clojure-contrib.git cd clojure-contrib build </code></pre> <p>Put the resulting <strong>clojure-contrib.jar</strong> on your <strong>CLASSPATH</strong>.</p></li> <li><p>Download <a href="http://dev.mysql.com/doc/refman/5.0/en/connector-j.html" rel="nofollow noreferrer">MySQL Connector/J</a> and put the <strong>mysql-connector-java-5.1.7-bin.jar</strong> on your <strong>CLASSPATH</strong></p> <p><em>You might have to run your JVM with these arguments:</em></p> <pre><code>-Djdbc.drivers=com.mysql.jdbc.Driver </code></pre></li> <li><p>Determine the connection URL of your MySQL database</p> <p>For example, if you are running MySQL under <a href="http://www.mamp.info" rel="nofollow noreferrer">MAMP</a> then the URL that you would use in JDBC will look something like:</p> <pre><code>conn = DriverManager.getConnection ("jdbc:mysql://localhost:8889/db_name?user=root&amp;password=root") </code></pre> <p>The url is broken down into these components:</p> <ul> <li>protocol: <code>jdbc:</code></li> <li>subprotocol: <code>mysql</code></li> <li>db-host: <code>localhost</code></li> <li>db-port: <code>8889</code></li> <li>username </li> <li>password</li> </ul></li> <li><p>Make this clojure script, modify the database connection parameters to match your URL, save as test.clj, compile and run.</p></li> </ol> <pre class="lang-clj prettyprint-override"><code> (use 'clojure.contrib.sql) ;;' satisfy prettify (let [db-host "localhost" db-port 8889 db-name "db_name"] (def db {:classname "com.mysql.jdbc.Driver" :subprotocol "mysql" :subname (str "//" db-host ":" db-port "/" db-name) :user "root" :password "root"}) (with-connection db (with-query-results rs ["select * from languages"] (dorun (map #(println (:language :iso_code %)) rs))))) ; rs will be a sequence of maps, ; one for each record in the result set. </code></pre> <p>NB This code was adapted from similar code written by Mark Volkmann to <a href="http://groups.google.com/group/clojure/browse_thread/thread/176a11847601cc5a/445026a02e302837" rel="nofollow noreferrer">access a Postgres database from Clojure</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload