Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/sql/SQLException.java
38829 views
/*1* Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.sql;2627import java.util.Iterator;28import java.util.NoSuchElementException;29import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;3031/**32* <P>An exception that provides information on a database access33* error or other errors.34*35* <P>Each <code>SQLException</code> provides several kinds of information:36* <UL>37* <LI> a string describing the error. This is used as the Java Exception38* message, available via the method <code>getMesasge</code>.39* <LI> a "SQLstate" string, which follows either the XOPEN SQLstate conventions40* or the SQL:2003 conventions.41* The values of the SQLState string are described in the appropriate spec.42* The <code>DatabaseMetaData</code> method <code>getSQLStateType</code>43* can be used to discover whether the driver returns the XOPEN type or44* the SQL:2003 type.45* <LI> an integer error code that is specific to each vendor. Normally this will46* be the actual error code returned by the underlying database.47* <LI> a chain to a next Exception. This can be used to provide additional48* error information.49* <LI> the causal relationship, if any for this <code>SQLException</code>.50* </UL>51*/52public class SQLException extends java.lang.Exception53implements Iterable<Throwable> {5455/**56* Constructs a <code>SQLException</code> object with a given57* <code>reason</code>, <code>SQLState</code> and58* <code>vendorCode</code>.59*60* The <code>cause</code> is not initialized, and may subsequently be61* initialized by a call to the62* {@link Throwable#initCause(java.lang.Throwable)} method.63* <p>64* @param reason a description of the exception65* @param SQLState an XOPEN or SQL:2003 code identifying the exception66* @param vendorCode a database vendor-specific exception code67*/68public SQLException(String reason, String SQLState, int vendorCode) {69super(reason);70this.SQLState = SQLState;71this.vendorCode = vendorCode;72if (!(this instanceof SQLWarning)) {73if (DriverManager.getLogWriter() != null) {74DriverManager.println("SQLState(" + SQLState +75") vendor code(" + vendorCode + ")");76printStackTrace(DriverManager.getLogWriter());77}78}79}808182/**83* Constructs a <code>SQLException</code> object with a given84* <code>reason</code> and <code>SQLState</code>.85*86* The <code>cause</code> is not initialized, and may subsequently be87* initialized by a call to the88* {@link Throwable#initCause(java.lang.Throwable)} method. The vendor code89* is initialized to 0.90* <p>91* @param reason a description of the exception92* @param SQLState an XOPEN or SQL:2003 code identifying the exception93*/94public SQLException(String reason, String SQLState) {95super(reason);96this.SQLState = SQLState;97this.vendorCode = 0;98if (!(this instanceof SQLWarning)) {99if (DriverManager.getLogWriter() != null) {100printStackTrace(DriverManager.getLogWriter());101DriverManager.println("SQLException: SQLState(" + SQLState + ")");102}103}104}105106/**107* Constructs a <code>SQLException</code> object with a given108* <code>reason</code>. The <code>SQLState</code> is initialized to109* <code>null</code> and the vendor code is initialized to 0.110*111* The <code>cause</code> is not initialized, and may subsequently be112* initialized by a call to the113* {@link Throwable#initCause(java.lang.Throwable)} method.114* <p>115* @param reason a description of the exception116*/117public SQLException(String reason) {118super(reason);119this.SQLState = null;120this.vendorCode = 0;121if (!(this instanceof SQLWarning)) {122if (DriverManager.getLogWriter() != null) {123printStackTrace(DriverManager.getLogWriter());124}125}126}127128/**129* Constructs a <code>SQLException</code> object.130* The <code>reason</code>, <code>SQLState</code> are initialized131* to <code>null</code> and the vendor code is initialized to 0.132*133* The <code>cause</code> is not initialized, and may subsequently be134* initialized by a call to the135* {@link Throwable#initCause(java.lang.Throwable)} method.136*137*/138public SQLException() {139super();140this.SQLState = null;141this.vendorCode = 0;142if (!(this instanceof SQLWarning)) {143if (DriverManager.getLogWriter() != null) {144printStackTrace(DriverManager.getLogWriter());145}146}147}148149/**150* Constructs a <code>SQLException</code> object with a given151* <code>cause</code>.152* The <code>SQLState</code> is initialized153* to <code>null</code> and the vendor code is initialized to 0.154* The <code>reason</code> is initialized to <code>null</code> if155* <code>cause==null</code> or to <code>cause.toString()</code> if156* <code>cause!=null</code>.157* <p>158* @param cause the underlying reason for this <code>SQLException</code>159* (which is saved for later retrieval by the <code>getCause()</code> method);160* may be null indicating the cause is non-existent or unknown.161* @since 1.6162*/163public SQLException(Throwable cause) {164super(cause);165166if (!(this instanceof SQLWarning)) {167if (DriverManager.getLogWriter() != null) {168printStackTrace(DriverManager.getLogWriter());169}170}171}172173/**174* Constructs a <code>SQLException</code> object with a given175* <code>reason</code> and <code>cause</code>.176* The <code>SQLState</code> is initialized to <code>null</code>177* and the vendor code is initialized to 0.178* <p>179* @param reason a description of the exception.180* @param cause the underlying reason for this <code>SQLException</code>181* (which is saved for later retrieval by the <code>getCause()</code> method);182* may be null indicating the cause is non-existent or unknown.183* @since 1.6184*/185public SQLException(String reason, Throwable cause) {186super(reason,cause);187188if (!(this instanceof SQLWarning)) {189if (DriverManager.getLogWriter() != null) {190printStackTrace(DriverManager.getLogWriter());191}192}193}194195/**196* Constructs a <code>SQLException</code> object with a given197* <code>reason</code>, <code>SQLState</code> and <code>cause</code>.198* The vendor code is initialized to 0.199* <p>200* @param reason a description of the exception.201* @param sqlState an XOPEN or SQL:2003 code identifying the exception202* @param cause the underlying reason for this <code>SQLException</code>203* (which is saved for later retrieval by the204* <code>getCause()</code> method); may be null indicating205* the cause is non-existent or unknown.206* @since 1.6207*/208public SQLException(String reason, String sqlState, Throwable cause) {209super(reason,cause);210211this.SQLState = sqlState;212this.vendorCode = 0;213if (!(this instanceof SQLWarning)) {214if (DriverManager.getLogWriter() != null) {215printStackTrace(DriverManager.getLogWriter());216DriverManager.println("SQLState(" + SQLState + ")");217}218}219}220221/**222* Constructs a <code>SQLException</code> object with a given223* <code>reason</code>, <code>SQLState</code>, <code>vendorCode</code>224* and <code>cause</code>.225* <p>226* @param reason a description of the exception227* @param sqlState an XOPEN or SQL:2003 code identifying the exception228* @param vendorCode a database vendor-specific exception code229* @param cause the underlying reason for this <code>SQLException</code>230* (which is saved for later retrieval by the <code>getCause()</code> method);231* may be null indicating the cause is non-existent or unknown.232* @since 1.6233*/234public SQLException(String reason, String sqlState, int vendorCode, Throwable cause) {235super(reason,cause);236237this.SQLState = sqlState;238this.vendorCode = vendorCode;239if (!(this instanceof SQLWarning)) {240if (DriverManager.getLogWriter() != null) {241DriverManager.println("SQLState(" + SQLState +242") vendor code(" + vendorCode + ")");243printStackTrace(DriverManager.getLogWriter());244}245}246}247248/**249* Retrieves the SQLState for this <code>SQLException</code> object.250*251* @return the SQLState value252*/253public String getSQLState() {254return (SQLState);255}256257/**258* Retrieves the vendor-specific exception code259* for this <code>SQLException</code> object.260*261* @return the vendor's error code262*/263public int getErrorCode() {264return (vendorCode);265}266267/**268* Retrieves the exception chained to this269* <code>SQLException</code> object by setNextException(SQLException ex).270*271* @return the next <code>SQLException</code> object in the chain;272* <code>null</code> if there are none273* @see #setNextException274*/275public SQLException getNextException() {276return (next);277}278279/**280* Adds an <code>SQLException</code> object to the end of the chain.281*282* @param ex the new exception that will be added to the end of283* the <code>SQLException</code> chain284* @see #getNextException285*/286public void setNextException(SQLException ex) {287288SQLException current = this;289for(;;) {290SQLException next=current.next;291if (next != null) {292current = next;293continue;294}295296if (nextUpdater.compareAndSet(current,null,ex)) {297return;298}299current=current.next;300}301}302303/**304* Returns an iterator over the chained SQLExceptions. The iterator will305* be used to iterate over each SQLException and its underlying cause306* (if any).307*308* @return an iterator over the chained SQLExceptions and causes in the proper309* order310*311* @since 1.6312*/313public Iterator<Throwable> iterator() {314315return new Iterator<Throwable>() {316317SQLException firstException = SQLException.this;318SQLException nextException = firstException.getNextException();319Throwable cause = firstException.getCause();320321public boolean hasNext() {322if(firstException != null || nextException != null || cause != null)323return true;324return false;325}326327public Throwable next() {328Throwable throwable = null;329if(firstException != null){330throwable = firstException;331firstException = null;332}333else if(cause != null){334throwable = cause;335cause = cause.getCause();336}337else if(nextException != null){338throwable = nextException;339cause = nextException.getCause();340nextException = nextException.getNextException();341}342else343throw new NoSuchElementException();344return throwable;345}346347public void remove() {348throw new UnsupportedOperationException();349}350351};352353}354355/**356* @serial357*/358private String SQLState;359360/**361* @serial362*/363private int vendorCode;364365/**366* @serial367*/368private volatile SQLException next;369370private static final AtomicReferenceFieldUpdater<SQLException,SQLException> nextUpdater =371AtomicReferenceFieldUpdater.newUpdater(SQLException.class,SQLException.class,"next");372373private static final long serialVersionUID = 2135244094396331484L;374}375376377