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