KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > sql > UserPreparedStatement


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.sql;
31
32 import com.caucho.log.Log;
33 import com.caucho.util.L10N;
34
35 import java.io.InputStream JavaDoc;
36 import java.io.Reader JavaDoc;
37 import java.math.BigDecimal JavaDoc;
38 import java.net.URL JavaDoc;
39 import java.sql.*;
40 import java.util.Calendar JavaDoc;
41 import java.util.logging.Logger JavaDoc;
42
43 /**
44  * User-view of prepared statements
45  */

46 public class UserPreparedStatement extends UserStatement
47   implements PreparedStatement {
48   protected final static Logger JavaDoc log = Log.open(UserPreparedStatement.class);
49   protected static L10N L = new L10N(UserPreparedStatement.class);
50
51   protected PreparedStatement _pstmt;
52   protected PreparedStatementCacheItem _cacheItem;
53
54   private boolean _isClosed;
55   
56   UserPreparedStatement(UserConnection conn,
57             PreparedStatement pStmt,
58             PreparedStatementCacheItem cacheItem)
59   {
60     super(conn, pStmt);
61     
62     _pstmt = pStmt;
63     _cacheItem = cacheItem;
64
65     if (pStmt == null)
66       throw new NullPointerException JavaDoc();
67   }
68   
69   UserPreparedStatement(UserConnection conn,
70             PreparedStatement pStmt)
71   {
72     this(conn, pStmt, null);
73   }
74
75   /**
76    * Returns the underlying statement.
77    */

78   public PreparedStatement getPreparedStatement()
79   {
80     return _pstmt;
81   }
82
83   /**
84    * Executes the prepared statement's query.
85    */

86   public ResultSet executeQuery()
87     throws SQLException
88   {
89     return _pstmt.executeQuery();
90   }
91
92   /**
93    * Executes the prepared statement's sql as an update
94    */

95   public int executeUpdate()
96     throws SQLException
97   {
98     return _pstmt.executeUpdate();
99   }
100
101   /**
102    * Executes the prepared statement's sql as an update or query
103    */

104   public boolean execute()
105     throws SQLException
106   {
107     return _pstmt.execute();
108   }
109
110   /**
111    * Adds the statement as a batch.
112    */

113   public void addBatch()
114     throws SQLException
115   {
116     _pstmt.addBatch();
117   }
118
119   /**
120    * Clears the statement's parameters.
121    */

122   public void clearParameters()
123     throws SQLException
124   {
125     _pstmt.clearParameters();
126   }
127
128   /**
129    * Returns the result metadata.
130    */

131   public ResultSetMetaData getMetaData()
132     throws SQLException
133   {
134     return _pstmt.getMetaData();
135   }
136
137   /**
138    * Returns the prepared statement's meta data.
139    */

140   public ParameterMetaData getParameterMetaData()
141     throws SQLException
142   {
143     return _pstmt.getParameterMetaData();
144   }
145
146   /**
147    * Sets the parameter as a null.
148    */

149   public void setNull(int parameterIndex, int sqlType)
150     throws SQLException
151   {
152     _pstmt.setNull(parameterIndex, sqlType);
153   }
154
155   /**
156    * Sets the parameter as a null.
157    */

158   public void setNull(int parameterIndex, int sqlType, String JavaDoc typeName)
159     throws SQLException
160   {
161     _pstmt.setNull(parameterIndex, sqlType, typeName);
162   }
163
164   /**
165    * Sets the parameter as a boolean.
166    */

167   public void setBoolean(int index, boolean value)
168     throws SQLException
169   {
170     _pstmt.setBoolean(index, value);
171   }
172
173   /**
174    * Sets the parameter as a byte.
175    */

176   public void setByte(int index, byte value)
177     throws SQLException
178   {
179     _pstmt.setByte(index, value);
180   }
181
182   /**
183    * Sets the parameter as a short.
184    */

185   public void setShort(int index, short value)
186     throws SQLException
187   {
188     _pstmt.setShort(index, value);
189   }
190
191   /**
192    * Sets the parameter as an int
193    */

194   public void setInt(int index, int value)
195     throws SQLException
196   {
197     _pstmt.setInt(index, value);
198   }
199
200   /**
201    * Sets the parameter as a long
202    */

203   public void setLong(int index, long value)
204     throws SQLException
205   {
206     _pstmt.setLong(index, value);
207   }
208
209   /**
210    * Sets the parameter as a float
211    */

212   public void setFloat(int index, float value)
213     throws SQLException
214   {
215     _pstmt.setFloat(index, value);
216   }
217
218   /**
219    * Sets the parameter as a double
220    */

221   public void setDouble(int index, double value)
222     throws SQLException
223   {
224     _pstmt.setDouble(index, value);
225   }
226
227   /**
228    * Sets the parameter as a BigDecimal
229    */

230   public void setBigDecimal(int index, BigDecimal JavaDoc value)
231     throws SQLException
232   {
233     _pstmt.setBigDecimal(index, value);
234   }
235
236   /**
237    * Sets the parameter as a string
238    */

239   public void setString(int index, String JavaDoc value)
240     throws SQLException
241   {
242     _pstmt.setString(index, value);
243   }
244
245   /**
246    * Sets the parameter as a byte array.
247    */

248   public void setBytes(int index, byte []value)
249     throws SQLException
250   {
251     _pstmt.setBytes(index, value);
252   }
253
254   /**
255    * Sets the parameter as a date
256    */

257   public void setDate(int index, Date value)
258     throws SQLException
259   {
260     _pstmt.setDate(index, value);
261   }
262
263   /**
264    * Sets the parameter as a time
265    */

266   public void setDate(int index, Date value, Calendar JavaDoc cal)
267     throws SQLException
268   {
269     _pstmt.setDate(index, value, cal);
270   }
271
272   /**
273    * Sets the parameter as a time
274    */

275   public void setTime(int index, Time value)
276     throws SQLException
277   {
278     _pstmt.setTime(index, value);
279   }
280
281   /**
282    * Sets the parameter as a time
283    */

284   public void setTime(int index, Time value, Calendar JavaDoc cal)
285     throws SQLException
286   {
287     _pstmt.setTime(index, value, cal);
288   }
289
290   /**
291    * Sets the parameter as a timestamp
292    */

293   public void setTimestamp(int index, Timestamp value)
294     throws SQLException
295   {
296     _pstmt.setTimestamp(index, value);
297   }
298
299   /**
300    * Sets the parameter as a timestamp
301    */

302   public void setTimestamp(int index, Timestamp value, Calendar JavaDoc cal)
303     throws SQLException
304   {
305     _pstmt.setTimestamp(index, value, cal);
306   }
307
308   /**
309    * Sets the parameter as an ascii stream.
310    */

311   public void setAsciiStream(int index, InputStream JavaDoc value, int length)
312     throws SQLException
313   {
314     _pstmt.setAsciiStream(index, value, length);
315   }
316
317   /**
318    * Sets the parameter as a unicode stream.
319    */

320   public void setUnicodeStream(int index, InputStream JavaDoc value, int length)
321     throws SQLException
322   {
323     _pstmt.setUnicodeStream(index, value, length);
324   }
325
326   /**
327    * Sets the parameter as a binary stream.
328    */

329   public void setBinaryStream(int index, InputStream JavaDoc value, int length)
330     throws SQLException
331   {
332     _pstmt.setBinaryStream(index, value, length);
333   }
334
335   /**
336    * Sets the parameter as an character stream.
337    */

338   public void setCharacterStream(int index, Reader JavaDoc value, int length)
339     throws SQLException
340   {
341     _pstmt.setCharacterStream(index, value, length);
342   }
343
344   /**
345    * Sets the parameter as an object with the given type and scale.
346    */

347   public void setObject(int index, Object JavaDoc value, int type, int scale)
348     throws SQLException
349   {
350     _pstmt.setObject(index, value, type, scale);
351   }
352
353   /**
354    * Sets the parameter as an object with the given type.
355    */

356   public void setObject(int index, Object JavaDoc value, int type)
357     throws SQLException
358   {
359     _pstmt.setObject(index, value, type);
360   }
361
362   /**
363    * Sets the parameter as a object.
364    */

365   public void setObject(int index, Object JavaDoc value)
366     throws SQLException
367   {
368     _pstmt.setObject(index, value);
369   }
370
371   /**
372    * Sets teh parameter as a ref.
373    */

374   public void setRef(int index, Ref value)
375     throws SQLException
376   {
377     _pstmt.setRef(index, value);
378   }
379
380   /**
381    * Sets the parameter as a blob.
382    */

383   public void setBlob(int index, Blob value)
384     throws SQLException
385   {
386     _pstmt.setBlob(index, value);
387   }
388
389   /**
390    * Sets the parameter as a clob.
391    */

392   public void setClob(int index, Clob value)
393     throws SQLException
394   {
395     _pstmt.setClob(index, value);
396   }
397
398   /**
399    * Sets the parameter as an array
400    */

401   public void setArray(int index, Array value)
402     throws SQLException
403   {
404     _pstmt.setArray(index, value);
405   }
406
407   /**
408    * Sets the parameter as a URL.
409    */

410   public void setURL(int index, URL JavaDoc value)
411     throws SQLException
412   {
413     _pstmt.setURL(index, value);
414   }
415
416   /**
417    * Closes the prepared statement.
418    */

419   public void close()
420     throws SQLException
421   {
422     synchronized (this) {
423       if (_isClosed)
424     return;
425       _isClosed = true;
426     }
427     
428     clearParameters();
429
430     if (_cacheItem == null)
431       super.close();
432     else if (_isChanged)
433       _cacheItem.destroy();
434     else
435       _cacheItem.toIdle();
436   }
437
438   public String JavaDoc toString()
439   {
440     return "UserPreparedStatement[" + _pstmt + "]";
441   }
442 }
443
Popular Tags