Statistics
| Revision:

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 @ 45152

History | View | Annotate | Download (8.36 KB)

1
/**
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
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
import java.util.Iterator;
32
import java.util.List;
33
import java.util.Objects;
34
import org.slf4j.Logger;
35
import org.slf4j.LoggerFactory;
36

    
37
@SuppressWarnings("UseSpecificCatch")
38
public class JDBCUtils {
39

    
40
    private static final Logger LOGGER = LoggerFactory.getLogger(JDBCUtils.class);
41
    
42
    private JDBCUtils() {
43

    
44
    }
45

    
46
    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

    
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
    
80
    public static String getHexId(Object obj) {
81
        if( obj==null ) {
82
            return "null";
83
        }
84
        return Integer.toHexString(obj.hashCode()).toUpperCase();
85
    }
86
    
87
    public static ResultSet executeQuery(Statement st, String sql) throws SQLException {
88
        if( LOGGER.isDebugEnabled() ) {
89
            LOGGER.debug("["+getConnId(st)+"] executeQuery(st) SQL= " + sql);
90
        }
91
        try {
92
            @SuppressWarnings("null")
93
            ResultSet rs = st.executeQuery(sql);
94
            return rs;
95
        } catch(Exception ex) {
96
            LOGGER.warn("execute SQL: " + sql, ex);
97
            throw ex;
98
        }
99
    }
100

    
101
    @SuppressWarnings("null")
102
    public static void execute(Statement st, String sql) throws SQLException {
103
        if( LOGGER.isDebugEnabled() ) {
104
            LOGGER.debug("["+getConnId(st)+"] execute(st) SQL: " + sql);
105
        }
106
        try {
107
            st.execute(sql);
108
        } catch(Exception ex) {
109
            LOGGER.warn("execute SQL: " + sql, ex);
110
            throw ex;
111
        }
112
    }
113

    
114
    public static void execute(Connection connection, String sql) throws SQLException {
115
        if( LOGGER.isDebugEnabled() ) {
116
            LOGGER.debug("["+getConnId(connection)+"] execute(conn) SQL: " + sql);
117
        }
118
        try {
119
            @SuppressWarnings("null")
120
            Statement st = connection.createStatement();
121
            st.execute(sql);
122
        } catch(Exception ex) {
123
            LOGGER.warn("execute SQL: " + sql, ex);
124
            throw ex;
125
        }
126
    }
127

    
128
    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
                if( LOGGER.isDebugEnabled() ) {
137
                    LOGGER.debug("["+getConnId(connection)+"] execute(conn) SQLs: " + sql);
138
                }
139
                st.execute(sql);
140
            }
141
        } catch (SQLException ex) {
142
            LOGGER.warn("execute SQL: " + sql, ex);
143
            throw ex;
144
        } finally {
145
            JDBCUtils.closeQuietly(st);
146
        }       
147
    }
148

    
149
    public static ResultSet executeQuery(PreparedStatement st, String sql) throws SQLException {
150
        if( LOGGER.isDebugEnabled() ) {
151
            LOGGER.debug("["+getConnId(st)+"] executeQuery(pst) SQL= "+ getSQLInfo(st,sql));
152
        }
153
        try {
154
            @SuppressWarnings("null")
155
            ResultSet rs = st.executeQuery();
156
            return rs;
157
        } catch(Exception ex) {
158
            LOGGER.warn("execute query SQL: " + getSQLInfo(st,sql), ex);
159
            throw ex;
160
        }
161
    }
162

    
163
    @SuppressWarnings("null")
164
    public static void execute(PreparedStatement st, String sql) throws SQLException {
165
        if( LOGGER.isDebugEnabled() ) {
166
            LOGGER.debug("["+getConnId(st)+"] execute(pst) SQL= "+ getSQLInfo(st,sql));
167
        }
168
        try {
169
            st.execute();
170
        } catch(Exception ex) {
171
            LOGGER.warn("execute SQL: " + getSQLInfo(st,sql), ex);
172
            throw ex;
173
        }
174
    }
175

    
176
    public static int executeUpdate(PreparedStatement st, String sql) throws SQLException {
177
        if( LOGGER.isDebugEnabled() ) {
178
            LOGGER.debug("["+getConnId(st)+"] executeUpdate(pst) SQL= "+ getSQLInfo(st,sql));
179
        }
180
        try {
181
            return st.executeUpdate();
182
        } catch(Exception ex) {
183
            LOGGER.warn("execute update SQL: " + getSQLInfo(st,sql), ex);
184
            throw ex;
185
        }
186
    }
187

    
188
    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
            LOGGER.warn("execute update SQL: " + sql, ex);
196
            throw ex;
197
        }
198
    }
199

    
200
    public static void closeQuietly(Statement st) {
201
        if( LOGGER.isDebugEnabled() ) {
202
            LOGGER.debug("["+getConnId(st)+"] Close statement");
203
        }
204
        if (st == null) {
205
            return;
206
        }
207
        try {
208
            st.close();
209
        } catch (Exception e) {
210
            LOGGER.warn("Problems closing " + st.getClass().getSimpleName() + " '" + st.toString() + "'.", e);
211
        }
212
    }
213

    
214
    public static void closeQuietly(ResultSet resulSet) {
215
        if( LOGGER.isDebugEnabled() ) {
216
            LOGGER.debug("["+getConnId(resulSet)+"] Close ResultSet");
217
        }
218
        if (resulSet == null) {
219
            return;
220
        }
221
        try {
222
            resulSet.close();
223
        } catch (Exception e) {
224
            LOGGER.warn("Problems closing " + resulSet.getClass().getSimpleName() + " '" + resulSet.toString() + "'.", e);
225
        }
226
    }
227

    
228
    public static void closeQuietly(Connection conn) {
229
        if( LOGGER.isDebugEnabled() ) {
230
            LOGGER.debug("["+getConnId(conn)+"] Close conn ("+Objects.toString(conn)+")");
231
        }
232
        if (conn == null) {
233
            return;
234
        }
235
        try {
236
            conn.close();
237
        } catch (Exception e) {
238
            LOGGER.warn("Problems closing " + conn.getClass().getSimpleName() + " '" + conn.toString() + "'.", e);
239
        }
240
    }
241
    
242
    public static void close(Connection conn) {
243
        if( LOGGER.isDebugEnabled() ) {
244
            LOGGER.debug("["+getConnId(conn)+"] Close conn ("+Objects.toString(conn)+")");
245
        }
246
        if (conn == null) {
247
            return;
248
        }
249
        try {
250
            conn.close();
251
        } catch (Exception e) {
252
            throw new RuntimeException(e);
253
        }
254
    }
255
    
256
    public static void closeQuietly(AutoCloseable obj) {
257
        if (obj == null) {
258
            return;
259
        }
260
        try {
261
            if( LOGGER.isDebugEnabled() ) {
262
                LOGGER.debug("Closing " + obj.getClass().getSimpleName() + " "+ obj.hashCode() + " ("+obj.toString()+")");
263
            }
264
            obj.close();
265
        } catch (Exception e) {
266
            LOGGER.warn("Problems closing " + obj.getClass().getSimpleName() + " '" + obj.toString() + "'.", e);
267
        }
268
    }
269
}