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

History | View | Annotate | Download (4.93 KB)

1 43020 jjdelcerro
package org.gvsig.fmap.dal.store.jdbc2;
2
3
import java.sql.Connection;
4
import java.sql.PreparedStatement;
5
import java.sql.ResultSet;
6
import java.sql.SQLException;
7
import java.sql.Statement;
8 43775 jjdelcerro
import java.util.Iterator;
9
import java.util.List;
10 44609 jjdelcerro
import java.util.Objects;
11 43020 jjdelcerro
import org.slf4j.Logger;
12
import org.slf4j.LoggerFactory;
13
14 44191 jjdelcerro
@SuppressWarnings("UseSpecificCatch")
15 43020 jjdelcerro
public class JDBCUtils {
16
17 44191 jjdelcerro
    private static final Logger LOGGER = LoggerFactory.getLogger(JDBCUtils.class);
18 43020 jjdelcerro
19
    private JDBCUtils() {
20
21
    }
22
23 44609 jjdelcerro
    private static String getSQLInfo(PreparedStatement st, String sql) {
24
        try {
25
            return sql+" [[ "+Objects.toString(st)+" ]]";
26
        } catch(Throwable th) {
27
            return sql;
28
        }
29
    }
30
31 43020 jjdelcerro
    public static ResultSet executeQuery(Statement st, String sql) throws SQLException {
32 44191 jjdelcerro
        LOGGER.debug("execute query SQL: " + sql);
33 43756 jjdelcerro
        try {
34
            ResultSet rs = st.executeQuery(sql);
35
            return rs;
36
        } catch(Exception ex) {
37 44191 jjdelcerro
            LOGGER.warn("execute SQL: " + sql, ex);
38 43756 jjdelcerro
            throw ex;
39
        }
40 43020 jjdelcerro
    }
41
42
    public static void execute(Statement st, String sql) throws SQLException {
43 44191 jjdelcerro
        LOGGER.debug("execute SQL: " + sql);
44 43756 jjdelcerro
        try {
45
            st.execute(sql);
46
        } catch(Exception ex) {
47 44191 jjdelcerro
            LOGGER.warn("execute SQL: " + sql, ex);
48 43756 jjdelcerro
            throw ex;
49
        }
50 43020 jjdelcerro
    }
51
52 43650 jjdelcerro
    public static void execute(Connection connection, String sql) throws SQLException {
53 44191 jjdelcerro
        LOGGER.debug("execute SQL: " + sql);
54 43650 jjdelcerro
        Statement st = connection.createStatement();
55 43756 jjdelcerro
        try {
56
            st.execute(sql);
57
        } catch(Exception ex) {
58 44191 jjdelcerro
            LOGGER.warn("execute SQL: " + sql, ex);
59 43756 jjdelcerro
            throw ex;
60
        }
61 43650 jjdelcerro
    }
62
63 43775 jjdelcerro
    public static void execute(Connection connection, List<String> sqls) throws SQLException {
64
        Statement st = null;
65
        String sql = null;
66
        try {
67
            st = connection.createStatement();
68
            Iterator<String> it = sqls.iterator();
69
            while( it.hasNext() ) {
70
                sql = it.next();
71 44191 jjdelcerro
                LOGGER.debug("execute SQL: " + sql);
72 43775 jjdelcerro
                st.execute(sql);
73
            }
74
        } catch (SQLException ex) {
75 44191 jjdelcerro
            LOGGER.warn("execute SQL: " + sql, ex);
76 43775 jjdelcerro
            throw ex;
77
        } finally {
78
            JDBCUtils.closeQuietly(st);
79
        }
80
    }
81
82 43020 jjdelcerro
    public static ResultSet executeQuery(PreparedStatement st, String sql) throws SQLException {
83 44609 jjdelcerro
        if( LOGGER.isDebugEnabled() ) {
84
            LOGGER.debug("execute query SQL= "+ getSQLInfo(st,sql));
85
        }
86 43756 jjdelcerro
        try {
87
            ResultSet rs = st.executeQuery();
88
            return rs;
89
        } catch(Exception ex) {
90 44609 jjdelcerro
            LOGGER.warn("execute query SQL: " + getSQLInfo(st,sql), ex);
91 43756 jjdelcerro
            throw ex;
92
        }
93 43020 jjdelcerro
    }
94
95
    public static void execute(PreparedStatement st, String sql) throws SQLException {
96 44609 jjdelcerro
        if( LOGGER.isDebugEnabled() ) {
97
            LOGGER.debug("execute SQL= "+ getSQLInfo(st,sql));
98
        }
99 43756 jjdelcerro
        try {
100
            st.execute();
101
        } catch(Exception ex) {
102 44609 jjdelcerro
            LOGGER.warn("execute SQL: " + getSQLInfo(st,sql), ex);
103 43756 jjdelcerro
            throw ex;
104
        }
105 43020 jjdelcerro
    }
106
107
    public static int executeUpdate(PreparedStatement st, String sql) throws SQLException {
108 44609 jjdelcerro
        if( LOGGER.isDebugEnabled() ) {
109
            LOGGER.debug("execute update SQL= "+ getSQLInfo(st,sql));
110
        }
111 43756 jjdelcerro
        try {
112
            return st.executeUpdate();
113
        } catch(Exception ex) {
114 44609 jjdelcerro
            LOGGER.warn("execute update SQL: " + getSQLInfo(st,sql), ex);
115 43756 jjdelcerro
            throw ex;
116
        }
117 43020 jjdelcerro
    }
118
119
    public static void closeQuietly(Statement obj) {
120
        if (obj == null) {
121
            return;
122
        }
123
        try {
124
            obj.close();
125
        } catch (Exception e) {
126 44191 jjdelcerro
            LOGGER.warn("Problems closing " + obj.getClass().getSimpleName() + " '" + obj.toString() + "'.", e);
127 43020 jjdelcerro
        }
128
    }
129
130
    public static void closeQuietly(ResultSet obj) {
131
        if (obj == null) {
132
            return;
133
        }
134
        try {
135
            obj.close();
136
        } catch (Exception e) {
137 44191 jjdelcerro
            LOGGER.warn("Problems closing " + obj.getClass().getSimpleName() + " '" + obj.toString() + "'.", e);
138 43020 jjdelcerro
        }
139
    }
140
141 44191 jjdelcerro
    public static void closeQuietly(Connection conn) {
142
        if (conn == null) {
143 43020 jjdelcerro
            return;
144
        }
145 44191 jjdelcerro
        LOGGER.debug("Closing connection "+ conn.hashCode() + " ("+conn.toString()+")");
146 43020 jjdelcerro
        try {
147 44191 jjdelcerro
            conn.close();
148 43020 jjdelcerro
        } catch (Exception e) {
149 44191 jjdelcerro
            LOGGER.warn("Problems closing " + conn.getClass().getSimpleName() + " '" + conn.toString() + "'.", e);
150 43020 jjdelcerro
        }
151
    }
152
153
    public static void closeQuietly(AutoCloseable obj) {
154
        if (obj == null) {
155
            return;
156
        }
157
        try {
158 44191 jjdelcerro
            LOGGER.debug("Closing " + obj.getClass().getSimpleName() + " "+ obj.hashCode() + " ("+obj.toString()+")");
159 43020 jjdelcerro
            obj.close();
160
        } catch (Exception e) {
161 44191 jjdelcerro
            LOGGER.warn("Problems closing " + obj.getClass().getSimpleName() + " '" + obj.toString() + "'.", e);
162 43020 jjdelcerro
        }
163
    }
164
}