I am trying to connect to Microsoft SQl Server 2008 database using windows authentication. I have downloaded the JDBC driver for MS SQL Server and added that to my CLASSPATH.
Below is my clojure code. No matter what I do I get java.sql.SQLException : No suitable driver found for jdbc:sqlserver
(ns Test)
(def db {:classname "com.microsoft.jdbc.sqlserver.SQLServerDriver"
:subprotocol "sqlserver"
:subname "server_name"
:DatabaseName "database_name"
:integratedSecurity true
})
(use 'clojure.contrib.sql)
(with-connection db
(with-query-results rs ["SELECT * FROM sys.objects"] (prn rs)))
I have verified that I have access on database , my classpath is correct , I have the correct JDBC version downloaded . Can someone help me out here.
Thanks in advance
In the later software versions (Clojure 1.6+, Microsoft SQL Server 2012 and Microsoft JDBC Driver 4.0 for SQL Server) the code posted by Ash works only with these modifications. I've also updated it according to my current knowledge of Clojure code styling guidelines.
(require '[clojure.java.jdbc :as jdbc])
;; you can optionally specify :host and :port to override the defaults
;; of "127.0.0.1" and 1433 respectively:
(def db-spec {:dbtype "mssql"
:dbname "database-name"
:user "sql-authentication-user-name"
:password "password"})
(let [rows (jdbc/query db-spec
["select * from sys.objects where type = 'U'"])]
(doseq [row rows] (println (:name row)))))
In the case of having multiple SQL Server instances on the same machine, you can specify the instance name as part of the :host
like this (this example is for the default instance name of SQL Server Express on the same machine):
:host "localhost\\sqlexpress"
To make Leiningen work properly with Microsoft JDBC Driver, merge the following with defproject in project.clj:
:dependencies [[org.clojure/java.jdbc "0.6.1"]
[com.microsoft.sqlserver/mssql-jdbc "6.3.6.jre8-preview"]]
Found the solution
(use 'clojure.contrib.sql)
(def db {:classname "com.microsoft.sqlserver.jdbc.SQLServerDriver"
:subprotocol "sqlserver"
:subname "//server-name:port;database=database-name;user=sql-authentication-user-name;password=password"
})
;Add Classpath to your C:\Program Files\Java\JDBC\sqljdbc_3.0\enu\sqljdbc4.jar
;Below code demos how to execute a simple sql select query and print it to console
;This query will print all the user tables in your MS SQL Server Database
(with-connection db
(with-query-results rs ["select * from sys.objects where type = 'U'"]
(doseq [row rs] (println (:name row)))
))
Also updated the wiki http://en.wikibooks.org/wiki/Clojure_Programming/Examples/JDBC_Examples#Microsoft_SQL_Server
Hopefully this will help someone
the prior answers are all correct and work just fine. However, the post is quite old and there are better options out there. Hence I thought it would make sense to update the post for the people searching for a solution (as I was).
It turns out that clojure.java.jdbc
is "Stable" (no longer "Active"). It has effectively been superseded by seancorfield/next.jdbc
.
Using next.jdbc
is rather straightforward and more information can be found in the project page https://github.com/seancorfield/next-jdbc:
(require '[next.jdbc :as jdbc])
(def db {:dbtype "mssql"
:dbname "database-name"
:host "host" ;;optional
:port "port" ;;optional
:user "sql-authentication-user-name"
:password "password"})
(def con (jdbc/get-connection db))
(jdbc/execute! con ["select * from sys.objects where type = 'U'"])
:dependencies [[seancorfield/next.jdbc "1.0.10"]]
[com.microsoft.sqlserver/mssql-jdbc "7.4.1.jre11"]]
Note: Download the mssql-jdbc driver that suits your jre version and place it in the resources folder of your leiningen project or add the path to the driver in the :dependencies [<path-here>]
in your project.clj
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With