File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\jdbc\JdbcSQLException.java.html

1 /*
2 * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
3 * Version 1.0, and under the Eclipse Public License, Version 1.0
4 * (http://h2database.com/html/license.html).
5 * Initial Developer: H2 Group
6 */
7 package org.h2.jdbc;
8
9 import java.io.PrintStream;
10 import java.io.PrintWriter;
11 import java.sql.SQLException;
12
13 import org.h2.engine.Constants;
14
15 /**
16 * Represents a database exception.
17 */
18 public class JdbcSQLException extends SQLException {
19
20 /**
21 * If the SQL statement contains this text, then it is never added to the
22 * SQL exception. Hiding the SQL statement may be important if it contains a
23 * passwords, such as a CREATE LINKED TABLE statement.
24 */
25 public static final String HIDE_SQL = "--hide--";
26
27 private static final long serialVersionUID = 1L;
28 private final String originalMessage;
29 private final Throwable cause;
30 private final String stackTrace;
31 private String message;
32 private String sql;
33
34 /**
35 * Creates a SQLException.
36 *
37 * @param message the reason
38 * @param sql the SQL statement
39 * @param state the SQL state
40 * @param errorCode the error code
41 * @param cause the exception that was the reason for this exception
42 * @param stackTrace the stack trace
43 */
44 public JdbcSQLException(String message, String sql, String state, int errorCode, Throwable cause, String stackTrace) {
45 super(message, state, errorCode);
46 this.originalMessage = message;
47 setSQL(sql);
48 this.cause = cause;
49 this.stackTrace = stackTrace;
50 buildMessage();
51 initCause(cause);
52 }
53
54 /**
55 * Get the detail error message.
56 *
57 * @return the message
58 */
59 public String getMessage() {
60 return message;
61 }
62
63 /**
64 * INTERNAL
65 */
66 public String getOriginalMessage() {
67 return originalMessage;
68 }
69
70 /**
71 * Prints the stack trace to the standard error stream.
72 */
73 public void printStackTrace() {
74 // The default implementation already does that,
75 // but we do it again to avoid problems.
76 // If it is not implemented, somebody might implement it
77 // later on which would be a problem if done in the wrong way.
78 printStackTrace(System.err);
79 }
80
81 /**
82 * Prints the stack trace to the specified print writer.
83 *
84 * @param s the print writer
85 */
86 public void printStackTrace(PrintWriter s) {
87 if (s != null) {
88 super.printStackTrace(s);
89 // getNextException().printStackTrace(s) would be very very slow
90 // if many exceptions are joined
91 SQLException next = getNextException();
92 for (int i = 0; i < 100 && next != null; i++) {
93 s.println(next.toString());
94 next = next.getNextException();
95 }
96 if (next != null) {
97 s.println("(truncated)");
98 }
99 }
100 }
101
102 /**
103 * Prints the stack trace to the specified print stream.
104 *
105 * @param s the print stream
106 */
107 public void printStackTrace(PrintStream s) {
108 if (s != null) {
109 super.printStackTrace(s);
110 // getNextException().printStackTrace(s) would be very very slow
111 // if many exceptions are joined
112 SQLException next = getNextException();
113 for (int i = 0; i < 100 && next != null; i++) {
114 s.println(next.toString());
115 next = next.getNextException();
116 }
117 if (next != null) {
118 s.println("(truncated)");
119 }
120 }
121 }
122
123 /**
124 * INTERNAL
125 */
126 public Throwable getOriginalCause() {
127 return cause;
128 }
129
130 /**
131 * Returns the SQL statement.
132 * SQL statements that contain '--hide--' are not listed.
133 *
134 * @return the SQL statement
135 */
136 public String getSQL() {
137 return sql;
138 }
139
140 /**
141 * INTERNAL
142 */
143 public void setSQL(String sql) {
144 if (sql != null && sql.indexOf(HIDE_SQL) >= 0) {
145 sql = "-";
146 }
147 this.sql = sql;
148 buildMessage();
149 }
150
151 private void buildMessage() {
152 StringBuilder buff = new StringBuilder(originalMessage == null ? "- " : originalMessage);
153 if (sql != null) {
154 buff.append("; SQL statement:\n").append(sql);
155 }
156 buff.append(" [").append(getErrorCode()).append('-').append(Constants.BUILD_ID).append(']');
157 message = buff.toString();
158 }
159
160 /**
161 * Returns the class name, the message, and in the server mode, the stack
162 * trace of the server
163 *
164 * @return the string representation
165 */
166 public String toString() {
167 if (stackTrace == null) {
168 return super.toString();
169 }
170 return stackTrace;
171 }
172
173 }
174