1/* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18package java.sql; 19 20import java.io.Serializable; 21 22/** 23 * An exception which is thrown when a JDBC driver unexpectedly truncates a data 24 * value either when reading (resulting in warning), or when writing data 25 * (resulting in an error). The {@code SQLState} error code for truncated data 26 * is {@code 01004}. 27 */ 28public class DataTruncation extends SQLWarning implements Serializable { 29 30 private static final long serialVersionUID = 6464298989504059473L; 31 32 private int index = 0; 33 34 private boolean parameter = false; 35 36 private boolean read = false; 37 38 private int dataSize = 0; 39 40 private int transferSize = 0; 41 42 private static final String THE_REASON = "Data truncation"; //$NON-NLS-1$ 43 44 private static final String THE_SQLSTATE = "01004"; //$NON-NLS-1$ 45 46 private static final int THE_ERROR_CODE = 0; 47 48 /** 49 * Creates the {@code DataTruncation} object. The reason is set to {@code 50 * "Data truncation"}, the {@code ErrorCode} is set to the {@code 51 * SQLException} default value, and the other fields are set to the values 52 * supplied as arguments. 53 * 54 * @param index 55 * the Index value of the column value or parameter that was 56 * truncated. 57 * @param parameter 58 * {@code true} if it was a parameter value that was truncated, 59 * {@code false} otherwise. 60 * @param read 61 * {@code true} if the truncation occurred on a read operation, 62 * {@code false} otherwise. 63 * @param dataSize 64 * the original size of the truncated data. 65 * @param transferSize 66 * the size of the data after truncation. 67 */ 68 public DataTruncation(int index, boolean parameter, boolean read, 69 int dataSize, int transferSize) { 70 super(THE_REASON, THE_SQLSTATE, THE_ERROR_CODE); 71 this.index = index; 72 this.parameter = parameter; 73 this.read = read; 74 this.dataSize = dataSize; 75 this.transferSize = transferSize; 76 } 77 78 /** 79 * Gets the number of bytes of data that should have been read/written. 80 * 81 * @return the number of bytes that should have been read or written. The 82 * value is set to {@code -1} if the size is unknown. 83 */ 84 public int getDataSize() { 85 return dataSize; 86 } 87 88 /** 89 * Gets the index of the column or of the parameter that was truncated. 90 * 91 * @return the index number of the column or of the parameter. 92 */ 93 public int getIndex() { 94 return index; 95 } 96 97 /** 98 * Gets whether the value truncated was a parameter value or a column value. 99 * 100 * @return {@code true} if the value truncated was a parameter value, 101 * {@code false} if it was a column value. 102 */ 103 public boolean getParameter() { 104 return parameter; 105 } 106 107 /** 108 * Gets whether the value was truncated on a read operation or a write 109 * operation 110 * 111 * @return {@code true} if the value was truncated on a read operation, 112 * {@code false} otherwise. 113 */ 114 public boolean getRead() { 115 return read; 116 } 117 118 /** 119 * Gets the number of bytes of data that was actually read or written. 120 * 121 * @return the number of bytes actually read/written. The value may be set 122 * to {@code -1} if the size is unknown. 123 */ 124 public int getTransferSize() { 125 return transferSize; 126 } 127} 128