KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > jdbc > conn > PooledPreparedStatement


1
2 /*
3  * Copyright (c) 1998 - 2005 Versant Corporation
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  * Versant Corporation - initial API and implementation
11  */

12 package com.versant.core.jdbc.conn;
13
14 import com.versant.core.logging.LogEventStore;
15 import com.versant.core.jdbc.logging.JdbcStatementEvent;
16 import com.versant.core.logging.LogEventStore;
17
18 import java.math.BigDecimal JavaDoc;
19 import java.io.InputStream JavaDoc;
20 import java.io.Reader JavaDoc;
21 import java.util.Calendar JavaDoc;
22 import java.sql.*;
23 import java.net.URL JavaDoc;
24
25 /**
26  * A JDBC PreparedStatement wrapped for event logging that can be pooled by
27  * PooledConnection.
28  */

29 public class PooledPreparedStatement extends LoggingStatement implements PreparedStatement {
30
31     protected final PreparedStatement statement;
32     protected final String JavaDoc sql;
33     private final PreparedStatementPool.Key key;
34
35     public PooledPreparedStatement next; // linked list for PsPool
36

37     public PooledPreparedStatement(LoggingConnection con,
38             PreparedStatement statement, LogEventStore pes, String JavaDoc sql,
39             PreparedStatementPool.Key key) {
40         super(con, statement, pes);
41         this.statement = statement;
42         this.sql = sql;
43         this.key = key;
44     }
45
46     /**
47      * Close the real statement.
48      */

49     public void closeRealStatement() throws SQLException {
50         super.close();
51     }
52
53     public PreparedStatementPool.Key getKey() {
54         return key;
55     }
56
57     /**
58      * If this is a pooled ps then return it to the pool otherwise close
59      * it normally.
60      */

61     public void close() throws SQLException {
62         if (key != null) con.returnPreparedStatement(this);
63         else super.close();
64     }
65
66     protected String JavaDoc getSql() {
67         return sql;
68     }
69
70     public void setURL(int parameterIndex, URL JavaDoc x) throws SQLException {
71         statement.setURL(parameterIndex, x);
72     }
73
74     public ParameterMetaData getParameterMetaData() throws SQLException {
75         return statement.getParameterMetaData();
76     }
77
78     public ResultSet executeQuery() throws SQLException {
79         if (!pes.isFine()) return statement.executeQuery();
80         JdbcStatementEvent ev = new JdbcStatementEvent(0, this,
81             sql, JdbcStatementEvent.STAT_EXEC_QUERY);
82         pes.log(ev);
83         try {
84             ResultSet rs = statement.executeQuery();
85             ev.updateResultSetID(rs);
86             if (pes.isFiner()) rs = new LoggingResultSet(this, sql, rs, pes);
87             return rs;
88         } catch (SQLException e) {
89             con.setNeedsValidation(true);
90             ev.setErrorMsg(e);
91             throw e;
92         } catch (RuntimeException JavaDoc e) {
93             con.setNeedsValidation(true);
94             ev.setErrorMsg(e);
95             throw e;
96         } finally {
97             ev.updateTotalMs();
98         }
99     }
100
101     public int executeUpdate() throws SQLException {
102         if (!pes.isFine()) return statement.executeUpdate();
103         JdbcStatementEvent ev = new JdbcStatementEvent(0, this,
104             sql, JdbcStatementEvent.STAT_EXEC_UPDATE);
105         pes.log(ev);
106         try {
107             int c = statement.executeUpdate();
108             ev.setUpdateCount(c);
109             return c;
110         } catch (SQLException e) {
111             con.setNeedsValidation(true);
112             ev.setErrorMsg(e);
113             throw e;
114         } catch (RuntimeException JavaDoc e) {
115             con.setNeedsValidation(true);
116             ev.setErrorMsg(e);
117             throw e;
118         } finally {
119             ev.updateTotalMs();
120         }
121     }
122
123     public void setNull(int parameterIndex, int sqlType) throws SQLException {
124         statement.setNull(parameterIndex,sqlType);
125     }
126
127     public void setBoolean(int parameterIndex, boolean x) throws SQLException {
128         statement.setBoolean(parameterIndex,x);
129     }
130
131     public void setByte(int parameterIndex, byte x) throws SQLException {
132         statement.setByte(parameterIndex,x);
133     }
134
135     public void setShort(int parameterIndex, short x) throws SQLException {
136         statement.setShort(parameterIndex,x);
137     }
138
139     public void setInt(int parameterIndex, int x) throws SQLException {
140         statement.setInt(parameterIndex,x);
141     }
142
143     public void setLong(int parameterIndex, long x) throws SQLException {
144         statement.setLong(parameterIndex,x);
145     }
146
147     public void setFloat(int parameterIndex, float x) throws SQLException {
148         statement.setFloat(parameterIndex,x);
149     }
150
151     public void setDouble(int parameterIndex, double x) throws SQLException {
152         statement.setDouble(parameterIndex,x);
153     }
154
155     public void setBigDecimal(int parameterIndex, BigDecimal JavaDoc x) throws SQLException {
156         statement.setBigDecimal(parameterIndex,x);
157     }
158
159     public void setString(int parameterIndex, String JavaDoc x) throws SQLException {
160         statement.setString(parameterIndex,x);
161     }
162
163
164
165     public void setBytes(int parameterIndex, byte x[]) throws SQLException
166     {
167         statement.setBytes(parameterIndex,x);
168     }
169
170     public void setDate(int parameterIndex, Date x)
171             throws SQLException {
172         statement.setDate(parameterIndex,x);
173     }
174
175     public void setTime(int parameterIndex, Time x)
176             throws SQLException {
177         statement.setTime(parameterIndex,x);
178     }
179
180     public void setTimestamp(int parameterIndex, Timestamp x)
181             throws SQLException {
182         statement.setTimestamp(parameterIndex,x);
183     }
184     
185     
186
187     public void setAsciiStream(int parameterIndex, InputStream JavaDoc x, int length)
188             throws SQLException {
189         statement.setAsciiStream(parameterIndex,x,length);
190     }
191
192     public void setUnicodeStream(int parameterIndex, InputStream JavaDoc x,
193                                  int length) throws SQLException {
194         statement.setUnicodeStream(parameterIndex,x,length);
195     }
196
197     public void setBinaryStream(int parameterIndex, InputStream JavaDoc x,
198                                 int length) throws SQLException {
199         statement.setBinaryStream(parameterIndex,x,length);
200     }
201
202     public void clearParameters() throws SQLException {
203         statement.clearParameters();
204     }
205
206     public void setObject(int parameterIndex, Object JavaDoc x, int targetSqlType, int scale)
207             throws SQLException {
208         statement.setObject(parameterIndex,x,targetSqlType,scale);
209     }
210
211     public void setObject(int parameterIndex, Object JavaDoc x, int targetSqlType)
212             throws SQLException {
213         statement.setObject(parameterIndex,x,targetSqlType);
214     }
215
216     public void setObject(int parameterIndex, Object JavaDoc x) throws SQLException {
217         statement.setObject(parameterIndex,x);
218     }
219
220     public boolean execute() throws SQLException {
221         if (!pes.isFine()) return statement.execute();
222         JdbcStatementEvent ev = new JdbcStatementEvent(0, this, sql,
223             JdbcStatementEvent.STAT_EXEC);
224         pes.log(ev);
225         try {
226             boolean ans = statement.execute();
227             ev.setHasResultSet(ans);
228             return ans;
229         } catch (SQLException e) {
230             con.setNeedsValidation(true);
231             ev.setErrorMsg(e);
232             throw e;
233         } catch (RuntimeException JavaDoc e) {
234             con.setNeedsValidation(true);
235             ev.setErrorMsg(e);
236             throw e;
237         } finally {
238             ev.updateTotalMs();
239         }
240     }
241
242     public void addBatch() throws SQLException {
243         if (!pes.isFiner()) {
244             statement.addBatch();
245             return;
246         }
247         JdbcStatementEvent ev = new JdbcStatementEvent(0, this, null,
248             JdbcStatementEvent.STAT_ADD_BATCH);
249         pes.log(ev);
250         try {
251             statement.addBatch();
252         } catch (SQLException e) {
253             con.setNeedsValidation(true);
254             ev.setErrorMsg(e);
255             throw e;
256         } catch (RuntimeException JavaDoc e) {
257             con.setNeedsValidation(true);
258             ev.setErrorMsg(e);
259             throw e;
260         } finally {
261             ev.updateTotalMs();
262         }
263     }
264
265     public void setCharacterStream(int parameterIndex,
266                                    Reader JavaDoc reader,
267                                    int length) throws SQLException {
268         statement.setCharacterStream(parameterIndex,reader,length);
269     }
270
271     public void setRef(int i, Ref x) throws SQLException {
272         statement.setRef(i,x);
273     }
274
275     public void setBlob(int i, Blob x) throws SQLException {
276         statement.setBlob(i,x);
277     }
278
279     public void setClob(int i, Clob x) throws SQLException {
280         statement.setClob(i,x);
281     }
282
283     public void setArray(int i, Array x) throws SQLException {
284         statement.setArray(i,x);
285     }
286
287     public ResultSetMetaData getMetaData() throws SQLException {
288         return statement.getMetaData();
289     }
290
291     public void setDate(int parameterIndex, Date x, Calendar JavaDoc cal)
292             throws SQLException {
293         statement.setDate(parameterIndex,x,cal);
294     }
295
296     public void setTime(int parameterIndex, Time x, Calendar JavaDoc cal)
297             throws SQLException {
298         statement.setTime(parameterIndex,x,cal);
299     }
300
301     public void setTimestamp(int parameterIndex, Timestamp x, Calendar JavaDoc cal)
302             throws SQLException {
303         statement.setTimestamp(parameterIndex,x,cal);
304     }
305
306     public void setNull(int paramIndex, int sqlType, String JavaDoc typeName)
307             throws SQLException {
308         statement.setNull(paramIndex,sqlType,typeName);
309     }
310
311 }
312
313
Popular Tags