PostgreSQL/Clojure的驱动程序问题
问题描述:
我试图访问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
答
你是在正确的轨道上。该异常表示您的类路径在任何地方都没有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"]]
)
你在将postgresql添加到你的project.clj之后运行“lein deps”吗? – mtnygard 2011-05-25 04:27:21
另外,你是如何执行你的项目的主体? “lein deps”只是将jar放在lib /中。您仍然需要使用配置的类路径运行程序。例如,“lein run”和“lein repl”都将设置您的类路径。 – mtnygard 2011-05-25 04:29:54
您发现问题了!我已经运行'lein deps',但我没有更新我的REPL。重新开始的时候给了我一个连接。谢谢! – Topher 2011-05-25 04:32:19