2011-08-16 94 views
16

当使用PostgreSQL的,每当我发出了一个声明以某种方式存在格式错误或由db本身我得到类似如下的错误而被拒绝的Clojure:Clojure/postgresql:如何在db中发生错误时看到异常?

java.sql.BatchUpdateException: 
    Batch entry 0 drop schema public cascade was aborted. 
    Call getNextException to see the cause. 

我怎么能叫的getNextException,这样我可以看到我做错了什么?我从哪里打电话给我?

回答

6

查看此link on clojure/jdbc显示如何使用Clojure/JDBC删除表。

它还向您展示了如何使用try catch块来处理错误。

从这个try catch块内,你可以写类似的东西:

(.printStackTrace (.getCause e)) 
+0

在某些情况下,调用'(.getCause e)'抛出'NullPointerException',但是'(.getNextException e)'起作用。 – siphiuel

2

我用下面下降/成功创建表,以及时得到的PostgreSQL得到不高兴准确的错误信息:

(defn drop-table [name] 
    (sql/with-connection db 
     (with-open [s (.createStatement (sql/connection))] 
     (try 
      (.addBatch s (str "drop table " name)) 
      (seq (.executeBatch s)) 
      (catch Exception _))))) 

(defn setup [] 
    (comment "TODO: create tables here")) 

(defn -main [] 
    (try 
    (print "Dropping table...") (flush) 
    (drop-table "table_name") 
    (print "Creating database structure...") (flush) 
    (setup) 
    (println " done") 
    (catch Exception e (.getNextException e)))) 
相关问题