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 @ 44609
History | View | Annotate | Download (4.93 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 java.util.Objects; |
11 |
import org.slf4j.Logger; |
12 |
import org.slf4j.LoggerFactory; |
13 |
|
14 |
@SuppressWarnings("UseSpecificCatch") |
15 |
public class JDBCUtils { |
16 |
|
17 |
private static final Logger LOGGER = LoggerFactory.getLogger(JDBCUtils.class); |
18 |
|
19 |
private JDBCUtils() {
|
20 |
|
21 |
} |
22 |
|
23 |
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 |
public static ResultSet executeQuery(Statement st, String sql) throws SQLException { |
32 |
LOGGER.debug("execute query SQL: " + sql);
|
33 |
try {
|
34 |
ResultSet rs = st.executeQuery(sql);
|
35 |
return rs;
|
36 |
} catch(Exception ex) { |
37 |
LOGGER.warn("execute SQL: " + sql, ex);
|
38 |
throw ex;
|
39 |
} |
40 |
} |
41 |
|
42 |
public static void execute(Statement st, String sql) throws SQLException { |
43 |
LOGGER.debug("execute SQL: " + sql);
|
44 |
try {
|
45 |
st.execute(sql); |
46 |
} catch(Exception ex) { |
47 |
LOGGER.warn("execute SQL: " + sql, ex);
|
48 |
throw ex;
|
49 |
} |
50 |
} |
51 |
|
52 |
public static void execute(Connection connection, String sql) throws SQLException { |
53 |
LOGGER.debug("execute SQL: " + sql);
|
54 |
Statement st = connection.createStatement();
|
55 |
try {
|
56 |
st.execute(sql); |
57 |
} catch(Exception ex) { |
58 |
LOGGER.warn("execute SQL: " + sql, ex);
|
59 |
throw ex;
|
60 |
} |
61 |
} |
62 |
|
63 |
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 |
LOGGER.debug("execute SQL: " + sql);
|
72 |
st.execute(sql); |
73 |
} |
74 |
} catch (SQLException ex) { |
75 |
LOGGER.warn("execute SQL: " + sql, ex);
|
76 |
throw ex;
|
77 |
} finally {
|
78 |
JDBCUtils.closeQuietly(st); |
79 |
} |
80 |
} |
81 |
|
82 |
public static ResultSet executeQuery(PreparedStatement st, String sql) throws SQLException { |
83 |
if( LOGGER.isDebugEnabled() ) {
|
84 |
LOGGER.debug("execute query SQL= "+ getSQLInfo(st,sql));
|
85 |
} |
86 |
try {
|
87 |
ResultSet rs = st.executeQuery();
|
88 |
return rs;
|
89 |
} catch(Exception ex) { |
90 |
LOGGER.warn("execute query SQL: " + getSQLInfo(st,sql), ex);
|
91 |
throw ex;
|
92 |
} |
93 |
} |
94 |
|
95 |
public static void execute(PreparedStatement st, String sql) throws SQLException { |
96 |
if( LOGGER.isDebugEnabled() ) {
|
97 |
LOGGER.debug("execute SQL= "+ getSQLInfo(st,sql));
|
98 |
} |
99 |
try {
|
100 |
st.execute(); |
101 |
} catch(Exception ex) { |
102 |
LOGGER.warn("execute SQL: " + getSQLInfo(st,sql), ex);
|
103 |
throw ex;
|
104 |
} |
105 |
} |
106 |
|
107 |
public static int executeUpdate(PreparedStatement st, String sql) throws SQLException { |
108 |
if( LOGGER.isDebugEnabled() ) {
|
109 |
LOGGER.debug("execute update SQL= "+ getSQLInfo(st,sql));
|
110 |
} |
111 |
try {
|
112 |
return st.executeUpdate();
|
113 |
} catch(Exception ex) { |
114 |
LOGGER.warn("execute update SQL: " + getSQLInfo(st,sql), ex);
|
115 |
throw ex;
|
116 |
} |
117 |
} |
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 |
LOGGER.warn("Problems closing " + obj.getClass().getSimpleName() + " '" + obj.toString() + "'.", e); |
127 |
} |
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 |
LOGGER.warn("Problems closing " + obj.getClass().getSimpleName() + " '" + obj.toString() + "'.", e); |
138 |
} |
139 |
} |
140 |
|
141 |
public static void closeQuietly(Connection conn) { |
142 |
if (conn == null) { |
143 |
return;
|
144 |
} |
145 |
LOGGER.debug("Closing connection "+ conn.hashCode() + " ("+conn.toString()+")"); |
146 |
try {
|
147 |
conn.close(); |
148 |
} catch (Exception e) { |
149 |
LOGGER.warn("Problems closing " + conn.getClass().getSimpleName() + " '" + conn.toString() + "'.", e); |
150 |
} |
151 |
} |
152 |
|
153 |
public static void closeQuietly(AutoCloseable obj) { |
154 |
if (obj == null) { |
155 |
return;
|
156 |
} |
157 |
try {
|
158 |
LOGGER.debug("Closing " + obj.getClass().getSimpleName() + " "+ obj.hashCode() + " ("+obj.toString()+")"); |
159 |
obj.close(); |
160 |
} catch (Exception e) { |
161 |
LOGGER.warn("Problems closing " + obj.getClass().getSimpleName() + " '" + obj.toString() + "'.", e); |
162 |
} |
163 |
} |
164 |
} |