KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > snaq > db > CachedStatement


1 /*
2     DBPool - JDBC Connection Pool Manager
3     Copyright (c) Giles Winstanley
4 */

5 package snaq.db;
6
7 import snaq.util.Reusable;
8 import java.sql.*;
9
10 /**
11  * Statement wrapper that provides methods for caching support.
12  * @author Giles Winstanley
13  */

14 public class CachedStatement implements Statement, Reusable
15 {
16     protected StatementListener listener;
17     protected Statement st;
18     protected boolean open = true; // Exists to avoid repeat cleaning of statement
19

20
21     /**
22      * Creates a new CachedStatement object, using the supplied Statement.
23      */

24     public CachedStatement(Statement st)
25     {
26         this.st = st;
27     }
28
29     /** Returns a string descriptions of the ResultSet parameters. */
30     protected String JavaDoc getParametersString()
31     {
32         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
33         try
34         {
35             switch(getResultSetType())
36             {
37                 case ResultSet.TYPE_SCROLL_INSENSITIVE:
38                     sb.append("TYPE_SCROLL_INSENSITIVE");
39                     break;
40                 case ResultSet.TYPE_SCROLL_SENSITIVE:
41                     sb.append("TYPE_SCROLL_SENSITIVE");
42                     break;
43                 default:
44                     sb.append("TYPE_FORWARD_ONLY");
45             }
46         }
47         catch (SQLException sqle) { sb.append("TYPE_UNKNOWN"); }
48         sb.append(',');
49         try
50         {
51             switch(getResultSetConcurrency())
52             {
53                 case ResultSet.CONCUR_UPDATABLE:
54                     sb.append("CONCUR_UPDATABLE");
55                     break;
56                 default:
57                     sb.append("CONCUR_READ_ONLY");
58             }
59         }
60         catch (SQLException sqle) { sb.append("CONCUR_UNKNOWN"); }
61         sb.append(',');
62         try
63         {
64             switch(getResultSetHoldability())
65             {
66                 case ResultSet.CLOSE_CURSORS_AT_COMMIT:
67                     sb.append("CLOSE_CURSORS_AT_COMMIT");
68                     break;
69                 case ResultSet.HOLD_CURSORS_OVER_COMMIT:
70                     sb.append("HOLD_CURSORS_OVER_COMMIT");
71             }
72         }
73         catch (SQLException sqle) { sb.append("HOLD_UNKNOWN"); }
74         return sb.toString();
75     }
76
77     // Cleans up the statement ready to be reused or closed.
78
public void recycle() throws SQLException
79     {
80         ResultSet rs = st.getResultSet();
81         if (rs != null)
82             rs.close();
83
84         try { st.clearWarnings(); }
85         catch (SQLException sqle) {} // Caught to fix bug in some drivers
86

87         try { st.clearBatch(); }
88         catch (SQLException sqle) {} // Caught to fix bug in some drivers
89
}
90
91     /**
92      * Overridden to provide caching support.
93      */

94     public void close() throws SQLException
95     {
96         if (!open)
97             return;
98         open = false;
99         // If listener registered, do callback, otherwise release statement
100
if (listener != null)
101             listener.statementClosed(this);
102         else
103             release();
104     }
105
106     /**
107      * Overridden to provide caching support.
108      */

109     public void release() throws SQLException
110     {
111         st.close();
112     }
113
114     /**
115      * Added to provide caching support.
116      */

117     void setOpen()
118     {
119         open = true;
120     }
121
122     /**
123      * Added to provide caching support.
124      */

125     void setStatementListener(StatementListener x)
126     {
127         this.listener = x;
128     }
129
130     //*****************************
131
// Statement interface methods
132
//*****************************
133

134     public ResultSet executeQuery(String JavaDoc sql) throws SQLException
135     {
136         return st.executeQuery(sql);
137     }
138
139     public int executeUpdate(String JavaDoc sql) throws SQLException
140     {
141         return st.executeUpdate(sql);
142     }
143
144     public int getMaxFieldSize() throws SQLException
145     {
146         return st.getMaxFieldSize();
147     }
148
149     public void setMaxFieldSize(int max) throws SQLException
150     {
151         st.setMaxFieldSize(max);
152     }
153
154     public int getMaxRows() throws SQLException
155     {
156         return st.getMaxRows();
157     }
158
159     public void setMaxRows(int max) throws SQLException
160     {
161         st.setMaxRows(max);
162     }
163
164     public void setEscapeProcessing(boolean enable) throws SQLException
165     {
166         st.setEscapeProcessing(enable);
167     }
168
169     public int getQueryTimeout() throws SQLException
170     {
171         return st.getQueryTimeout();
172     }
173
174     public void setQueryTimeout(int seconds) throws SQLException
175     {
176         st.setQueryTimeout(seconds);
177     }
178
179     public void cancel() throws SQLException
180     {
181         st.cancel();
182     }
183
184     public SQLWarning getWarnings() throws SQLException
185     {
186         return st.getWarnings();
187     }
188
189     public void clearWarnings() throws SQLException
190     {
191         st.clearWarnings();
192     }
193
194     public void setCursorName(String JavaDoc name) throws SQLException
195     {
196         st.setCursorName(name);
197     }
198
199     public boolean execute(String JavaDoc sql) throws SQLException
200     {
201         return st.execute(sql);
202     }
203
204     public ResultSet getResultSet() throws SQLException
205     {
206         return st.getResultSet();
207     }
208
209     public int getUpdateCount() throws SQLException
210     {
211         return st.getUpdateCount();
212     }
213
214     public boolean getMoreResults() throws SQLException
215     {
216         return st.getMoreResults();
217     }
218
219     public void setFetchDirection(int direction) throws SQLException
220     {
221         st.setFetchDirection(direction);
222     }
223
224     public int getFetchDirection() throws SQLException
225     {
226         return st.getFetchDirection();
227     }
228
229     public void setFetchSize(int rows) throws SQLException
230     {
231         st.setFetchSize(rows);
232     }
233
234     public int getFetchSize() throws SQLException
235     {
236         return st.getFetchSize();
237     }
238
239     public int getResultSetConcurrency() throws SQLException
240     {
241         return st.getResultSetConcurrency();
242     }
243
244     public int getResultSetType() throws SQLException
245     {
246         return st.getResultSetType();
247     }
248
249     public void addBatch(String JavaDoc sql) throws SQLException
250     {
251         st.addBatch(sql);
252     }
253
254     public void clearBatch() throws SQLException
255     {
256         st.clearBatch();
257     }
258
259     public int[] executeBatch() throws SQLException
260     {
261         return st.executeBatch();
262     }
263
264     public Connection getConnection() throws SQLException
265     {
266         return st.getConnection();
267     }
268
269     //**********************************
270
// Interface methods from JDBC 3.0
271
//**********************************
272

273     public boolean getMoreResults(int current) throws SQLException
274     {
275         return st.getMoreResults(current);
276     }
277
278     public ResultSet getGeneratedKeys() throws SQLException
279     {
280         return st.getGeneratedKeys();
281     }
282
283     public int executeUpdate(String JavaDoc sql, int autoGeneratedKeys) throws SQLException
284     {
285         return st.executeUpdate(sql, autoGeneratedKeys);
286     }
287
288     public int executeUpdate(String JavaDoc sql, int[] columnIndexes) throws SQLException
289     {
290         return st.executeUpdate(sql, columnIndexes);
291     }
292
293     public int executeUpdate(String JavaDoc sql, String JavaDoc[] columnNames) throws SQLException
294     {
295         return st.executeUpdate(sql, columnNames);
296     }
297
298     public boolean execute(String JavaDoc sql, int autoGeneratedKeys) throws SQLException
299     {
300         return st.execute(sql, autoGeneratedKeys);
301     }
302
303     public boolean execute(String JavaDoc sql, int[] columnIndexes) throws SQLException
304     {
305         return st.execute(sql, columnIndexes);
306     }
307
308     public boolean execute(String JavaDoc sql, String JavaDoc[] columnNames) throws SQLException
309     {
310         return st.execute(sql, columnNames);
311     }
312
313     public int getResultSetHoldability() throws SQLException
314     {
315         return st.getResultSetHoldability();
316     }
317 }
Popular Tags