1. /*
  2. * @(#)SQLException.java 1.26 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.sql;
  8. /**
  9. * <P>An exception that provides information on a database access
  10. * error or other errors.
  11. *
  12. * <P>Each <code>SQLException</code> provides several kinds of information:
  13. * <UL>
  14. * <LI> a string describing the error. This is used as the Java Exception
  15. * message, available via the method <code>getMesage</code>.
  16. * <LI> a "SQLstate" string, which follows either the XOPEN SQLstate conventions
  17. * or the SQL 99 conventions.
  18. * The values of the SQLState string are described in the appropriate spec.
  19. * The <code>DatabaseMetaData</code> method <code>getSQLStateType</code>
  20. * can be used to discover whether the driver returns the XOPEN type or
  21. * the SQL 99 type.
  22. * <LI> an integer error code that is specific to each vendor. Normally this will
  23. * be the actual error code returned by the underlying database.
  24. * <LI> a chain to a next Exception. This can be used to provide additional
  25. * error information.
  26. * </UL>
  27. */
  28. public class SQLException extends java.lang.Exception {
  29. /**
  30. * Constructs a fully-specified <code>SQLException</code> object.
  31. *
  32. * @param reason a description of the exception
  33. * @param SQLState an XOPEN or SQL 99 code identifying the exception
  34. * @param vendorCode a database vendor-specific exception code
  35. */
  36. public SQLException(String reason, String SQLState, int vendorCode) {
  37. super(reason);
  38. this.SQLState = SQLState;
  39. this.vendorCode = vendorCode;
  40. if (!(this instanceof SQLWarning)) {
  41. if (DriverManager.getLogWriter() != null) {
  42. DriverManager.println("SQLException: SQLState(" + SQLState +
  43. ") vendor code(" + vendorCode + ")");
  44. printStackTrace(DriverManager.getLogWriter());
  45. }
  46. }
  47. }
  48. /**
  49. * Constructs an <code>SQLException</code> object with the given reason and
  50. * SQLState; the <code>vendorCode</code> field defaults to 0.
  51. *
  52. * @param reason a description of the exception
  53. * @param SQLState an XOPEN or SQL 99 code identifying the exception
  54. */
  55. public SQLException(String reason, String SQLState) {
  56. super(reason);
  57. this.SQLState = SQLState;
  58. this.vendorCode = 0;
  59. if (!(this instanceof SQLWarning)) {
  60. if (DriverManager.getLogWriter() != null) {
  61. printStackTrace(DriverManager.getLogWriter());
  62. DriverManager.println("SQLException: SQLState(" + SQLState + ")");
  63. }
  64. }
  65. }
  66. /**
  67. * Constructs an <code>SQLException</code> object with a reason;
  68. * the <code>SQLState</code> field defaults to <code>null</code>, and
  69. * the <code>vendorCode</code> field defaults to 0.
  70. *
  71. * @param reason a description of the exception
  72. */
  73. public SQLException(String reason) {
  74. super(reason);
  75. this.SQLState = null;
  76. this.vendorCode = 0;
  77. if (!(this instanceof SQLWarning)) {
  78. if (DriverManager.getLogWriter() != null) {
  79. printStackTrace(DriverManager.getLogWriter());
  80. }
  81. }
  82. }
  83. /**
  84. * Constructs an <code>SQLException</code> object;
  85. * the <code>reason</code> field defaults to null,
  86. * the <code>SQLState</code> field defaults to <code>null</code>, and
  87. * the <code>vendorCode</code> field defaults to 0.
  88. *
  89. */
  90. public SQLException() {
  91. super();
  92. this.SQLState = null;
  93. this.vendorCode = 0;
  94. if (!(this instanceof SQLWarning)) {
  95. if (DriverManager.getLogWriter() != null) {
  96. printStackTrace(DriverManager.getLogWriter());
  97. }
  98. }
  99. }
  100. /**
  101. * Retrieves the SQLState for this <code>SQLException</code> object.
  102. *
  103. * @return the SQLState value
  104. */
  105. public String getSQLState() {
  106. return (SQLState);
  107. }
  108. /**
  109. * Retrieves the vendor-specific exception code
  110. * for this <code>SQLException</code> object.
  111. *
  112. * @return the vendor's error code
  113. */
  114. public int getErrorCode() {
  115. return (vendorCode);
  116. }
  117. /**
  118. * Retrieves the exception chained to this
  119. * <code>SQLException</code> object.
  120. *
  121. * @return the next <code>SQLException</code> object in the chain;
  122. * <code>null</code> if there are none
  123. * @see #setNextException
  124. */
  125. public SQLException getNextException() {
  126. return (next);
  127. }
  128. /**
  129. * Adds an <code>SQLException</code> object to the end of the chain.
  130. *
  131. * @param ex the new exception that will be added to the end of
  132. * the <code>SQLException</code> chain
  133. * @see #getNextException
  134. */
  135. public synchronized void setNextException(SQLException ex) {
  136. SQLException theEnd = this;
  137. while (theEnd.next != null) {
  138. theEnd = theEnd.next;
  139. }
  140. theEnd.next = ex;
  141. }
  142. /**
  143. * @serial
  144. */
  145. private String SQLState;
  146. /**
  147. * @serial
  148. */
  149. private int vendorCode;
  150. /**
  151. * @serial
  152. */
  153. private SQLException next;
  154. }