Skip to content

Commit 5c5e8e1

Browse files
committed
Fix for Bug#104559 (33232419), ResultSet.getObject(i,
java.util.Date.class) throws NPE when the value is null.
1 parent 2ad747a commit 5c5e8e1

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

CHANGES

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
Version 8.0.27
55

6+
- Fix for Bug#104559 (33232419), ResultSet.getObject(i, java.util.Date.class) throws NPE when the value is null.
7+
68
- WL#14707, Support OCI IAM authentication.
79

810
- WL#14660, Testsuite with support for single MySQL server instance.

src/main/user-impl/java/com/mysql/cj/jdbc/result/ResultSetImpl.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1343,7 +1343,8 @@ public <T> T getObject(int columnIndex, Class<T> type) throws SQLException {
13431343
return (T) getTimestamp(columnIndex);
13441344

13451345
} else if (type.equals(java.util.Date.class)) {
1346-
return (T) java.util.Date.from(getTimestamp(columnIndex).toInstant());
1346+
Timestamp ts = getTimestamp(columnIndex);
1347+
return ts == null ? null : (T) java.util.Date.from(ts.toInstant());
13471348

13481349
} else if (type.equals(java.util.Calendar.class)) {
13491350
return (T) getUtilCalendar(columnIndex);

src/test/java/testsuite/regression/DateTimeRegressionTest.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
package testsuite.regression;
3131

3232
import static org.junit.jupiter.api.Assertions.assertEquals;
33+
import static org.junit.jupiter.api.Assertions.assertFalse;
34+
import static org.junit.jupiter.api.Assertions.assertNull;
3335
import static org.junit.jupiter.api.Assertions.assertTrue;
3436
import static org.junit.jupiter.api.Assumptions.assumeTrue;
3537

@@ -1110,6 +1112,8 @@ public void testBug101413() throws Exception {
11101112
createTable("testBug101413", "(createtime1 TIMESTAMP, createtime2 DATETIME)");
11111113

11121114
Properties props = new Properties();
1115+
props.setProperty(PropertyKey.sslMode.getKeyName(), "DISABLED");
1116+
props.setProperty(PropertyKey.allowPublicKeyRetrieval.getKeyName(), "true");
11131117
for (boolean forceConnectionTimeZoneToSession : new boolean[] { false, true }) {
11141118
for (boolean preserveInstants : new boolean[] { false, true }) {
11151119
for (boolean useSSPS : new boolean[] { false, true }) {
@@ -1136,4 +1140,25 @@ public void testBug101413() throws Exception {
11361140
}
11371141
}
11381142
}
1143+
1144+
/**
1145+
* Tests fix for Bug#104559 (33232419), ResultSet.getObject(i, java.util.Date.class) throws NPE when the value is null.
1146+
*
1147+
* @throws Exception
1148+
*/
1149+
@Test
1150+
public void testBug104559() throws Exception {
1151+
createTable("testBug104559", "(`dt` DATE DEFAULT NULL)");
1152+
this.stmt.executeUpdate("INSERT INTO testBug104559 VALUES (null)");
1153+
1154+
Properties props = new Properties();
1155+
props.setProperty(PropertyKey.sslMode.getKeyName(), "DISABLED");
1156+
props.setProperty(PropertyKey.allowPublicKeyRetrieval.getKeyName(), "true");
1157+
Connection con = getConnectionWithProps(props);
1158+
Statement st = con.createStatement();
1159+
this.rs = st.executeQuery("SELECT * FROM testBug104559");
1160+
assertTrue(this.rs.next());
1161+
assertNull(this.rs.getObject("dt", java.util.Date.class));
1162+
assertFalse(this.rs.next());
1163+
}
11391164
}

0 commit comments

Comments
 (0)