Skip to content

Commit 54b5a19

Browse files
authored
Fix | Add null check for getObject() with LocalTime and LocalDate (#1250)
1 parent c97b863 commit 54b5a19

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

src/main/java/com/microsoft/sqlserver/jdbc/SQLServerResultSet.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2394,12 +2394,16 @@ public <T> T getObject(int columnIndex, Class<T> type) throws SQLException {
23942394
} else if (type == java.time.LocalDateTime.class || type == java.time.LocalDate.class
23952395
|| type == java.time.LocalTime.class) {
23962396
java.time.LocalDateTime ldt = getLocalDateTime(columnIndex);
2397-
if (type == java.time.LocalDateTime.class) {
2398-
returnValue = ldt;
2399-
} else if (type == java.time.LocalDate.class) {
2400-
returnValue = ldt.toLocalDate();
2397+
if (null == ldt) {
2398+
returnValue = null;
24012399
} else {
2402-
returnValue = ldt.toLocalTime();
2400+
if (type == java.time.LocalDateTime.class) {
2401+
returnValue = ldt;
2402+
} else if (type == java.time.LocalDate.class) {
2403+
returnValue = ldt.toLocalDate();
2404+
} else {
2405+
returnValue = ldt.toLocalTime();
2406+
}
24032407
}
24042408
} else if (type == java.time.OffsetDateTime.class) {
24052409
microsoft.sql.DateTimeOffset dateTimeOffset = getDateTimeOffset(columnIndex);

src/test/java/com/microsoft/sqlserver/jdbc/datatypes/DataTypesTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
import java.text.DateFormatSymbols;
2020
import java.time.Duration;
2121
import java.time.Instant;
22+
import java.time.LocalDate;
2223
import java.time.LocalDateTime;
24+
import java.time.LocalTime;
2325
import java.time.ZoneId;
2426
import java.time.temporal.ChronoUnit;
2527
import java.time.zone.ZoneOffsetTransition;
@@ -1821,6 +1823,28 @@ public void testGetLocalDateTimePriorGregorian() throws Exception {
18211823
}
18221824
}
18231825
}
1826+
1827+
@Test
1828+
public void testNullValuesWithGetObject() throws Exception {
1829+
String ldtTable = AbstractSQLGenerator.escapeIdentifier(RandomUtil.getIdentifier("ldtTable"));
1830+
try (Connection conn = getConnection(); Statement st = conn.createStatement();) {
1831+
TestUtils.dropTableIfExists(ldtTable, st);
1832+
st.execute("CREATE TABLE " + ldtTable + " (c1 datetime2)");
1833+
st.execute("INSERT INTO " + ldtTable + " VALUES (NULL)");
1834+
1835+
try (ResultSet rs = st.executeQuery("SELECT c1 FROM " + ldtTable);) {
1836+
rs.next();
1837+
LocalDateTime ldtActual = rs.getObject(1, LocalDateTime.class);
1838+
assertEquals(ldtActual, null);
1839+
LocalTime ltActual = rs.getObject(1, LocalTime.class);
1840+
assertEquals(ltActual, null);
1841+
LocalDate ldActual = rs.getObject(1, LocalDate.class);
1842+
assertEquals(ldActual, null);
1843+
} finally {
1844+
TestUtils.dropTableIfExists(ldtTable, st);
1845+
}
1846+
}
1847+
}
18241848

18251849
/**
18261850
* Test example from https://github.com/microsoft/mssql-jdbc/issues/1143

0 commit comments

Comments
 (0)