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

History | View | Annotate | Download (4.43 KB)

1
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
import java.util.Iterator;
9
import java.util.List;
10
import org.slf4j.Logger;
11
import org.slf4j.LoggerFactory;
12

    
13
@SuppressWarnings("UseSpecificCatch")
14
public class JDBCUtils {
15

    
16
    private static final Logger LOGGER = LoggerFactory.getLogger(JDBCUtils.class);
17
    
18
    private JDBCUtils() {
19

    
20
    }
21

    
22
    public static ResultSet executeQuery(Statement st, String sql) throws SQLException {
23
        LOGGER.debug("execute query SQL: " + sql);
24
        try {
25
            ResultSet rs = st.executeQuery(sql);
26
            return rs;
27
        } catch(Exception ex) {
28
            LOGGER.warn("execute SQL: " + sql, ex);
29
            throw ex;
30
        }
31
    }
32

    
33
    public static void execute(Statement st, String sql) throws SQLException {
34
        LOGGER.debug("execute SQL: " + sql);
35
        try {
36
            st.execute(sql);
37
        } catch(Exception ex) {
38
            LOGGER.warn("execute SQL: " + sql, ex);
39
            throw ex;
40
        }
41
    }
42

    
43
    public static void execute(Connection connection, String sql) throws SQLException {
44
        LOGGER.debug("execute SQL: " + sql);
45
        Statement st = connection.createStatement();
46
        try {
47
            st.execute(sql);
48
        } catch(Exception ex) {
49
            LOGGER.warn("execute SQL: " + sql, ex);
50
            throw ex;
51
        }
52
    }
53

    
54
    public static void execute(Connection connection, List<String> sqls) throws SQLException {
55
        Statement st = null;
56
        String sql = null;
57
        try {
58
            st = connection.createStatement();
59
            Iterator<String> it = sqls.iterator();
60
            while( it.hasNext() ) {
61
                sql = it.next();
62
                LOGGER.debug("execute SQL: " + sql);
63
                st.execute(sql);
64
            }
65
        } catch (SQLException ex) {
66
            LOGGER.warn("execute SQL: " + sql, ex);
67
            throw ex;
68
        } finally {
69
            JDBCUtils.closeQuietly(st);
70
        }       
71
    }
72

    
73
    public static ResultSet executeQuery(PreparedStatement st, String sql) throws SQLException {
74
        LOGGER.debug("execute query SQL: " + sql);
75
        try {
76
            ResultSet rs = st.executeQuery();
77
            return rs;
78
        } catch(Exception ex) {
79
            LOGGER.warn("execute SQL: " + sql, ex);
80
            throw ex;
81
        }
82
    }
83

    
84
    public static void execute(PreparedStatement st, String sql) throws SQLException {
85
        LOGGER.debug("execute SQL: " + sql);
86
        try {
87
            st.execute();
88
        } catch(Exception ex) {
89
            LOGGER.warn("execute SQL: " + sql, ex);
90
            throw ex;
91
        }
92
    }
93

    
94
    public static int executeUpdate(PreparedStatement st, String sql) throws SQLException {
95
        LOGGER.debug("execute update SQL: "+ sql);
96
        try {
97
            return st.executeUpdate();
98
        } catch(Exception ex) {
99
            LOGGER.warn("execute SQL: " + sql, ex);
100
            throw ex;
101
        }
102
    }
103

    
104
    public static void closeQuietly(Statement obj) {
105
        if (obj == null) {
106
            return;
107
        }
108
        try {
109
            obj.close();
110
        } catch (Exception e) {
111
            LOGGER.warn("Problems closing " + obj.getClass().getSimpleName() + " '" + obj.toString() + "'.", e);
112
        }
113
    }
114

    
115
    public static void closeQuietly(ResultSet obj) {
116
        if (obj == null) {
117
            return;
118
        }
119
        try {
120
            obj.close();
121
        } catch (Exception e) {
122
            LOGGER.warn("Problems closing " + obj.getClass().getSimpleName() + " '" + obj.toString() + "'.", e);
123
        }
124
    }
125

    
126
    public static void closeQuietly(Connection conn) {
127
        if (conn == null) {
128
            return;
129
        }
130
        LOGGER.debug("Closing connection "+ conn.hashCode() + " ("+conn.toString()+")");
131
        try {
132
            conn.close();
133
        } catch (Exception e) {
134
            LOGGER.warn("Problems closing " + conn.getClass().getSimpleName() + " '" + conn.toString() + "'.", e);
135
        }
136
    }
137
    
138
    public static void closeQuietly(AutoCloseable obj) {
139
        if (obj == null) {
140
            return;
141
        }
142
        try {
143
            LOGGER.debug("Closing " + obj.getClass().getSimpleName() + " "+ obj.hashCode() + " ("+obj.toString()+")");
144
            obj.close();
145
        } catch (Exception e) {
146
            LOGGER.warn("Problems closing " + obj.getClass().getSimpleName() + " '" + obj.toString() + "'.", e);
147
        }
148
    }
149
}