1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.jdbc.core;
18
19 import java.sql.ResultSet;
20 import java.sql.SQLException;
21 import javax.sql.rowset.CachedRowSet;
22 import javax.sql.rowset.RowSetFactory;
23 import javax.sql.rowset.RowSetProvider;
24
25 import org.springframework.core.JdkVersion;
26 import org.springframework.jdbc.support.rowset.ResultSetWrappingSqlRowSet;
27 import org.springframework.jdbc.support.rowset.SqlRowSet;
28 import org.springframework.lang.UsesJava7;
29 import org.springframework.util.ClassUtils;
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 public class SqlRowSetResultSetExtractor implements ResultSetExtractor<SqlRowSet> {
48
49 private static final CachedRowSetFactory cachedRowSetFactory;
50
51 static {
52 if (JdkVersion.getMajorJavaVersion() >= JdkVersion.JAVA_17) {
53
54 cachedRowSetFactory = new StandardCachedRowSetFactory();
55 }
56 else {
57
58 cachedRowSetFactory = new SunCachedRowSetFactory();
59 }
60 }
61
62
63 @Override
64 public SqlRowSet extractData(ResultSet rs) throws SQLException {
65 return createSqlRowSet(rs);
66 }
67
68
69
70
71
72
73
74
75
76
77
78
79
80 protected SqlRowSet createSqlRowSet(ResultSet rs) throws SQLException {
81 CachedRowSet rowSet = newCachedRowSet();
82 rowSet.populate(rs);
83 return new ResultSetWrappingSqlRowSet(rowSet);
84 }
85
86
87
88
89
90
91
92
93
94
95
96 protected CachedRowSet newCachedRowSet() throws SQLException {
97 return cachedRowSetFactory.createCachedRowSet();
98 }
99
100
101
102
103
104 private interface CachedRowSetFactory {
105
106 CachedRowSet createCachedRowSet() throws SQLException;
107 }
108
109
110
111
112
113 @UsesJava7
114 private static class StandardCachedRowSetFactory implements CachedRowSetFactory {
115
116 private final RowSetFactory rowSetFactory;
117
118 public StandardCachedRowSetFactory() {
119 try {
120 this.rowSetFactory = RowSetProvider.newFactory();
121 }
122 catch (SQLException ex) {
123 throw new IllegalStateException("Cannot create RowSetFactory through RowSetProvider", ex);
124 }
125 }
126
127 @Override
128 public CachedRowSet createCachedRowSet() throws SQLException {
129 return this.rowSetFactory.createCachedRowSet();
130 }
131 }
132
133
134
135
136
137 private static class SunCachedRowSetFactory implements CachedRowSetFactory {
138
139 private static final Class<?> implementationClass;
140
141 static {
142 try {
143 implementationClass = ClassUtils.forName("com.sun.rowset.CachedRowSetImpl",
144 SqlRowSetResultSetExtractor.class.getClassLoader());
145 }
146 catch (Throwable ex) {
147 throw new IllegalStateException(ex);
148 }
149 }
150
151 @Override
152 public CachedRowSet createCachedRowSet() throws SQLException {
153 try {
154 return (CachedRowSet) implementationClass.newInstance();
155 }
156 catch (Throwable ex) {
157 throw new IllegalStateException(ex);
158 }
159 }
160 }
161
162 }