diff src/main/java/nl/cwi/monetdb/jdbc/MonetResultSet.java @ 9:c37a76cc1e6e

Corrected ResultSet methods: getByte(), getBigDecimal(), getShort(), getInt(), getLong(), getFloat() and getDouble() in case the conversion to the native type failed due to a Number Format conversion error. It used to silently ignore the conversion error and return 0 instead, which is not correct. Now it throws an SQLException with message "Could not convert value to a number." and SQLstate "22003" meaning: Numeric value out of range.
author Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
date Thu, 29 Sep 2016 16:43:45 +0200 (2016-09-29)
parents a27ee2cb14a0
children 4ef332ad8ec0
line wrap: on
line diff
--- a/src/main/java/nl/cwi/monetdb/jdbc/MonetResultSet.java
+++ b/src/main/java/nl/cwi/monetdb/jdbc/MonetResultSet.java
@@ -669,11 +669,9 @@ public class MonetResultSet extends Mone
 				return null;
 			}
 			lastReadWasNull = false;
-			try {
-				return new BigDecimal(val);
-			} catch (NumberFormatException e) {
-				return BigDecimal.ZERO;
-			}
+			return new BigDecimal(val);
+		} catch (NumberFormatException e) {
+			throw newSQLNumberFormatException(e);
 		} catch (IndexOutOfBoundsException e) {
 			throw newSQLInvalidColumnIndexException(columnIndex);
 		}
@@ -701,13 +699,12 @@ public class MonetResultSet extends Mone
 				return null;
 			}
 			lastReadWasNull = false;
-			try {
-				BigDecimal bd = new BigDecimal(val);
-				bd.setScale(scale);
-				return bd;
-			} catch (NumberFormatException e) {
-				return BigDecimal.ZERO;
-			}
+
+			BigDecimal bd = new BigDecimal(val);
+			bd.setScale(scale);
+			return bd;
+		} catch (NumberFormatException e) {
+			throw newSQLNumberFormatException(e);
 		} catch (IndexOutOfBoundsException e) {
 			throw newSQLInvalidColumnIndexException(columnIndex);
 		}
@@ -846,12 +843,9 @@ public class MonetResultSet extends Mone
 				return (byte) 0;
 			}
 			lastReadWasNull = false;
-			try {
-				return Byte.parseByte(val);
-			} catch (NumberFormatException e) {
-				// ignore parse error, return the default: 0
-				return (byte) 0;
-			}
+			return Byte.parseByte(val);
+		} catch (NumberFormatException e) {
+			throw newSQLNumberFormatException(e);
 		} catch (IndexOutOfBoundsException e) {
 			throw newSQLInvalidColumnIndexException(columnIndex);
 		}
@@ -994,12 +988,9 @@ public class MonetResultSet extends Mone
 				return 0;
 			}
 			lastReadWasNull = false;
-			try {
-				return Double.parseDouble(val);
-			} catch (NumberFormatException e) {
-				// ignore conversion error, return the default: 0
-				return 0;
-			}
+			return Double.parseDouble(val);
+		} catch (NumberFormatException e) {
+			throw newSQLNumberFormatException(e);
 		} catch (IndexOutOfBoundsException e) {
 			throw newSQLInvalidColumnIndexException(columnIndex);
 		}
@@ -1117,12 +1108,9 @@ public class MonetResultSet extends Mone
 				return 0;
 			}
 			lastReadWasNull = false;
-			try {
-				return Float.parseFloat(val);
-			} catch (NumberFormatException e) {
-				// ignore conversion error, return the default: 0
-				return 0;
-			}
+			return Float.parseFloat(val);
+		} catch (NumberFormatException e) {
+			throw newSQLNumberFormatException(e);
 		} catch (IndexOutOfBoundsException e) {
 			throw newSQLInvalidColumnIndexException(columnIndex);
 		}
@@ -1160,12 +1148,9 @@ public class MonetResultSet extends Mone
 				return 0;
 			}
 			lastReadWasNull = false;
-			try {
-				return Integer.parseInt(val);
-			} catch (NumberFormatException e) {
-				// ignore conversion error, return the default: 0
-				return 0;
-			}
+			return Integer.parseInt(val);
+		} catch (NumberFormatException e) {
+			throw newSQLNumberFormatException(e);
 		} catch (IndexOutOfBoundsException e) {
 			throw newSQLInvalidColumnIndexException(columnIndex);
 		}
@@ -1211,12 +1196,9 @@ public class MonetResultSet extends Mone
 				if (len > 2 && val.endsWith("@0"))
 					val = val.substring(0, len-2);
 			}
-			try {
-				return Long.parseLong(val);
-			} catch (NumberFormatException e) {
-				// ignore conversion error, return the default: 0
-				return 0;
-			}
+			return Long.parseLong(val);
+		} catch (NumberFormatException e) {
+			throw newSQLNumberFormatException(e);
 		} catch (IndexOutOfBoundsException e) {
 			throw newSQLInvalidColumnIndexException(columnIndex);
 		}
@@ -2413,12 +2395,9 @@ public class MonetResultSet extends Mone
 				return 0;
 			}
 			lastReadWasNull = false;
-			try {
-				return Short.parseShort(val);
-			} catch (NumberFormatException e) {
-				// ignore conversion error, return the default: 0
-				return 0;
-			}
+			return Short.parseShort(val);
+		} catch (NumberFormatException e) {
+			throw newSQLNumberFormatException(e);
 		} catch (IndexOutOfBoundsException e) {
 			throw newSQLInvalidColumnIndexException(columnIndex);
 		}
@@ -3724,7 +3703,7 @@ public class MonetResultSet extends Mone
 	 * Small helper method that formats the "Invalid Column Index number ..." message
 	 * and creates a new SQLException object whose SQLState is set to "M1M05".
 	 *
-	 * @param name the method name
+	 * @param colIdx the column index numberr
 	 * @return a new created SQLException object with SQLState M1M05
 	 */
 	private final static SQLException newSQLInvalidColumnIndexException(int colIdx) {
@@ -3732,6 +3711,17 @@ public class MonetResultSet extends Mone
 	}
 
 	/**
+	 * Small helper method that formats the "Could not convert value to a number" message
+	 * and creates a new SQLException object whose SQLState is set to "22003": Numeric value out of range.
+	 *
+	 * @param error the NumberFormatException
+	 * @return a new created SQLException object with SQLState 22003
+	 */
+	private final static SQLException newSQLNumberFormatException(NumberFormatException error) {
+		return new SQLException("Could not convert value to a number. " + error.getMessage(), "22003");
+	}
+
+	/**
 	 * Small helper method that formats the "Method ... not implemented" message
 	 * and creates a new SQLFeatureNotSupportedException object
 	 * whose SQLState is set to "0A000".