KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > sql > SQLException


1 /*
2  * @(#)SQLException.java 1.27 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.sql;
9
10 /**
11  * <P>An exception that provides information on a database access
12  * error or other errors.
13  *
14  * <P>Each <code>SQLException</code> provides several kinds of information:
15  * <UL>
16  * <LI> a string describing the error. This is used as the Java Exception
17  * message, available via the method <code>getMesage</code>.
18  * <LI> a "SQLstate" string, which follows either the XOPEN SQLstate conventions
19  * or the SQL 99 conventions.
20  * The values of the SQLState string are described in the appropriate spec.
21  * The <code>DatabaseMetaData</code> method <code>getSQLStateType</code>
22  * can be used to discover whether the driver returns the XOPEN type or
23  * the SQL 99 type.
24  * <LI> an integer error code that is specific to each vendor. Normally this will
25  * be the actual error code returned by the underlying database.
26  * <LI> a chain to a next Exception. This can be used to provide additional
27  * error information.
28  * </UL>
29  */

30 public class SQLException extends java.lang.Exception JavaDoc {
31
32     /**
33      * Constructs a fully-specified <code>SQLException</code> object.
34      *
35      * @param reason a description of the exception
36      * @param SQLState an XOPEN or SQL 99 code identifying the exception
37      * @param vendorCode a database vendor-specific exception code
38      */

39     public SQLException(String JavaDoc reason, String JavaDoc SQLState, int vendorCode) {
40     super(reason);
41     this.SQLState = SQLState;
42     this.vendorCode = vendorCode;
43     if (!(this instanceof SQLWarning JavaDoc)) {
44         if (DriverManager.getLogWriter() != null) {
45         DriverManager.println("SQLException: SQLState(" + SQLState +
46                         ") vendor code(" + vendorCode + ")");
47         printStackTrace(DriverManager.getLogWriter());
48         }
49     }
50     }
51
52
53     /**
54      * Constructs an <code>SQLException</code> object with the given reason and
55      * SQLState; the <code>vendorCode</code> field defaults to 0.
56      *
57      * @param reason a description of the exception
58      * @param SQLState an XOPEN or SQL 99 code identifying the exception
59      */

60     public SQLException(String JavaDoc reason, String JavaDoc SQLState) {
61     super(reason);
62     this.SQLState = SQLState;
63     this.vendorCode = 0;
64     if (!(this instanceof SQLWarning JavaDoc)) {
65         if (DriverManager.getLogWriter() != null) {
66         printStackTrace(DriverManager.getLogWriter());
67         DriverManager.println("SQLException: SQLState(" + SQLState + ")");
68         }
69     }
70     }
71
72     /**
73      * Constructs an <code>SQLException</code> object with a reason;
74      * the <code>SQLState</code> field defaults to <code>null</code>, and
75      * the <code>vendorCode</code> field defaults to 0.
76      *
77      * @param reason a description of the exception
78      */

79     public SQLException(String JavaDoc reason) {
80     super(reason);
81     this.SQLState = null;
82     this.vendorCode = 0;
83     if (!(this instanceof SQLWarning JavaDoc)) {
84         if (DriverManager.getLogWriter() != null) {
85         printStackTrace(DriverManager.getLogWriter());
86         }
87     }
88     }
89
90     /**
91      * Constructs an <code>SQLException</code> object;
92      * the <code>reason</code> field defaults to null,
93      * the <code>SQLState</code> field defaults to <code>null</code>, and
94      * the <code>vendorCode</code> field defaults to 0.
95      *
96      */

97     public SQLException() {
98     super();
99     this.SQLState = null;
100     this.vendorCode = 0;
101     if (!(this instanceof SQLWarning JavaDoc)) {
102         if (DriverManager.getLogWriter() != null) {
103         printStackTrace(DriverManager.getLogWriter());
104         }
105     }
106     }
107
108     /**
109      * Retrieves the SQLState for this <code>SQLException</code> object.
110      *
111      * @return the SQLState value
112      */

113     public String JavaDoc getSQLState() {
114     return (SQLState);
115     }
116
117     /**
118      * Retrieves the vendor-specific exception code
119      * for this <code>SQLException</code> object.
120      *
121      * @return the vendor's error code
122      */

123     public int getErrorCode() {
124     return (vendorCode);
125     }
126
127     /**
128      * Retrieves the exception chained to this
129      * <code>SQLException</code> object.
130      *
131      * @return the next <code>SQLException</code> object in the chain;
132      * <code>null</code> if there are none
133      * @see #setNextException
134      */

135     public SQLException JavaDoc getNextException() {
136     return (next);
137     }
138
139     /**
140      * Adds an <code>SQLException</code> object to the end of the chain.
141      *
142      * @param ex the new exception that will be added to the end of
143      * the <code>SQLException</code> chain
144      * @see #getNextException
145      */

146     public synchronized void setNextException(SQLException JavaDoc ex) {
147     SQLException JavaDoc theEnd = this;
148     while (theEnd.next != null) {
149         theEnd = theEnd.next;
150     }
151     theEnd.next = ex;
152     }
153
154     /**
155      * @serial
156      */

157     private String JavaDoc SQLState;
158
159     /**
160      * @serial
161      */

162     private int vendorCode;
163
164     /**
165      * @serial
166      */

167     private SQLException JavaDoc next;
168 }
169
Popular Tags