Ash Ash - 1 year ago 231
SQL Question

Connecting to Microsoft SQL Server using Clojure

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 ""
: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

Ash Ash
Answer Source

Found the solution

(use 'clojure.contrib.sql)
    (def db {:classname ""
                   :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

Hopefully this will help someone