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

History | View | Annotate | Download (7.98 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 void closeQuietly(Statement st) {
189
        if( LOGGER.isDebugEnabled() ) {
190
            LOGGER.debug("["+getConnId(st)+"] Close statement");
191
        }
192
        if (st == null) {
193
            return;
194
        }
195
        try {
196
            st.close();
197
        } catch (Exception e) {
198
            LOGGER.warn("Problems closing " + st.getClass().getSimpleName() + " '" + st.toString() + "'.", e);
199
        }
200
    }
201

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

    
216
    public static void closeQuietly(Connection conn) {
217
        if( LOGGER.isDebugEnabled() ) {
218
            LOGGER.debug("["+getConnId(conn)+"] Close conn ("+Objects.toString(conn)+")");
219
        }
220
        if (conn == null) {
221
            return;
222
        }
223
        try {
224
            conn.close();
225
        } catch (Exception e) {
226
            LOGGER.warn("Problems closing " + conn.getClass().getSimpleName() + " '" + conn.toString() + "'.", e);
227
        }
228
    }
229
    
230
    public static void close(Connection conn) {
231
        if( LOGGER.isDebugEnabled() ) {
232
            LOGGER.debug("["+getConnId(conn)+"] Close conn ("+Objects.toString(conn)+")");
233
        }
234
        if (conn == null) {
235
            return;
236
        }
237
        try {
238
            conn.close();
239
        } catch (Exception e) {
240
            throw new RuntimeException(e);
241
        }
242
    }
243
    
244
    public static void closeQuietly(AutoCloseable obj) {
245
        if (obj == null) {
246
            return;
247
        }
248
        try {
249
            if( LOGGER.isDebugEnabled() ) {
250
                LOGGER.debug("Closing " + obj.getClass().getSimpleName() + " "+ obj.hashCode() + " ("+obj.toString()+")");
251
            }
252
            obj.close();
253
        } catch (Exception e) {
254
            LOGGER.warn("Problems closing " + obj.getClass().getSimpleName() + " '" + obj.toString() + "'.", e);
255
        }
256
    }
257
}