KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > providers > jdbc > SQLExceptionReader


1 /*
2  * $Id: SQLExceptionReader.java 3798 2006-11-04 04:07:14Z aperepel $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.providers.jdbc;
12
13 import org.apache.commons.lang.StringUtils;
14 import org.mule.config.ExceptionReader;
15
16 import java.sql.SQLException JavaDoc;
17 import java.util.HashMap JavaDoc;
18 import java.util.Map JavaDoc;
19
20 /**
21  * Surfaces information about SQLExceptions such as the code and sql state. Also uses
22  * the NextException to find the cause
23  */

24 public class SQLExceptionReader implements ExceptionReader
25 {
26     public String JavaDoc getMessage(Throwable JavaDoc t)
27     {
28         SQLException JavaDoc e = (SQLException JavaDoc)t;
29         return e.getMessage() + "(SQL Code: " + e.getErrorCode() + ", SQL State: + " + e.getSQLState() + ")";
30     }
31
32     public Throwable JavaDoc getCause(Throwable JavaDoc t)
33     {
34         SQLException JavaDoc e = (SQLException JavaDoc)t;
35         Throwable JavaDoc cause = e.getNextException();
36         if (cause == null)
37         {
38             cause = e.getCause();
39         }
40         return cause;
41     }
42
43     public Class JavaDoc getExceptionType()
44     {
45         return SQLException JavaDoc.class;
46     }
47
48     /**
49      * Returns a map of the non-stanard information stored on the exception
50      *
51      * @param t the exception to extract the information from
52      * @return a map of the non-stanard information stored on the exception
53      */

54     public Map JavaDoc getInfo(Throwable JavaDoc t)
55     {
56         SQLException JavaDoc e = (SQLException JavaDoc)t;
57         Map JavaDoc info = new HashMap JavaDoc();
58         if (e.getErrorCode() != 0)
59         {
60             info.put("SQL Code", String.valueOf(e.getErrorCode()));
61         }
62         if (e.getSQLState() != null && !e.getSQLState().equals(StringUtils.EMPTY))
63         {
64             info.put("SQL State", e.getSQLState());
65         }
66         return info;
67     }
68 }
69
Popular Tags