Clojure/PostgreSQL:当错误在数据库中时,我该如何查看异常?

19

使用Clojure和PostgreSQL时,每当我发出的语句存在错误或被数据库本身拒绝时,我会收到类似以下错误:

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

我该如何调用getNextException以查看我的错误信息?在哪里调用它?

2个回答

8
请参考clojure/jdbc的此链接,了解如何使用Clojure/JDBC删除表。
该链接还展示了如何在try catch块中处理错误。
在该try catch块内,您可以编写类似以下内容的代码:
(.printStackTrace (.getCause e))

在某些情况下,调用 (.getCause e) 会抛出 NullPointerException,但是 (.getNextException e) 可以正常工作。 - siphiuel

3
我已成功使用以下方法来删除/创建表,并在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))))

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接