KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > contrib > jdbc > ParameterizedStatement


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.contrib.jdbc;
16
17 import java.sql.Connection JavaDoc;
18 import java.sql.PreparedStatement JavaDoc;
19 import java.sql.ResultSet JavaDoc;
20 import java.sql.SQLException JavaDoc;
21 import java.sql.Statement JavaDoc;
22 import java.util.List JavaDoc;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26
27 /**
28  * A wrapper around {@link PreparedStatement}.
29  *
30  * @author Howard Lewis Ship
31  *
32  **/

33
34 public class ParameterizedStatement implements IStatement
35 {
36     private static final Log LOG = LogFactory.getLog(ParameterizedStatement.class);
37
38     private String JavaDoc _SQL;
39     private PreparedStatement JavaDoc _statement;
40     private IParameter[] _parameters;
41
42     /**
43      * Create a new instance; the parameters list is copied.
44      *
45      * @param SQL the SQL to execute (see {@link Connection#prepareStatement(java.lang.String)})
46      * @param connection the JDBC connection to use
47      * @param parameters list of {@link IParameter}
48      *
49      **/

50     
51     public ParameterizedStatement(String JavaDoc SQL, Connection JavaDoc connection, List JavaDoc parameters) throws SQLException JavaDoc
52     {
53         _SQL = SQL;
54
55         _statement = connection.prepareStatement(SQL);
56
57         _parameters = (IParameter[]) parameters.toArray(new IParameter[parameters.size()]);
58
59         for (int i = 0; i < _parameters.length; i++)
60         {
61             // JDBC numbers things from 1, not 0.
62

63             _parameters[i].set(_statement, i + 1);
64         }
65     }
66
67     /**
68      * Returns the SQL associated with this statement.
69      *
70      **/

71
72     public String JavaDoc getSQL()
73     {
74         return _SQL;
75     }
76
77     /**
78      * Returns the underlying or {@link PreparedStatement}.
79      *
80      **/

81
82     public Statement JavaDoc getStatement()
83     {
84         return _statement;
85     }
86
87     /**
88      * Closes the underlying statement, and nulls the reference to it.
89      *
90      **/

91
92     public void close() throws SQLException JavaDoc
93     {
94         _statement.close();
95
96         _statement = null;
97         _SQL = null;
98     }
99
100     /**
101      * Executes the statement as a query, returning a {@link ResultSet}.
102      *
103      **/

104
105     public ResultSet JavaDoc executeQuery() throws SQLException JavaDoc
106     {
107         if (LOG.isDebugEnabled())
108             LOG.debug("Executing query: " + this);
109
110         return _statement.executeQuery();
111     }
112
113     /**
114      * Executes the statement as an update, returning the number of rows
115      * affected.
116      *
117      **/

118
119     public int executeUpdate() throws SQLException JavaDoc
120     {
121         if (LOG.isDebugEnabled())
122             LOG.debug("Executing update: " + this);
123
124         return _statement.executeUpdate();
125     }
126
127     public String JavaDoc toString()
128     {
129         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc("ParameterizedStatement@");
130         buffer.append(Integer.toHexString(hashCode()));
131         buffer.append("[SQL=\n<");
132         buffer.append(_SQL);
133         buffer.append("\n>");
134
135         for (int i = 0; i < _parameters.length; i++)
136         {
137             IParameter parameter = _parameters[i];
138
139             buffer.append(" ?");
140             buffer.append(i + 1);
141             buffer.append('=');
142
143             buffer.append(parameter);
144         }
145
146         buffer.append(']');
147
148         return buffer.toString();
149     }
150
151 }
Popular Tags