clojure with mysql and java -
not sure why error when try code below execute query on mysql database clojure:
user=> (mysql.core/list-users) classnotfoundexception mysql.core java.net.urlclassloader$1.run (urlclassloader.java:366)
here 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 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 * user"] (println rows))))
here works (the thing have deps correct without project file):
$ mysql -u root mysql> create database clojure_test; grant on clojure_test.* clojure_test@localhost identified "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 * fruit appearance = ?" "rosy"] :row-fn :cost)
it seems run code in repl, need change namespace , load file:
1, change repl's namespace (ns mysql.core)
2, run (load-file path-to-file)
edit:
another solution excute (use 'mysql.core)
in repl, can run code.
Comments
Post a Comment