001 // Copyright 2004, 2005 The Apache Software Foundation
002 //
003 // Licensed under the Apache License, Version 2.0 (the "License");
004 // you may not use this file except in compliance with the License.
005 // You may obtain a copy of the License at
006 //
007 // http://www.apache.org/licenses/LICENSE-2.0
008 //
009 // Unless required by applicable law or agreed to in writing, software
010 // distributed under the License is distributed on an "AS IS" BASIS,
011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012 // See the License for the specific language governing permissions and
013 // limitations under the License.
014
015 package org.apache.tapestry.contrib.jdbc;
016
017 import java.sql.Connection;
018 import java.sql.PreparedStatement;
019 import java.sql.ResultSet;
020 import java.sql.SQLException;
021 import java.sql.Statement;
022 import java.util.List;
023
024 import org.apache.commons.logging.Log;
025 import org.apache.commons.logging.LogFactory;
026
027 /**
028 * A wrapper around {@link PreparedStatement}.
029 *
030 * @author Howard Lewis Ship
031 *
032 **/
033
034 public class ParameterizedStatement implements IStatement
035 {
036 private static final Log LOG = LogFactory.getLog(ParameterizedStatement.class);
037
038 private String _sql;
039 private PreparedStatement _statement;
040 private IParameter[] _parameters;
041
042 /**
043 * Create a new instance; the parameters list is copied.
044 *
045 * @param SQL the SQL to execute (see {@link Connection#prepareStatement(java.lang.String)})
046 * @param connection the JDBC connection to use
047 * @param parameters list of {@link IParameter}
048 *
049 **/
050
051 public ParameterizedStatement(String SQL, Connection connection, List parameters) throws SQLException
052 {
053 _sql = SQL;
054
055 _statement = connection.prepareStatement(SQL);
056
057 _parameters = (IParameter[]) parameters.toArray(new IParameter[parameters.size()]);
058
059 for (int i = 0; i < _parameters.length; i++)
060 {
061 // JDBC numbers things from 1, not 0.
062
063 _parameters[i].set(_statement, i + 1);
064 }
065 }
066
067 /**
068 * Returns the SQL associated with this statement.
069 *
070 **/
071
072 public String getSQL()
073 {
074 return _sql;
075 }
076
077 /**
078 * Returns the underlying or {@link PreparedStatement}.
079 *
080 **/
081
082 public Statement getStatement()
083 {
084 return _statement;
085 }
086
087 /**
088 * Closes the underlying statement, and nulls the reference to it.
089 *
090 **/
091
092 public void close() throws SQLException
093 {
094 _statement.close();
095
096 _statement = null;
097 _sql = null;
098 }
099
100 /**
101 * Executes the statement as a query, returning a {@link ResultSet}.
102 *
103 **/
104
105 public ResultSet executeQuery() throws SQLException
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
120 {
121 if (LOG.isDebugEnabled())
122 LOG.debug("Executing update: " + this);
123
124 return _statement.executeUpdate();
125 }
126
127 public String toString()
128 {
129 StringBuffer buffer = new StringBuffer("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 }