001 /*
002 * Copyright 1996-2005 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package java.sql;
027
028 /**
029 * An exception thrown as a <code>DataTruncation</code> exception
030 * (on writes) or reported as a
031 * <code>DataTruncation</code> warning (on reads)
032 * when a data values is unexpectedly truncated for reasons other than its having
033 * execeeded <code>MaxFieldSize</code>.
034 *
035 * <P>The SQLstate for a <code>DataTruncation</code> during read is <code>01004</code>.
036 * <P>The SQLstate for a <code>DataTruncation</code> during write is <code>22001</code>.
037 */
038
039 public class DataTruncation extends SQLWarning {
040
041 /**
042 * Creates a <code>DataTruncation</code> object
043 * with the SQLState initialized
044 * to 01004 when <code>read</code> is set to <code>true</code> and 22001
045 * when <code>read</code> is set to <code>false</code>,
046 * the reason set to "Data truncation", the
047 * vendor code set to 0, and
048 * the other fields set to the given values.
049 * The <code>cause</code> is not initialized, and may subsequently be
050 * initialized by a call to the
051 * {@link Throwable#initCause(java.lang.Throwable)} method.
052 * <p>
053 *
054 * @param index The index of the parameter or column value
055 * @param parameter true if a parameter value was truncated
056 * @param read true if a read was truncated
057 * @param dataSize the original size of the data
058 * @param transferSize the size after truncation
059 */
060 public DataTruncation(int index, boolean parameter, boolean read,
061 int dataSize, int transferSize) {
062 super ("Data truncation", read == true ? "01004" : "22001");
063 this .index = index;
064 this .parameter = parameter;
065 this .read = read;
066 this .dataSize = dataSize;
067 this .transferSize = transferSize;
068
069 }
070
071 /**
072 * Creates a <code>DataTruncation</code> object
073 * with the SQLState initialized
074 * to 01004 when <code>read</code> is set to <code>true</code> and 22001
075 * when <code>read</code> is set to <code>false</code>,
076 * the reason set to "Data truncation", the
077 * vendor code set to 0, and
078 * the other fields set to the given values.
079 * <p>
080 *
081 * @param index The index of the parameter or column value
082 * @param parameter true if a parameter value was truncated
083 * @param read true if a read was truncated
084 * @param dataSize the original size of the data
085 * @param transferSize the size after truncation
086 * @param cause the underlying reason for this <code>DataTruncation</code>
087 * (which is saved for later retrieval by the <code>getCause()</code> method);
088 * may be null indicating the cause is non-existent or unknown.
089 *
090 * @since 1.6
091 */
092 public DataTruncation(int index, boolean parameter, boolean read,
093 int dataSize, int transferSize, Throwable cause) {
094 super ("Data truncation", read == true ? "01004" : "22001",
095 cause);
096 this .index = index;
097 this .parameter = parameter;
098 this .read = read;
099 this .dataSize = dataSize;
100 this .transferSize = transferSize;
101 }
102
103 /**
104 * Retrieves the index of the column or parameter that was truncated.
105 *
106 * <P>This may be -1 if the column or parameter index is unknown, in
107 * which case the <code>parameter</code> and <code>read</code> fields should be ignored.
108 *
109 * @return the index of the truncated paramter or column value
110 */
111 public int getIndex() {
112 return index;
113 }
114
115 /**
116 * Indicates whether the value truncated was a parameter value or
117 * a column value.
118 *
119 * @return <code>true</code> if the value truncated was a parameter;
120 * <code>false</code> if it was a column value
121 */
122 public boolean getParameter() {
123 return parameter;
124 }
125
126 /**
127 * Indicates whether or not the value was truncated on a read.
128 *
129 * @return <code>true</code> if the value was truncated when read from
130 * the database; <code>false</code> if the data was truncated on a write
131 */
132 public boolean getRead() {
133 return read;
134 }
135
136 /**
137 * Gets the number of bytes of data that should have been transferred.
138 * This number may be approximate if data conversions were being
139 * performed. The value may be <code>-1</code> if the size is unknown.
140 *
141 * @return the number of bytes of data that should have been transferred
142 */
143 public int getDataSize() {
144 return dataSize;
145 }
146
147 /**
148 * Gets the number of bytes of data actually transferred.
149 * The value may be <code>-1</code> if the size is unknown.
150 *
151 * @return the number of bytes of data actually transferred
152 */
153 public int getTransferSize() {
154 return transferSize;
155 }
156
157 /**
158 * @serial
159 */
160 private int index;
161
162 /**
163 * @serial
164 */
165 private boolean parameter;
166
167 /**
168 * @serial
169 */
170 private boolean read;
171
172 /**
173 * @serial
174 */
175 private int dataSize;
176
177 /**
178 * @serial
179 */
180 private int transferSize;
181
182 /**
183 * @serial
184 */
185 private static final long serialVersionUID = 6464298989504059473L;
186
187 }
|