2011-08-16 7 views
16

私が発行する文が何らかの方法で不正な形式またはその他のDB自体によって拒否されるたびに、私は次のエラーのようなものを取得、PostgreSQLのとClojureのを使用して:私が見ることができるように、私はgetNextExceptionを呼び出すことができますどのようにClojure/postgresql:エラーがdbにある場合、どのように例外が表示されますか?

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

を私は何を間違えたの?どこから呼び出すのですか?

答えて

6

Clojure/JDBCを使用してテーブルを削除する方法を示すlink on clojure/jdbcを参照してください。

また、try catchブロックでエラーを処理する方法も示します。そのトライ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)))) 
関連する問題