Skip to content

Commit c0e5d00

Browse files
committed
Jdbc4SqlXmlHandler returns null as documented (instead of throwing NPE)
Issue: SPR-13782 (cherry picked from commit 78dad4c)
1 parent 7382315 commit c0e5d00

File tree

2 files changed

+28
-15
lines changed

2 files changed

+28
-15
lines changed

spring-jdbc/src/main/java/org/springframework/jdbc/support/xml/Jdbc4SqlXmlHandler.java

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -51,37 +51,51 @@ public class Jdbc4SqlXmlHandler implements SqlXmlHandler {
5151
//-------------------------------------------------------------------------
5252

5353
public String getXmlAsString(ResultSet rs, String columnName) throws SQLException {
54-
return rs.getSQLXML(columnName).getString();
54+
SQLXML xmlObject = rs.getSQLXML(columnName);
55+
return (xmlObject != null ? xmlObject.getString() : null);
5556
}
5657

5758
public String getXmlAsString(ResultSet rs, int columnIndex) throws SQLException {
58-
return rs.getSQLXML(columnIndex).getString();
59+
SQLXML xmlObject = rs.getSQLXML(columnIndex);
60+
return (xmlObject != null ? xmlObject.getString() : null);
5961
}
6062

6163
public InputStream getXmlAsBinaryStream(ResultSet rs, String columnName) throws SQLException {
62-
return rs.getSQLXML(columnName).getBinaryStream();
64+
SQLXML xmlObject = rs.getSQLXML(columnName);
65+
return (xmlObject != null ? xmlObject.getBinaryStream() : null);
6366
}
6467

6568
public InputStream getXmlAsBinaryStream(ResultSet rs, int columnIndex) throws SQLException {
66-
return rs.getSQLXML(columnIndex).getBinaryStream();
69+
SQLXML xmlObject = rs.getSQLXML(columnIndex);
70+
return (xmlObject != null ? xmlObject.getBinaryStream() : null);
6771
}
6872

6973
public Reader getXmlAsCharacterStream(ResultSet rs, String columnName) throws SQLException {
70-
return rs.getSQLXML(columnName).getCharacterStream();
74+
SQLXML xmlObject = rs.getSQLXML(columnName);
75+
return (xmlObject != null ? xmlObject.getCharacterStream() : null);
7176
}
7277

7378
public Reader getXmlAsCharacterStream(ResultSet rs, int columnIndex) throws SQLException {
74-
return rs.getSQLXML(columnIndex).getCharacterStream();
79+
SQLXML xmlObject = rs.getSQLXML(columnIndex);
80+
return (xmlObject != null ? xmlObject.getCharacterStream() : null);
7581
}
7682

7783
@SuppressWarnings("unchecked")
7884
public Source getXmlAsSource(ResultSet rs, String columnName, Class sourceClass) throws SQLException {
79-
return rs.getSQLXML(columnName).getSource(sourceClass != null ? sourceClass : DOMSource.class);
85+
SQLXML xmlObject = rs.getSQLXML(columnName);
86+
if (xmlObject == null) {
87+
return null;
88+
}
89+
return xmlObject.getSource(sourceClass != null ? sourceClass : DOMSource.class);
8090
}
8191

8292
@SuppressWarnings("unchecked")
8393
public Source getXmlAsSource(ResultSet rs, int columnIndex, Class sourceClass) throws SQLException {
84-
return rs.getSQLXML(columnIndex).getSource(sourceClass != null ? sourceClass : DOMSource.class);
94+
SQLXML xmlObject = rs.getSQLXML(columnIndex);
95+
if (xmlObject == null) {
96+
return null;
97+
}
98+
return xmlObject.getSource(sourceClass != null ? sourceClass : DOMSource.class);
8599
}
86100

87101

spring-jdbc/src/main/java/org/springframework/jdbc/support/xml/SqlXmlHandler.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,7 +20,6 @@
2020
import java.io.Reader;
2121
import java.sql.ResultSet;
2222
import java.sql.SQLException;
23-
2423
import javax.xml.transform.Source;
2524

2625
import org.w3c.dom.Document;
@@ -112,7 +111,7 @@ public interface SqlXmlHandler {
112111
* database and driver.
113112
* @param rs the ResultSet to retrieve the content from
114113
* @param columnName the column name to use
115-
* @return the content as character stream
114+
* @return the content as character stream, or {@code null} in case of SQL NULL
116115
* @throws SQLException if thrown by JDBC methods
117116
* @see java.sql.ResultSet#getSQLXML
118117
* @see java.sql.SQLXML#getCharacterStream
@@ -126,7 +125,7 @@ public interface SqlXmlHandler {
126125
* database and driver.
127126
* @param rs the ResultSet to retrieve the content from
128127
* @param columnIndex the column index to use
129-
* @return the content as character stream
128+
* @return the content as character stream, or {@code null} in case of SQL NULL
130129
* @throws SQLException if thrown by JDBC methods
131130
* @see java.sql.ResultSet#getSQLXML
132131
* @see java.sql.SQLXML#getCharacterStream
@@ -141,7 +140,7 @@ public interface SqlXmlHandler {
141140
* @param rs the ResultSet to retrieve the content from
142141
* @param columnName the column name to use
143142
* @param sourceClass the implementation class to be used
144-
* @return the content as character stream
143+
* @return the content as character stream, or {@code null} in case of SQL NULL
145144
* @throws SQLException if thrown by JDBC methods
146145
* @see java.sql.ResultSet#getSQLXML
147146
* @see java.sql.SQLXML#getSource
@@ -156,7 +155,7 @@ public interface SqlXmlHandler {
156155
* @param rs the ResultSet to retrieve the content from
157156
* @param columnIndex the column index to use
158157
* @param sourceClass the implementation class to be used
159-
* @return the content as character stream
158+
* @return the content as character stream, or {@code null} in case of SQL NULL
160159
* @throws SQLException if thrown by JDBC methods
161160
* @see java.sql.ResultSet#getSQLXML
162161
* @see java.sql.SQLXML#getSource

0 commit comments

Comments
 (0)