1 package com.instantbank.collections.util;
2
3 import java.sql.Connection;
4 import java.sql.PreparedStatement;
5 import java.sql.ResultSet;
6 import java.sql.SQLException;
7
8
9
10 public class SqlUtil {
11
12
13 private SqlUtil() { }
14
15
16
24 public static void release(Connection con, PreparedStatement ps, ResultSet rs)
25 throws SQLException {
26
27 try {
28 if(rs != null) {
29 rs.close();
30 }
31 }
32 catch(Exception e) {
33 System.out.println("Error closing ResultSet: " + e);
34 e.printStackTrace();
35 }
36
37 try {
38 if(ps != null) {
39 ps.close();
40 }
41 }
42 catch(Exception e) {
43 System.out.println("Error closing PreparedStatement: " + e);
44 e.printStackTrace();
45 }
46
47 try {
48 if(con != null) {
49 con.close();
50 }
51 }
52 catch(Exception e) {
53 System.out.println("Error closing Connection: " + e);
54 e.printStackTrace();
55 }
56 }
57
58
59
69 public static final long nextOracleSequence(Connection con, boolean closeConnection,
70 String sequenceName) throws SQLException {
71
72 PreparedStatement ps = null;
73 ResultSet rs = null;
74
75 if(con == null) {
76 throw new SQLException("Null connection passed to nextOracleSequence");
77 }
78
79 if(sequenceName == null) {
80 throw new SQLException("Null sequenceName passed to nextOracleSequence");
81 }
82
83 try {
84
85 long nextValue = 0;
86
87 String query = "select " + sequenceName + ".nextval from dual";
88 ps = con.prepareStatement(query);
89 ps.executeQuery();
90
91 rs = ps.getResultSet();
92 if(rs.next()) {
93 nextValue = rs.getLong(1);
94 }
95 else {
96 String err = "Error: No next value returned for sequence table " + sequenceName;
97 System.out.println(err);
98 throw new SQLException(err);
99 }
100
101 return nextValue;
102 }
103 finally {
104
105 try {
106 if(rs != null) {
107 rs.close();
108 }
109 }
110 catch(Exception e) {
111 System.out.println("Error closing ResultSet: " + e);
112 throw new SQLException(e.toString());
113 }
114
115 try {
116 if(ps != null) {
117 ps.close();
118 }
119 }
120 catch(Exception e) {
121 System.out.println("Error closing PreparedStatement: " + e);
122 throw new SQLException(e.toString());
123 }
124
125
126 if(closeConnection) {
127 try {
128 con.close();
129 }
130 catch(Exception e) {
131 System.out.println("Error closing Connection: " + e);
132 throw new SQLException(e.toString());
133 }
134 }
135 }
136 }
137
138 }
139