2011-05-25 67 views
8

我試圖訪問Clojure內的Postgres數據庫。我發現一噸的使用DB的項目爲例,建立這樣的數據庫:PostgreSQL/Clojure的驅動程序問題

(def db 
    {:classname "org.postgresql.Driver" 
    :subprotocol "postgresql" 
    :subname "//localhost/testdb" 
    :username "postgres" 
    :password "postgres"}) 

我那麼努力那麼像這樣訪問數據庫:

(sql/with-connection db 
    (sql/with-query-results recs ["select * from asdf"] 
     (doseq [rec recs] 
      (println rec)))) 

然而,我「M收到此錯誤:

No suitable driver found for jdbc:postgresql://localhost/testdb 
    [Thrown class java.sql.SQLException] 

我假設的問題是:classname "org.postgresql.Driver",但我不知道該解決方案是什麼。我想我需要提供這個驅動程序,但我不知道在哪裏得到它或放在哪裏。有一個下載可在postgresql.org - 我應該下載?還是有什麼我可以把我的項目設置得到lein下載它作爲依賴?一旦我有了它,它會去哪裏?


編輯(響應@mtnygard): 我有這個在我的project.clj:

(defproject hello-www "1.0.0-SNAPSHOT" 
    :dependencies [[org.clojure/clojure "1.2.1"] 
        [postgresql/postgresql "8.4-702.jdbc4"] 
        ...] 

我的Postgres的版本是8.4:

[/media/data/dev/clojure/hello-www (postgres *)]$ postgres --version 
postgres (PostgreSQL) 8.4.8 
+0

你在將postgresql添加到你的project.clj之後運行「lein deps」嗎? – mtnygard 2011-05-25 04:27:21

+0

另外,你是如何執行你的項目的主體? 「lein deps」只是將jar放在lib /中。您仍然需要使用配置的類路徑運行程序。例如,「lein run」和「lein repl」都將設置您的類路徑。 – mtnygard 2011-05-25 04:29:54

+0

您發現問題了!我已經運行'lein deps',但我沒有更新我的REPL。重新開始的時候給了我一個連接。謝謝! – Topher 2011-05-25 04:32:19

回答

8

你是在正確的軌道上。該異常表示您的類路徑在任何地方都沒有org.postgresql.Driver。

檢查jarvana.com,我發現this entry爲postgres JDBC 4驅動程序。還有其他版本可用,具體取決於其他運行時環境。你可以通過編輯你的project.clj文件來添加這個依賴:

(defproject xxxxxxx 
    ;;; other stuff 

    :dependencies [[org.clojure/clojure "1.2.0"] 
       [postgresql/postgresql "9.0-801.jdbc4"]] 
) 
+0

我已經在我的project.clj中有postgresql了 - 請看我更新的答案。 – Topher 2011-05-25 04:30:40

+0

對於任何有同樣問題的人 - 確保以上是在project.clj中。然後,運行'lein deps'。如果你在一個以確保獲得更新,你可能不得不重新啓動你的REPL。 – Topher 2011-05-25 04:33:37