Not sure why I get this error when I try the code below to execute a query on a MySQL database from clojure:
user=> (mysql.core/list-users)
ClassNotFoundException mysql.core java.net.URLClassLoader$1.run (URLClassLoader.java:366)
Here is my project.clj file
(defproject mysql "0.1.0-SNAPSHOT"
:description "FIXME: write description"
:url "http://example.com/FIXME"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [
[org.clojure/clojure "1.5.1"]
[org.clojure/java.jdbc "0.3.3"]
[mysql/mysql-connector-java "5.1.25"]
[postgresql/postgresql "8.4-702.jdbc4"]
[org.xerial/sqlite-jdbc "3.7.2"]
[java-jdbc/dsl "0.1.0"]
])
Here is my core.clj file
(ns mysql.core
(:require [clojure.java.jdbc :as sql]))
(def db {:classname "com.mysql.jdbc.Driver"
:subprotocol "mysql"
:subname "//localhost:3306/dummy"
:user "idf"
:password "pwd"})
(defn list-users []
(sql/with-connection db
(sql/with-query-results rows
["select * from user"]
(println rows))))
HERE IS SOMETHING THAT WORKS (the only thing is you have to get the deps correct without a project file):
$ mysql -u root
mysql> create database clojure_test;
grant all on clojure_test.* to clojure_test@localhost identified by "clojure_test”;
use clojure_test;
CREATE TABLE fruit (id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, name VARCHAR(20), appearance VARCHAR(20), cost DECIMAL(13,2));
lein repl
user=> (require '[clojure.java.jdbc :as j])
user=> (def mysql-db {:subprotocol "mysql" :subname "//127.0.0.1:3306/clojure_test" :user "clojure_test" :password "clojure_test"})
user=> (j/insert! mysql-db :fruit {:name "Apple" :appearance "rosy" :cost 24} {:name "Orange" :appearance "round" :cost 49})
user=> (j/query mysql-db ["select * from fruit where appearance = ?" "rosy"] :row-fn :cost)