Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't use jdbc inside clojure

Tags:

sql

clojure

I am trying to make connect clojure to mysql, but somehow fail. My project.clj is:

(defproject my-project "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.3.0"]
                 [org.clojure/java.jdbc "0.2.3"]]
  :jvm-opts ["-Djdbc.drivers=com.mysql.jdbc.Driver"])

My file is:

(ns my-project
  (:require [clojure.java.jdbc :as sql]))


(def db {:classname "com.mysql.jdbc.Driver"
         :subprotocol "mysql"
         :subname "//localhost:3306/db"
         :user "xxx"
         :password "xxx"})

And I am trying to run the following from the REPL:

(sql/with-connection db (sql/with-query-results rows "show tables;"))

The error I am getting is:

No suitable driver found for jdbc:mysql://localhost:3306/db

I have in sql a database called db and I ensured mysql.server is running on port 3306. I would be grateful if someone could give me a hint. Also my error message is not dependant from the username and password I provide, and nothing is shown on the log file from mysql.

Thanks!

like image 896
S4M Avatar asked Dec 08 '22 23:12

S4M


1 Answers

You still fail to put the driver in the classpath. The org.clojure/java.jdbc only provides the client libraries, not the driver itself. Add the following to your :dependencies:

[mysql/mysql-connector-java "5.1.18"]
like image 82
skuro Avatar answered Feb 26 '23 03:02

skuro