Clojure/postgresql: How do I see the exception when an error is in the db?

余生颓废 提交于 2020-01-03 07:21:33

问题


When using clojure with postgresql, whenever a statement I issue is malformed in some way or otherwise rejected by the db itself I get something like the following error:

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

How can I call getNextException so that I can see what I did wrong? Where do I call it from?


回答1:


See this link on clojure/jdbc showing how to drop a table with Clojure/JDBC.

It also shows you how to handle errors with a try catch block.

From within that try catch block, you can write something similar to:

(.printStackTrace (.getCause e))



回答2:


I have used the following to drop/create tables successfully and to get accurate error information when postgresql gets upset:

(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))))


来源:https://stackoverflow.com/questions/7074327/clojure-postgresql-how-do-i-see-the-exception-when-an-error-is-in-the-db

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!