1 /
5
6 package com.instantbank.common.utilcomponents;
7
8 import java.sql.Connection;
9 import java.sql.PreparedStatement;
10 import java.sql.ResultSet;
11 import java.sql.SQLException;
12
13 public class SqlUtils {
14
15
16 private SqlUtils() { }
17
18
19
27 public static void release(Connection con, PreparedStatement ps, ResultSet rs)
28 throws SQLException {
29
30 try {
31 if(rs != null) {
32 rs.close();
33 }
34 }
35 catch(Exception e) {
36 System.out.println("Error closing ResultSet: " + e);
37 e.printStackTrace();
38 }
39
40 try {
41 if(ps != null) {
42 ps.close();
43 }
44 }
45 catch(Exception e) {
46 System.out.println("Error closing PreparedStatement: " + e);
47 e.printStackTrace();
48 }
49
50 try {
51 if(con != null) {
52 con.close();
53 }
54 }
55 catch(Exception e) {
56 System.out.println("Error closing Connection: " + e);
57 e.printStackTrace();
58 }
59 }
60
61
62
72 public static final long nextOracleSequence(Connection con, boolean closeConnection,
73 String sequenceName) throws SQLException {
74
75 PreparedStatement ps = null;
76 ResultSet rs = null;
77
78 if(con == null) {
79 throw new SQLException("Null connection passed to nextOracleSequence");
80 }
81
82 if(sequenceName == null) {
83 throw new SQLException("Null sequenceName passed to nextOracleSequence");
84 }
85
86 try {
87
88 long nextValue = 0;
89
90 String query = "select " + sequenceName + ".nextval from dual";
91 ps = con.prepareStatement(query);
92 ps.executeQuery();
93
94 rs = ps.getResultSet();
95 if(rs.next()) {
96 nextValue = rs.getLong(1);
97 }
98 else {
99 String err = "Error: No next value returned for sequence table " + sequenceName;
100 System.out.println(err);
101 throw new SQLException(err);
102 }
103
104 return nextValue;
105 }
106 finally {
107
108 try {
109 if(rs != null) {
110 rs.close();
111 }
112 }
113 catch(Exception e) {
114 System.out.println("Error closing ResultSet: " + e);
115 throw new SQLException(e.toString());
116 }
117
118 try {
119 if(ps != null) {
120 ps.close();
121 }
122 }
123 catch(Exception e) {
124 System.out.println("Error closing PreparedStatement: " + e);
125 throw new SQLException(e.toString());
126 }
127
128
129 if(closeConnection) {
130 try {
131 con.close();
132 }
133 catch(Exception e) {
134 System.out.println("Error closing Connection: " + e);
135 throw new SQLException(e.toString());
136 }
137 }
138 }
139 }
140
141
142 public static final long getAgreementCodeFieldIndex(Connection dbConnection) throws DAOException {
143 long lIndex = -1;
144 String fieldColumn = "";
145
146 PreparedStatement ps = null;
147 ResultSet rs = null;
148 String queryStrFields =
149 " SELECT field_id, field_column"
150 + " FROM "
151 + DatabaseNames.LETT_FIELD + " "
152 + " WHERE field_column= 'AGRM_CODE'";
153
154 try {
155 ps = dbConnection.prepareStatement(queryStrFields);
156
157 ps.executeQuery();
158 rs = ps.getResultSet();
159 while(rs.next()) {
160 lIndex = rs.getLong(1);
161 fieldColumn = rs.getString(2).trim();
162 break;
163 }
164 }
165 catch(Exception ae) {
166
167 throw new DAOException
168 (LetterTemplateExceptionMessage.PROBLEM_LOAD_FIELDS
169 + ae.getMessage());
170 }
171 finally {
172 DAOUtil.closeResultSet(rs);
173 DAOUtil.closePreparedStatement(ps);
174
175 }
176
177
178
179 return lIndex;
180 }
181
182
183 public static final long getActionCodeFieldIndex(Connection dbConnection, String sActionCode, long lCompID) throws DAOException {
184 long lIndex = -1;
185 String fieldColumn = "";
186
187 PreparedStatement ps = null;
188 ResultSet rs = null;
189 String queryStrFields =
190 " SELECT act_id, act_code"
191 + " FROM "
192 + DatabaseNames.COLL_ACTIONS + " "
193 + " WHERE act_code = ? AND act_cmp_id = ?";
194
195 try {
196 ps = dbConnection.prepareStatement(queryStrFields);
197 ps.setString(1, sActionCode);
198 ps.setLong(2, lCompID);
199 ps.executeQuery();
200 rs = ps.getResultSet();
201 while(rs.next()) {
202 lIndex = rs.getLong(1);
203 fieldColumn = rs.getString(2).trim();
204 break;
205 }
206 }
207 catch(Exception ae) {
208
209 throw new DAOException
210 (LetterTemplateExceptionMessage.PROBLEM_ACTION_CODE
211 + ae.getMessage());
212 }
213 finally {
214 DAOUtil.closeResultSet(rs);
215 DAOUtil.closePreparedStatement(ps);
216
217 }
218
219
220
221 return lIndex;
222 }
223
224
225 public static final long getResultCodeFieldIndex(Connection dbConnection, String sResultCode, long lCompID) throws DAOException {
226 long lIndex = -1;
227 String fieldColumn = "";
228
229 PreparedStatement ps = null;
230 ResultSet rs = null;
231 String queryStrFields =
232 " SELECT res_id, res_code"
233 + " FROM "
234 + DatabaseNames.COLL_RESULTS + " "
235 + " WHERE res_code = ? AND res_cmp_id = ?";
236
237 try {
238 ps = dbConnection.prepareStatement(queryStrFields);
239 ps.setString(1, sResultCode);
240 ps.setLong(2, lCompID);
241
242 ps.executeQuery();
243 rs = ps.getResultSet();
244 while(rs.next()) {
245 lIndex = rs.getLong(1);
246 fieldColumn = rs.getString(2).trim();
247 break;
248 }
249 }
250 catch(Exception ae) {
251
252 throw new DAOException
253 (LetterTemplateExceptionMessage.PROBLEM_RESULT_CODE
254 + ae.getMessage());
255 }
256 finally {
257 DAOUtil.closeResultSet(rs);
258 DAOUtil.closePreparedStatement(ps);
259
260 }
261
262
263
264 return lIndex;
265 }
266
267
268 public static final long getLetterTemplateIndex(Connection dbConnection, String sLetterCode, long lCompID) throws DAOException {
269 long lIndex = -1;
270 String fieldColumn = "";
271
272 PreparedStatement ps = null;
273 ResultSet rs = null;
274 String queryStrFields =
275 " SELECT let_id, let_code"
276 + " FROM "
277 + DatabaseNames.COLL_LETTER_TEMPLATES + " "
278 + " WHERE let_code = ? AND let_cmp_id = ?";
279
280 try {
281 ps = dbConnection.prepareStatement(queryStrFields);
282 ps.setString(1, sLetterCode);
283 ps.setLong(2, lCompID);
284
285 ps.executeQuery();
286 rs = ps.getResultSet();
287 while(rs.next()) {
288 lIndex = rs.getLong(1);
289 fieldColumn = rs.getString(2).trim();
290 break;
291 }
292 }
293 catch(Exception ae) {
294
295 throw new DAOException
296 (LetterTemplateExceptionMessage.PROBLEM_LETTER_CODE
297 + ae.getMessage());
298 }
299 finally {
300 DAOUtil.closeResultSet(rs);
301 DAOUtil.closePreparedStatement(ps);
302
303 }
304
305
306
307 return lIndex;
308 }
309
310
311 public static final Long getAgreementId(Connection dbConnection, long lCompID, long lAgrmCode) throws DAOException {
312 long lIndex = -1;
313
314 PreparedStatement ps = null;
315 ResultSet rs = null;
316 String queryStrFields = "SELECT agrm_id FROM agreements WHERE agrm_code= ? and agrm_cmp_id = ?";
317
318 try {
319 ps = dbConnection.prepareStatement(queryStrFields);
320 ps.setLong(1, lAgrmCode);
321 ps.setLong(2, lCompID);
322
323 ps.executeQuery();
324 rs = ps.getResultSet();
325 while(rs.next()) {
326 lIndex = rs.getLong(1);
327 break;
328 }
329 }
330 catch(Exception ae) {
331
332 throw new DAOException
333 (LetterTemplateExceptionMessage.PROBLEM_LETTER_CODE
334 + ae.getMessage());
335 }
336 finally {
337 DAOUtil.closeResultSet(rs);
338 DAOUtil.closePreparedStatement(ps);
339
340 }
341
342
343
344 return new Long(lIndex);
345 }
346
347 }
348