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