KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > snaq > db > CachedPreparedStatement


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

5 package snaq.db;
6
7 import java.math.*;
8 import java.net.*;
9 import java.sql.*;
10 import java.util.Calendar JavaDoc;
11 import java.io.InputStream JavaDoc;
12 import java.io.Reader JavaDoc;
13
14 /**
15  * PreparedStatement wrapper that provides caching support.
16  * @author Giles Winstanley
17  */

18 public class CachedPreparedStatement extends CachedStatement implements PreparedStatement
19 {
20     protected String JavaDoc sql;
21
22     /**
23      * Creates a new CachedPreparedStatement object, using the supplied PreparedStatement.
24      */

25     public CachedPreparedStatement(String JavaDoc sql, PreparedStatement st)
26     {
27         super(st);
28         this.sql = sql;
29     }
30
31     String JavaDoc getSQLString()
32     {
33         return sql;
34     }
35
36     public String JavaDoc toString()
37     {
38         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
39         sb.append(super.toString());
40         sb.append(" [");
41         sb.append(sql);
42         sb.append(',');
43         sb.append(getParametersString());
44         sb.append("]");
45         return sb.toString();
46     }
47
48     /**
49      * Overridden to add PreparedStatement specific code.
50      */

51     public void recycle() throws SQLException
52     {
53         super.recycle();
54         PreparedStatement ps = (PreparedStatement)st;
55
56         try { ps.clearParameters(); }
57         catch (NullPointerException JavaDoc npe) {} // Catch clearParameters() bug in Java when no parameters
58
catch (SQLException sqle) {} // Caught to fix bug in some drivers
59
}
60
61     /**
62      * Overridden to provide caching support.
63      */

64     public void release() throws SQLException
65     {
66         st.close();
67     }
68
69     //*************************************
70
// PreparedStatement interface methods
71
//*************************************
72

73     public ResultSet executeQuery() throws SQLException
74     {
75         return ((PreparedStatement)st).executeQuery();
76     }
77
78     public int executeUpdate() throws SQLException
79     {
80         return ((PreparedStatement)st).executeUpdate();
81     }
82
83     public void setNull(int parameterIndex, int sqlType) throws SQLException
84     {
85         ((PreparedStatement)st).setNull(parameterIndex, sqlType);
86     }
87
88     public void setBoolean(int parameterIndex, boolean x) throws SQLException
89     {
90         ((PreparedStatement)st).setBoolean(parameterIndex, x);
91     }
92
93     public void setByte(int parameterIndex, byte x) throws SQLException
94     {
95         ((PreparedStatement)st).setByte(parameterIndex, x);
96     }
97
98     public void setShort(int parameterIndex, short x) throws SQLException
99     {
100         ((PreparedStatement)st).setShort(parameterIndex, x);
101     }
102
103     public void setInt(int parameterIndex, int x) throws SQLException
104     {
105         ((PreparedStatement)st).setInt(parameterIndex, x);
106     }
107
108     public void setLong(int parameterIndex, long x) throws SQLException
109     {
110         ((PreparedStatement)st).setLong(parameterIndex, x);
111     }
112
113     public void setFloat(int parameterIndex, float x) throws SQLException
114     {
115         ((PreparedStatement)st).setFloat(parameterIndex, x);
116     }
117
118     public void setDouble(int parameterIndex, double x) throws SQLException
119     {
120         ((PreparedStatement)st).setDouble(parameterIndex, x);
121     }
122
123     public void setBigDecimal(int parameterIndex, BigDecimal x) throws SQLException
124     {
125         ((PreparedStatement)st).setBigDecimal(parameterIndex, x);
126     }
127
128     public void setString(int parameterIndex, String JavaDoc x) throws SQLException
129     {
130         ((PreparedStatement)st).setString(parameterIndex, x);
131     }
132
133     public void setBytes(int parameterIndex, byte[] x) throws SQLException
134     {
135         ((PreparedStatement)st).setBytes(parameterIndex, x);
136     }
137
138     public void setDate(int parameterIndex, Date x) throws SQLException
139     {
140         ((PreparedStatement)st).setDate(parameterIndex, x);
141     }
142
143     public void setTime(int parameterIndex, Time x) throws SQLException
144     {
145         ((PreparedStatement)st).setTime(parameterIndex, x);
146     }
147
148     public void setTimestamp(int parameterIndex, Timestamp x) throws SQLException
149     {
150         ((PreparedStatement)st).setTimestamp(parameterIndex, x);
151     }
152
153     public void setAsciiStream(int parameterIndex, InputStream JavaDoc x, int length) throws SQLException
154     {
155         ((PreparedStatement)st).setAsciiStream(parameterIndex, x, length);
156     }
157
158     public void setUnicodeStream(int parameterIndex, InputStream JavaDoc x, int length) throws SQLException
159     {
160         ((PreparedStatement)st).setUnicodeStream(parameterIndex, x, length);
161     }
162
163     public void setBinaryStream(int parameterIndex, InputStream JavaDoc x, int length) throws SQLException
164     {
165         ((PreparedStatement)st).setBinaryStream(parameterIndex, x, length);
166     }
167
168     public void clearParameters() throws SQLException
169     {
170         ((PreparedStatement)st).clearParameters();
171     }
172
173     public void setObject(int parameterIndex, Object JavaDoc x, int targetSqlType, int scale) throws SQLException
174     {
175         ((PreparedStatement)st).setObject(parameterIndex, x, targetSqlType, scale);
176     }
177
178     public void setObject(int parameterIndex, Object JavaDoc x, int targetSqlType) throws SQLException
179     {
180         ((PreparedStatement)st).setObject(parameterIndex, x, targetSqlType);
181     }
182
183     public void setObject(int parameterIndex, Object JavaDoc x) throws SQLException
184     {
185         ((PreparedStatement)st).setObject(parameterIndex, x);
186     }
187
188     public boolean execute() throws SQLException
189     {
190         return ((PreparedStatement)st).execute();
191     }
192
193     public void addBatch() throws SQLException
194     {
195         ((PreparedStatement)st).addBatch();
196     }
197
198     public void setCharacterStream(int parameterIndex, Reader JavaDoc reader, int length) throws SQLException
199     {
200         ((PreparedStatement)st).setCharacterStream(parameterIndex, reader, length);
201     }
202
203     public void setRef(int i, Ref x) throws SQLException
204     {
205         ((PreparedStatement)st).setRef(i, x);
206     }
207
208     public void setBlob(int i, Blob x) throws SQLException
209     {
210         ((PreparedStatement)st).setBlob(i, x);
211     }
212
213     public void setClob(int i, Clob x) throws SQLException
214     {
215         ((PreparedStatement)st).setClob(i, x);
216     }
217
218     public void setArray(int i, Array x) throws SQLException
219     {
220         ((PreparedStatement)st).setArray(i, x);
221     }
222
223     public ResultSetMetaData getMetaData() throws SQLException
224     {
225         return ((PreparedStatement)st).getMetaData();
226     }
227
228     public void setDate(int parameterIndex, Date x, Calendar JavaDoc cal) throws SQLException
229     {
230         ((PreparedStatement)st).setDate(parameterIndex, x, cal);
231     }
232
233     public void setTime(int parameterIndex, Time x, Calendar JavaDoc cal) throws SQLException
234     {
235         ((PreparedStatement)st).setTime(parameterIndex, x, cal);
236     }
237
238     public void setTimestamp(int parameterIndex, Timestamp x, Calendar JavaDoc cal) throws SQLException
239     {
240         ((PreparedStatement)st).setTimestamp(parameterIndex, x, cal);
241     }
242
243     public void setNull(int paramIndex, int sqlType, String JavaDoc typeName) throws SQLException
244     {
245         ((PreparedStatement)st).setNull(paramIndex, sqlType, typeName);
246     }
247
248     //**********************************
249
// Interface methods from JDBC 3.0
250
//**********************************
251

252     public ParameterMetaData getParameterMetaData() throws SQLException
253     {
254         return ((PreparedStatement)st).getParameterMetaData();
255     }
256
257     public void setURL(int parameterIndex, URL x) throws SQLException
258     {
259         ((PreparedStatement)st).setURL(parameterIndex, x);
260     }
261 }
Popular Tags