Skip to content

Commit 15ecb1b

Browse files
authored
Use more specific types instead of String when fetching JDBC3ResultSet.getBigDecimal (#666)
1 parent c57be93 commit 15ecb1b

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

src/main/java/org/sqlite/jdbc3/JDBC3ResultSet.java

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -153,14 +153,28 @@ public boolean wasNull() throws SQLException {
153153

154154
/** @see java.sql.ResultSet#getBigDecimal(int) */
155155
public BigDecimal getBigDecimal(int col) throws SQLException {
156-
final String stringValue = getString(col);
157-
if (stringValue == null) {
158-
return null;
156+
final int columnType = getColumnType(col);
157+
158+
if (columnType == Types.INTEGER) {
159+
long decimal = getLong(col);
160+
return BigDecimal.valueOf(decimal);
161+
} else if (columnType == Types.FLOAT || columnType == Types.DOUBLE) {
162+
final double decimal = getDouble(col);
163+
if (Double.isNaN(decimal)) {
164+
throw new SQLException("Bad value for type BigDecimal : Not a Number");
165+
} else {
166+
return BigDecimal.valueOf(decimal);
167+
}
159168
} else {
160-
try {
161-
return new BigDecimal(stringValue);
162-
} catch (NumberFormatException e) {
163-
throw new SQLException("Bad value for type BigDecimal : " + stringValue);
169+
final String stringValue = getString(col);
170+
if (stringValue == null) {
171+
return null;
172+
} else {
173+
try {
174+
return new BigDecimal(stringValue);
175+
} catch (NumberFormatException e) {
176+
throw new SQLException("Bad value for type BigDecimal : " + stringValue);
177+
}
164178
}
165179
}
166180
}

0 commit comments

Comments
 (0)