Connecting to Microsoft SQL Server using Clojure

Viewed 6571

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

3 Answers

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:

Code

(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'"])

Leiningen configuration

: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

Related