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 | 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 | 43020 | jjdelcerro | import org.slf4j.Logger; |
11 | import org.slf4j.LoggerFactory; |
||
12 | |||
13 | 44191 | jjdelcerro | @SuppressWarnings("UseSpecificCatch") |
14 | 43020 | jjdelcerro | public class JDBCUtils { |
15 | |||
16 | 44191 | jjdelcerro | private static final Logger LOGGER = LoggerFactory.getLogger(JDBCUtils.class); |
17 | 43020 | jjdelcerro | |
18 | private JDBCUtils() {
|
||
19 | |||
20 | } |
||
21 | |||
22 | public static ResultSet executeQuery(Statement st, String sql) throws SQLException { |
||
23 | 44191 | jjdelcerro | LOGGER.debug("execute query SQL: " + sql);
|
24 | 43756 | jjdelcerro | try {
|
25 | ResultSet rs = st.executeQuery(sql);
|
||
26 | return rs;
|
||
27 | } catch(Exception ex) { |
||
28 | 44191 | jjdelcerro | LOGGER.warn("execute SQL: " + sql, ex);
|
29 | 43756 | jjdelcerro | throw ex;
|
30 | } |
||
31 | 43020 | jjdelcerro | } |
32 | |||
33 | public static void execute(Statement st, String sql) throws SQLException { |
||
34 | 44191 | jjdelcerro | LOGGER.debug("execute SQL: " + sql);
|
35 | 43756 | jjdelcerro | try {
|
36 | st.execute(sql); |
||
37 | } catch(Exception ex) { |
||
38 | 44191 | jjdelcerro | LOGGER.warn("execute SQL: " + sql, ex);
|
39 | 43756 | jjdelcerro | throw ex;
|
40 | } |
||
41 | 43020 | jjdelcerro | } |
42 | |||
43 | 43650 | jjdelcerro | public static void execute(Connection connection, String sql) throws SQLException { |
44 | 44191 | jjdelcerro | LOGGER.debug("execute SQL: " + sql);
|
45 | 43650 | jjdelcerro | Statement st = connection.createStatement();
|
46 | 43756 | jjdelcerro | try {
|
47 | st.execute(sql); |
||
48 | } catch(Exception ex) { |
||
49 | 44191 | jjdelcerro | LOGGER.warn("execute SQL: " + sql, ex);
|
50 | 43756 | jjdelcerro | throw ex;
|
51 | } |
||
52 | 43650 | jjdelcerro | } |
53 | |||
54 | 43775 | jjdelcerro | 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 | 44191 | jjdelcerro | LOGGER.debug("execute SQL: " + sql);
|
63 | 43775 | jjdelcerro | st.execute(sql); |
64 | } |
||
65 | } catch (SQLException ex) { |
||
66 | 44191 | jjdelcerro | LOGGER.warn("execute SQL: " + sql, ex);
|
67 | 43775 | jjdelcerro | throw ex;
|
68 | } finally {
|
||
69 | JDBCUtils.closeQuietly(st); |
||
70 | } |
||
71 | } |
||
72 | |||
73 | 43020 | jjdelcerro | public static ResultSet executeQuery(PreparedStatement st, String sql) throws SQLException { |
74 | 44191 | jjdelcerro | LOGGER.debug("execute query SQL: " + sql);
|
75 | 43756 | jjdelcerro | try {
|
76 | ResultSet rs = st.executeQuery();
|
||
77 | return rs;
|
||
78 | } catch(Exception ex) { |
||
79 | 44191 | jjdelcerro | LOGGER.warn("execute SQL: " + sql, ex);
|
80 | 43756 | jjdelcerro | throw ex;
|
81 | } |
||
82 | 43020 | jjdelcerro | } |
83 | |||
84 | public static void execute(PreparedStatement st, String sql) throws SQLException { |
||
85 | 44191 | jjdelcerro | LOGGER.debug("execute SQL: " + sql);
|
86 | 43756 | jjdelcerro | try {
|
87 | st.execute(); |
||
88 | } catch(Exception ex) { |
||
89 | 44191 | jjdelcerro | LOGGER.warn("execute SQL: " + sql, ex);
|
90 | 43756 | jjdelcerro | throw ex;
|
91 | } |
||
92 | 43020 | jjdelcerro | } |
93 | |||
94 | public static int executeUpdate(PreparedStatement st, String sql) throws SQLException { |
||
95 | 44191 | jjdelcerro | LOGGER.debug("execute update SQL: "+ sql);
|
96 | 43756 | jjdelcerro | try {
|
97 | return st.executeUpdate();
|
||
98 | } catch(Exception ex) { |
||
99 | 44191 | jjdelcerro | LOGGER.warn("execute SQL: " + sql, ex);
|
100 | 43756 | jjdelcerro | throw ex;
|
101 | } |
||
102 | 43020 | jjdelcerro | } |
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 | 44191 | jjdelcerro | LOGGER.warn("Problems closing " + obj.getClass().getSimpleName() + " '" + obj.toString() + "'.", e); |
112 | 43020 | jjdelcerro | } |
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 | 44191 | jjdelcerro | LOGGER.warn("Problems closing " + obj.getClass().getSimpleName() + " '" + obj.toString() + "'.", e); |
123 | 43020 | jjdelcerro | } |
124 | } |
||
125 | |||
126 | 44191 | jjdelcerro | public static void closeQuietly(Connection conn) { |
127 | if (conn == null) { |
||
128 | 43020 | jjdelcerro | return;
|
129 | } |
||
130 | 44191 | jjdelcerro | LOGGER.debug("Closing connection "+ conn.hashCode() + " ("+conn.toString()+")"); |
131 | 43020 | jjdelcerro | try {
|
132 | 44191 | jjdelcerro | conn.close(); |
133 | 43020 | jjdelcerro | } catch (Exception e) { |
134 | 44191 | jjdelcerro | LOGGER.warn("Problems closing " + conn.getClass().getSimpleName() + " '" + conn.toString() + "'.", e); |
135 | 43020 | jjdelcerro | } |
136 | } |
||
137 | |||
138 | public static void closeQuietly(AutoCloseable obj) { |
||
139 | if (obj == null) { |
||
140 | return;
|
||
141 | } |
||
142 | try {
|
||
143 | 44191 | jjdelcerro | LOGGER.debug("Closing " + obj.getClass().getSimpleName() + " "+ obj.hashCode() + " ("+obj.toString()+")"); |
144 | 43020 | jjdelcerro | obj.close(); |
145 | } catch (Exception e) { |
||
146 | 44191 | jjdelcerro | LOGGER.warn("Problems closing " + obj.getClass().getSimpleName() + " '" + obj.toString() + "'.", e); |
147 | 43020 | jjdelcerro | } |
148 | } |
||
149 | } |