KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > scriptella > jdbc > JdbcException


1 /*
2  * Copyright 2006-2007 The Scriptella Project Team.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package scriptella.jdbc;
17
18 import scriptella.spi.ProviderException;
19
20 import java.sql.SQLException JavaDoc;
21 import java.util.List JavaDoc;
22
23 /**
24  * Unchecked wrapper for SQL exceptions or other SQL related errors.
25  *
26  * @author Fyodor Kupolov
27  * @version 1.0
28  */

29 public class JdbcException extends ProviderException {
30
31     public JdbcException(String JavaDoc message) {
32         super(message);
33     }
34
35     public JdbcException(String JavaDoc message, Throwable JavaDoc cause) {
36         super(message, cause);
37         initVendorCodes(cause);
38     }
39
40     public JdbcException(String JavaDoc message, Throwable JavaDoc cause, String JavaDoc sql, List JavaDoc<?> parameters) {
41         super(message, cause);
42         initVendorCodes(cause);
43         setErrorStatement(sql, parameters);
44     }
45
46     public JdbcException(String JavaDoc message, Throwable JavaDoc cause, String JavaDoc sql) {
47         super(message, cause);
48         initVendorCodes(cause);
49         setErrorStatement(sql, null);
50     }
51     public JdbcException(String JavaDoc message, String JavaDoc sql) {
52         super(message);
53         setErrorStatement(sql, null);
54     }
55
56
57     ProviderException setErrorStatement(String JavaDoc sql, List JavaDoc<?> params) {
58         return super.setErrorStatement(sql + ((params == null || params.isEmpty()) ? "" : (". Parameters: " + params)));
59     }
60
61
62     protected void initVendorCodes(Throwable JavaDoc t) {
63         if (t instanceof SQLException JavaDoc) {
64             SQLException JavaDoc sqlEx = (SQLException JavaDoc) t;
65             addErrorCode(sqlEx.getSQLState()).addErrorCode(String.valueOf(sqlEx.getErrorCode()));
66         }
67
68     }
69
70     public Throwable JavaDoc getNativeException() {
71         //Search for SQLException, which is important for user to see.
72
for (Throwable JavaDoc e = getCause(); e != null; e = e.getCause()) {
73             if (e instanceof SQLException JavaDoc) {
74                 return e;
75             }
76         }
77         return null;
78     }
79
80     public String JavaDoc getProviderName() {
81         return "JDBC";
82     }
83
84 }
85
Popular Tags