svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.db / org.gvsig.fmap.dal.db.jdbc / src / main / java / org / gvsig / fmap / dal / store / jdbc2 / JDBCUtils.java @ 46064
History | View | Annotate | Download (9.2 KB)
1 | 45065 | jjdelcerro | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | *
|
||
4 | * Copyright (C) 2007-2020 gvSIG Association.
|
||
5 | *
|
||
6 | * This program is free software; you can redistribute it and/or
|
||
7 | * modify it under the terms of the GNU General Public License
|
||
8 | * as published by the Free Software Foundation; either version 3
|
||
9 | * of the License, or (at your option) any later version.
|
||
10 | *
|
||
11 | * This program is distributed in the hope that it will be useful,
|
||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
14 | * GNU General Public License for more details.
|
||
15 | *
|
||
16 | * You should have received a copy of the GNU General Public License
|
||
17 | * along with this program; if not, write to the Free Software
|
||
18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||
19 | * MA 02110-1301, USA.
|
||
20 | *
|
||
21 | * For any additional information, do not hesitate to contact us
|
||
22 | * at info AT gvsig.com, or visit our website www.gvsig.com.
|
||
23 | */
|
||
24 | 43020 | jjdelcerro | package org.gvsig.fmap.dal.store.jdbc2; |
25 | |||
26 | import java.sql.Connection; |
||
27 | import java.sql.PreparedStatement; |
||
28 | import java.sql.ResultSet; |
||
29 | import java.sql.SQLException; |
||
30 | import java.sql.Statement; |
||
31 | 43775 | jjdelcerro | import java.util.Iterator; |
32 | import java.util.List; |
||
33 | 44609 | jjdelcerro | import java.util.Objects; |
34 | 43020 | jjdelcerro | import org.slf4j.Logger; |
35 | import org.slf4j.LoggerFactory; |
||
36 | |||
37 | 44191 | jjdelcerro | @SuppressWarnings("UseSpecificCatch") |
38 | 43020 | jjdelcerro | public class JDBCUtils { |
39 | |||
40 | 44191 | jjdelcerro | private static final Logger LOGGER = LoggerFactory.getLogger(JDBCUtils.class); |
41 | 43020 | jjdelcerro | |
42 | private JDBCUtils() {
|
||
43 | |||
44 | } |
||
45 | |||
46 | 44609 | jjdelcerro | private static String getSQLInfo(PreparedStatement st, String sql) { |
47 | try {
|
||
48 | return sql+" [[ "+Objects.toString(st)+" ]]"; |
||
49 | } catch(Throwable th) { |
||
50 | return sql;
|
||
51 | } |
||
52 | } |
||
53 | 45097 | jjdelcerro | |
54 | public static String getConnId(Statement st) { |
||
55 | if( st==null ) { |
||
56 | return "null"; |
||
57 | } |
||
58 | try {
|
||
59 | return getHexId(st.getConnection());
|
||
60 | } catch (SQLException ex) { |
||
61 | return "null"; |
||
62 | } |
||
63 | } |
||
64 | |||
65 | public static String getConnId(ResultSet resulSet) { |
||
66 | if( resulSet==null ) { |
||
67 | return "null"; |
||
68 | } |
||
69 | try {
|
||
70 | return getConnId(resulSet.getStatement());
|
||
71 | } catch (SQLException ex) { |
||
72 | return "null"; |
||
73 | } |
||
74 | } |
||
75 | |||
76 | public static String getConnId(Connection conn) { |
||
77 | return getHexId(conn);
|
||
78 | } |
||
79 | 44609 | jjdelcerro | |
80 | 45097 | jjdelcerro | public static String getHexId(Object obj) { |
81 | if( obj==null ) { |
||
82 | return "null"; |
||
83 | } |
||
84 | return Integer.toHexString(obj.hashCode()).toUpperCase(); |
||
85 | } |
||
86 | |||
87 | 43020 | jjdelcerro | public static ResultSet executeQuery(Statement st, String sql) throws SQLException { |
88 | 45097 | jjdelcerro | if( LOGGER.isDebugEnabled() ) {
|
89 | LOGGER.debug("["+getConnId(st)+"] executeQuery(st) SQL= " + sql); |
||
90 | } |
||
91 | 43756 | jjdelcerro | try {
|
92 | 45097 | jjdelcerro | @SuppressWarnings("null") |
93 | 43756 | jjdelcerro | ResultSet rs = st.executeQuery(sql);
|
94 | return rs;
|
||
95 | } catch(Exception ex) { |
||
96 | 45425 | jjdelcerro | LOGGER.debug("execute SQL: " + sql, ex);
|
97 | 43756 | jjdelcerro | throw ex;
|
98 | } |
||
99 | 43020 | jjdelcerro | } |
100 | |||
101 | 45097 | jjdelcerro | @SuppressWarnings("null") |
102 | 43020 | jjdelcerro | public static void execute(Statement st, String sql) throws SQLException { |
103 | 45097 | jjdelcerro | if( LOGGER.isDebugEnabled() ) {
|
104 | LOGGER.debug("["+getConnId(st)+"] execute(st) SQL: " + sql); |
||
105 | } |
||
106 | 43756 | jjdelcerro | try {
|
107 | st.execute(sql); |
||
108 | } catch(Exception ex) { |
||
109 | 45425 | jjdelcerro | LOGGER.debug("execute SQL: " + sql, ex);
|
110 | 43756 | jjdelcerro | throw ex;
|
111 | } |
||
112 | 43020 | jjdelcerro | } |
113 | |||
114 | 43650 | jjdelcerro | public static void execute(Connection connection, String sql) throws SQLException { |
115 | 45097 | jjdelcerro | if( LOGGER.isDebugEnabled() ) {
|
116 | LOGGER.debug("["+getConnId(connection)+"] execute(conn) SQL: " + sql); |
||
117 | } |
||
118 | 43756 | jjdelcerro | try {
|
119 | 45097 | jjdelcerro | @SuppressWarnings("null") |
120 | Statement st = connection.createStatement();
|
||
121 | 43756 | jjdelcerro | st.execute(sql); |
122 | } catch(Exception ex) { |
||
123 | 45425 | jjdelcerro | LOGGER.debug("execute SQL: " + sql, ex);
|
124 | 43756 | jjdelcerro | throw ex;
|
125 | } |
||
126 | 43650 | jjdelcerro | } |
127 | |||
128 | 43775 | jjdelcerro | public static void execute(Connection connection, List<String> sqls) throws SQLException { |
129 | Statement st = null; |
||
130 | String sql = null; |
||
131 | try {
|
||
132 | st = connection.createStatement(); |
||
133 | Iterator<String> it = sqls.iterator(); |
||
134 | while( it.hasNext() ) {
|
||
135 | sql = it.next(); |
||
136 | 45097 | jjdelcerro | if( LOGGER.isDebugEnabled() ) {
|
137 | LOGGER.debug("["+getConnId(connection)+"] execute(conn) SQLs: " + sql); |
||
138 | } |
||
139 | 43775 | jjdelcerro | st.execute(sql); |
140 | } |
||
141 | } catch (SQLException ex) { |
||
142 | 45425 | jjdelcerro | LOGGER.debug("execute SQL: " + sql, ex);
|
143 | 43775 | jjdelcerro | throw ex;
|
144 | } finally {
|
||
145 | JDBCUtils.closeQuietly(st); |
||
146 | } |
||
147 | } |
||
148 | |||
149 | 43020 | jjdelcerro | public static ResultSet executeQuery(PreparedStatement st, String sql) throws SQLException { |
150 | 44609 | jjdelcerro | if( LOGGER.isDebugEnabled() ) {
|
151 | 45097 | jjdelcerro | LOGGER.debug("["+getConnId(st)+"] executeQuery(pst) SQL= "+ getSQLInfo(st,sql)); |
152 | 44609 | jjdelcerro | } |
153 | 43756 | jjdelcerro | try {
|
154 | 45097 | jjdelcerro | @SuppressWarnings("null") |
155 | 43756 | jjdelcerro | ResultSet rs = st.executeQuery();
|
156 | return rs;
|
||
157 | } catch(Exception ex) { |
||
158 | 45425 | jjdelcerro | LOGGER.debug("execute query SQL: " + getSQLInfo(st,sql), ex);
|
159 | 43756 | jjdelcerro | throw ex;
|
160 | } |
||
161 | 43020 | jjdelcerro | } |
162 | |||
163 | 45097 | jjdelcerro | @SuppressWarnings("null") |
164 | 43020 | jjdelcerro | public static void execute(PreparedStatement st, String sql) throws SQLException { |
165 | 44609 | jjdelcerro | if( LOGGER.isDebugEnabled() ) {
|
166 | 45097 | jjdelcerro | LOGGER.debug("["+getConnId(st)+"] execute(pst) SQL= "+ getSQLInfo(st,sql)); |
167 | 44609 | jjdelcerro | } |
168 | 43756 | jjdelcerro | try {
|
169 | st.execute(); |
||
170 | } catch(Exception ex) { |
||
171 | 45425 | jjdelcerro | LOGGER.debug("execute SQL: " + getSQLInfo(st,sql), ex);
|
172 | 43756 | jjdelcerro | throw ex;
|
173 | } |
||
174 | 43020 | jjdelcerro | } |
175 | |||
176 | public static int executeUpdate(PreparedStatement st, String sql) throws SQLException { |
||
177 | 44609 | jjdelcerro | if( LOGGER.isDebugEnabled() ) {
|
178 | 45097 | jjdelcerro | LOGGER.debug("["+getConnId(st)+"] executeUpdate(pst) SQL= "+ getSQLInfo(st,sql)); |
179 | 44609 | jjdelcerro | } |
180 | 43756 | jjdelcerro | try {
|
181 | return st.executeUpdate();
|
||
182 | } catch(Exception ex) { |
||
183 | 45425 | jjdelcerro | LOGGER.debug("execute update SQL: " + getSQLInfo(st,sql), ex);
|
184 | 43756 | jjdelcerro | throw ex;
|
185 | } |
||
186 | 43020 | jjdelcerro | } |
187 | |||
188 | 45152 | fdiaz | public static int executeUpdate(Statement st, String sql) throws SQLException { |
189 | if( LOGGER.isDebugEnabled() ) {
|
||
190 | LOGGER.debug("["+getConnId(st)+"] executeUpdate(st) SQL= "+ sql); |
||
191 | } |
||
192 | try {
|
||
193 | return st.executeUpdate(sql);
|
||
194 | } catch(Exception ex) { |
||
195 | 45425 | jjdelcerro | LOGGER.debug("execute update SQL: " + sql, ex);
|
196 | 45152 | fdiaz | throw ex;
|
197 | } |
||
198 | } |
||
199 | |||
200 | 45506 | jjdelcerro | public static int[] executeBatch(PreparedStatement st, String sql) throws SQLException { |
201 | if( LOGGER.isDebugEnabled() ) {
|
||
202 | LOGGER.debug("["+getConnId(st)+"] executeBatch(pst) SQL= "+ getSQLInfo(st,sql)); |
||
203 | } |
||
204 | try {
|
||
205 | return st.executeBatch();
|
||
206 | } catch(Exception ex) { |
||
207 | LOGGER.debug("execute batch SQL: " + getSQLInfo(st,sql), ex);
|
||
208 | throw ex;
|
||
209 | } |
||
210 | } |
||
211 | |||
212 | public static void addBatch(PreparedStatement st, String sql) throws SQLException { |
||
213 | if( LOGGER.isDebugEnabled() ) {
|
||
214 | LOGGER.debug("["+getConnId(st)+"] addBatch(pst) SQL= "+ getSQLInfo(st,sql)); |
||
215 | } |
||
216 | try {
|
||
217 | st.addBatch(); |
||
218 | } catch(Exception ex) { |
||
219 | LOGGER.debug("add batch SQL: " + getSQLInfo(st,sql), ex);
|
||
220 | throw ex;
|
||
221 | } |
||
222 | } |
||
223 | |||
224 | 45097 | jjdelcerro | public static void closeQuietly(Statement st) { |
225 | if( LOGGER.isDebugEnabled() ) {
|
||
226 | LOGGER.debug("["+getConnId(st)+"] Close statement"); |
||
227 | } |
||
228 | if (st == null) { |
||
229 | 43020 | jjdelcerro | return;
|
230 | } |
||
231 | try {
|
||
232 | 45097 | jjdelcerro | st.close(); |
233 | 43020 | jjdelcerro | } catch (Exception e) { |
234 | 45425 | jjdelcerro | LOGGER.debug("Problems closing " + st.getClass().getSimpleName() + " '" + st.toString() + "'.", e); |
235 | 43020 | jjdelcerro | } |
236 | } |
||
237 | |||
238 | 45097 | jjdelcerro | public static void closeQuietly(ResultSet resulSet) { |
239 | if( LOGGER.isDebugEnabled() ) {
|
||
240 | LOGGER.debug("["+getConnId(resulSet)+"] Close ResultSet"); |
||
241 | } |
||
242 | if (resulSet == null) { |
||
243 | 43020 | jjdelcerro | return;
|
244 | } |
||
245 | try {
|
||
246 | 45097 | jjdelcerro | resulSet.close(); |
247 | 43020 | jjdelcerro | } catch (Exception e) { |
248 | 45425 | jjdelcerro | LOGGER.debug("Problems closing " + resulSet.getClass().getSimpleName() + " '" + resulSet.toString() + "'.", e); |
249 | 43020 | jjdelcerro | } |
250 | } |
||
251 | |||
252 | 44191 | jjdelcerro | public static void closeQuietly(Connection conn) { |
253 | 45097 | jjdelcerro | if( LOGGER.isDebugEnabled() ) {
|
254 | LOGGER.debug("["+getConnId(conn)+"] Close conn ("+Objects.toString(conn)+")"); |
||
255 | } |
||
256 | 44191 | jjdelcerro | if (conn == null) { |
257 | 43020 | jjdelcerro | return;
|
258 | } |
||
259 | try {
|
||
260 | 44191 | jjdelcerro | conn.close(); |
261 | 43020 | jjdelcerro | } catch (Exception e) { |
262 | 45425 | jjdelcerro | LOGGER.debug("Problems closing " + conn.getClass().getSimpleName() + " '" + conn.toString() + "'.", e); |
263 | 43020 | jjdelcerro | } |
264 | } |
||
265 | |||
266 | 45097 | jjdelcerro | public static void close(Connection conn) { |
267 | if( LOGGER.isDebugEnabled() ) {
|
||
268 | LOGGER.debug("["+getConnId(conn)+"] Close conn ("+Objects.toString(conn)+")"); |
||
269 | } |
||
270 | if (conn == null) { |
||
271 | return;
|
||
272 | } |
||
273 | try {
|
||
274 | conn.close(); |
||
275 | } catch (Exception e) { |
||
276 | throw new RuntimeException(e); |
||
277 | } |
||
278 | } |
||
279 | |||
280 | 43020 | jjdelcerro | public static void closeQuietly(AutoCloseable obj) { |
281 | if (obj == null) { |
||
282 | return;
|
||
283 | } |
||
284 | try {
|
||
285 | 45097 | jjdelcerro | if( LOGGER.isDebugEnabled() ) {
|
286 | LOGGER.debug("Closing " + obj.getClass().getSimpleName() + " "+ obj.hashCode() + " ("+obj.toString()+")"); |
||
287 | } |
||
288 | 43020 | jjdelcerro | obj.close(); |
289 | } catch (Exception e) { |
||
290 | 45425 | jjdelcerro | LOGGER.debug("Problems closing " + obj.getClass().getSimpleName() + " '" + obj.toString() + "'.", e); |
291 | 43020 | jjdelcerro | } |
292 | } |
||
293 | } |