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 @ 43420
History | View | Annotate | Download (4.22 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 | import java.util.List; |
||
9 | import org.gvsig.fmap.dal.ExpressionBuilder.GeometrySupportType; |
||
10 | import static org.gvsig.fmap.dal.ExpressionBuilder.GeometrySupportType.EWKB; |
||
11 | import static org.gvsig.fmap.dal.ExpressionBuilder.GeometrySupportType.WKB; |
||
12 | import static org.gvsig.fmap.dal.ExpressionBuilder.GeometrySupportType.WKT; |
||
13 | import org.gvsig.fmap.geom.Geometry; |
||
14 | import org.slf4j.Logger; |
||
15 | import org.slf4j.LoggerFactory; |
||
16 | |||
17 | public class JDBCUtils { |
||
18 | |||
19 | private static final Logger logger = LoggerFactory.getLogger(JDBCUtils.class); |
||
20 | |||
21 | private JDBCUtils() {
|
||
22 | |||
23 | } |
||
24 | |||
25 | public static ResultSet executeQuery(Statement st, String sql) throws SQLException { |
||
26 | logger.debug("execute query SQL: " + sql);
|
||
27 | ResultSet rs = st.executeQuery(sql);
|
||
28 | return rs;
|
||
29 | } |
||
30 | |||
31 | public static void execute(Statement st, String sql) throws SQLException { |
||
32 | logger.debug("execute SQL: " + sql);
|
||
33 | st.execute(sql); |
||
34 | } |
||
35 | |||
36 | public static ResultSet executeQuery(PreparedStatement st, String sql) throws SQLException { |
||
37 | logger.debug("execute query SQL: " + sql);
|
||
38 | ResultSet rs = st.executeQuery();
|
||
39 | return rs;
|
||
40 | } |
||
41 | |||
42 | public static void execute(PreparedStatement st, String sql) throws SQLException { |
||
43 | logger.debug("execute SQL: " + sql);
|
||
44 | st.execute(); |
||
45 | } |
||
46 | |||
47 | public static int executeUpdate(PreparedStatement st, String sql) throws SQLException { |
||
48 | logger.debug("execute update SQL: "+ sql);
|
||
49 | return st.executeUpdate();
|
||
50 | } |
||
51 | |||
52 | public static void closeQuietly(Statement obj) { |
||
53 | if (obj == null) { |
||
54 | return;
|
||
55 | } |
||
56 | try {
|
||
57 | obj.close(); |
||
58 | } catch (Exception e) { |
||
59 | logger.warn("Problems closing " + obj.getClass().getSimpleName() + " '" + obj.toString() + "'.", e); |
||
60 | } |
||
61 | } |
||
62 | |||
63 | public static void closeQuietly(ResultSet obj) { |
||
64 | if (obj == null) { |
||
65 | return;
|
||
66 | } |
||
67 | try {
|
||
68 | obj.close(); |
||
69 | } catch (Exception e) { |
||
70 | logger.warn("Problems closing " + obj.getClass().getSimpleName() + " '" + obj.toString() + "'.", e); |
||
71 | } |
||
72 | } |
||
73 | |||
74 | public static void closeQuietly(Connection obj) { |
||
75 | if (obj == null) { |
||
76 | return;
|
||
77 | } |
||
78 | try {
|
||
79 | obj.close(); |
||
80 | } catch (Exception e) { |
||
81 | logger.warn("Problems closing " + obj.getClass().getSimpleName() + " '" + obj.toString() + "'.", e); |
||
82 | } |
||
83 | } |
||
84 | |||
85 | public static void closeQuietly(AutoCloseable obj) { |
||
86 | if (obj == null) { |
||
87 | return;
|
||
88 | } |
||
89 | try {
|
||
90 | obj.close(); |
||
91 | } catch (Exception e) { |
||
92 | logger.warn("Problems closing " + obj.getClass().getSimpleName() + " '" + obj.toString() + "'.", e); |
||
93 | } |
||
94 | } |
||
95 | |||
96 | public static void setObjects( |
||
97 | PreparedStatement st,
|
||
98 | List values,
|
||
99 | GeometrySupportType geometrySupportType) throws SQLException { |
||
100 | |||
101 | if (values == null) { |
||
102 | return;
|
||
103 | } |
||
104 | try {
|
||
105 | byte[] bytes; |
||
106 | int columnIndex = 1; |
||
107 | for (Object value : values) { |
||
108 | if (value instanceof Geometry) { |
||
109 | switch(geometrySupportType) {
|
||
110 | case WKT:
|
||
111 | value = ((Geometry) value).convertToWKT(); |
||
112 | st.setObject(columnIndex, value); |
||
113 | break;
|
||
114 | 43358 | jjdelcerro | case NATIVE:
|
115 | 43020 | jjdelcerro | case WKB:
|
116 | bytes = ((Geometry) value).convertToWKB(); |
||
117 | st.setBytes(columnIndex, bytes); |
||
118 | break;
|
||
119 | case EWKB:
|
||
120 | bytes = ((Geometry) value).convertToEWKB(); |
||
121 | st.setBytes(columnIndex, bytes); |
||
122 | break;
|
||
123 | } |
||
124 | } else {
|
||
125 | st.setObject(columnIndex, value); |
||
126 | } |
||
127 | columnIndex++; |
||
128 | } |
||
129 | } catch(Exception ex) { |
||
130 | throw new SQLException("Can't set values for the prepared statement.", ex); |
||
131 | } |
||
132 | } |
||
133 | } |