KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mockobjects > eziba > sql > PreparedStatement


1 /*
2  * Copyright (C) 2001 eZiba.com, Inc.
3  * All Rights Reserved
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * Redistributions in binary form must reproduce the above
12  * copyright notice, this list of conditions and the following
13  * disclaimer in the documentation and/or other materials provided
14  * with the distribution. Neither the name of eZiba.com nor the
15  * names of its contributors may be used to endorse or promote
16  * products derived from this software without specific prior
17  * written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23  * eZiba.com OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
30  * OF THE POSSIBILITY OF SUCH DAMAGE.
31  */

32
33 package com.mockobjects.eziba.sql;
34
35 import java.io.ByteArrayOutputStream JavaDoc;
36 import java.io.IOException JavaDoc;
37 import java.io.InputStream JavaDoc;
38 import java.io.Reader JavaDoc;
39 import java.math.BigDecimal JavaDoc;
40 import java.sql.SQLException JavaDoc;
41 import java.util.Calendar JavaDoc;
42 import java.util.Vector JavaDoc;
43
44 public class PreparedStatement
45     extends Statement
46     implements java.sql.PreparedStatement JavaDoc {
47
48     protected final Vector JavaDoc m_args = new Vector JavaDoc();
49
50     protected void setArg(int p_pos, Object JavaDoc p_arg) {
51         p_pos--;
52         if(m_args.size() <= p_pos) {
53             int size = m_args.size();
54             m_args.setSize(p_pos + 1);
55             for(int i = size; i < m_args.size(); ++i) {
56                 m_args.set(i, Connection.WILDCARD);
57             }
58         }
59         m_args.set(p_pos, p_arg);
60     }
61
62     protected final Object JavaDoc[] getArguments() {
63         m_args.trimToSize();
64         return m_args.toArray();
65     }
66
67     PreparedStatement(Connection p_connection, String JavaDoc p_sql) {
68         super(p_connection, p_sql);
69     }
70
71     public void addBatch() {
72         throw new NotImplementedException();
73     }
74
75     public void clearParameters() {
76         throw new NotImplementedException();
77     }
78
79     public boolean execute()
80         throws SQLException JavaDoc {
81         m_returnValue = -1;
82         m_resultSet = null;
83         try {
84             m_returnValue = executeUpdate();
85             return false;
86         } catch(SQLException JavaDoc e) {
87             m_resultSet = executeQuery();
88             return true;
89         }
90     }
91
92     public java.sql.ResultSet JavaDoc getResultSet() {
93         return m_resultSet;
94     }
95
96     public int getUpdateCount() {
97         return m_returnValue;
98     }
99
100     public java.sql.ResultSet JavaDoc executeQuery()
101         throws SQLException JavaDoc {
102         return connection.getRegisteredResult(sql, getArguments());
103     }
104
105     public int executeUpdate()
106         throws SQLException JavaDoc {
107         return connection.getRegisteredUpdate(sql, getArguments());
108     }
109
110     public void setInt(int parameterIndex, int x) {
111         setArg(parameterIndex, new Integer JavaDoc(x));
112     }
113
114     public void setString(int parameterIndex, String JavaDoc x) {
115         setArg(parameterIndex, x);
116     }
117
118     public void setBigDecimal(int parameterIndex, BigDecimal JavaDoc x) {
119         setArg(parameterIndex, x);
120     }
121
122     public void setBinaryStream(int parameterIndex, InputStream JavaDoc x, int length)
123         throws SQLException JavaDoc {
124         try {
125             ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
126             int c = 0;
127             while(((c = x.read()) != -1)) {
128                 baos.write(c);
129             }
130             setArg(parameterIndex, baos.toByteArray());
131         } catch(IOException JavaDoc e) {
132             throw new SQLException JavaDoc("IOException reading stream");
133         }
134     }
135
136     public void setNull(int parameterIndex, int sqlType) {
137         setArg(parameterIndex, null);
138     }
139
140     private int m_returnValue;
141     private java.sql.ResultSet JavaDoc m_resultSet;
142
143     // be afraid, be very afraid, for below this line
144
// all methods throw the dreaded NotImplementedException ...
145

146
147
148     public java.sql.ResultSetMetaData JavaDoc getMetaData() {
149         throw new NotImplementedException();
150     }
151
152     public void setArray(int i, java.sql.Array JavaDoc x) {
153         throw new NotImplementedException();
154     }
155
156     public void setAsciiStream(int parameterIndex, InputStream JavaDoc x, int length) {
157         throw new NotImplementedException();
158     }
159
160     public void setBlob(int i, java.sql.Blob JavaDoc x) {
161         throw new NotImplementedException();
162     }
163
164     public void setBoolean(int parameterIndex, boolean x) {
165         throw new NotImplementedException();
166     }
167
168     public void setByte(int parameterIndex, byte x) {
169         throw new NotImplementedException();
170     }
171
172     public void setBytes(int parameterIndex, byte[] x) {
173         throw new NotImplementedException();
174     }
175
176     public void setCharacterStream(int parameterIndex, Reader JavaDoc reader, int length) {
177         throw new NotImplementedException();
178     }
179
180     public void setClob(int i, java.sql.Clob JavaDoc x) {
181         throw new NotImplementedException();
182     }
183
184     public void setDate(int parameterIndex, java.sql.Date JavaDoc x) {
185         throw new NotImplementedException();
186     }
187
188     public void setDate(int parameterIndex, java.sql.Date JavaDoc x, Calendar JavaDoc cal) {
189         throw new NotImplementedException();
190     }
191
192     public void setDouble(int parameterIndex, double x) {
193         throw new NotImplementedException();
194     }
195
196     public void setFloat(int parameterIndex, float x) {
197         throw new NotImplementedException();
198     }
199
200     public void setLong(int parameterIndex, long x) {
201         throw new NotImplementedException();
202     }
203
204     public void setNull(int paramIndex, int sqlType, String JavaDoc typeName) {
205         throw new NotImplementedException();
206     }
207
208     public void setObject(int parameterIndex, Object JavaDoc x) {
209         throw new NotImplementedException();
210     }
211
212     public void setObject(int parameterIndex, Object JavaDoc x, int targetSqlType) {
213         throw new NotImplementedException();
214     }
215
216     public void setObject(int parameterIndex, Object JavaDoc x, int targetSqlType, int scale) {
217         throw new NotImplementedException();
218     }
219
220     public void setRef(int i, java.sql.Ref JavaDoc x) {
221         throw new NotImplementedException();
222     }
223
224     public void setShort(int parameterIndex, short x) {
225         throw new NotImplementedException();
226     }
227
228     public void setTime(int parameterIndex, java.sql.Time JavaDoc x) {
229         throw new NotImplementedException();
230     }
231
232     public void setTime(int parameterIndex, java.sql.Time JavaDoc x, Calendar JavaDoc cal) {
233         throw new NotImplementedException();
234     }
235
236     public void setTimestamp(int parameterIndex, java.sql.Timestamp JavaDoc x) {
237         throw new NotImplementedException();
238     }
239
240     public void setTimestamp(int parameterIndex, java.sql.Timestamp JavaDoc x, Calendar JavaDoc cal) {
241         throw new NotImplementedException();
242     }
243
244     /**
245      * @deprecated
246      */

247     public void setUnicodeStream(int parameterIndex, InputStream JavaDoc x, int length) {
248         throw new NotImplementedException();
249     }
250 }
Popular Tags