1 15 package org.apache.tapestry.contrib.jdbc; 16 17 import java.sql.Connection ; 18 import java.sql.PreparedStatement ; 19 import java.sql.ResultSet ; 20 import java.sql.SQLException ; 21 import java.sql.Statement ; 22 import java.util.List ; 23 24 import org.apache.commons.logging.Log; 25 import org.apache.commons.logging.LogFactory; 26 27 33 34 public class ParameterizedStatement implements IStatement 35 { 36 private static final Log LOG = LogFactory.getLog(ParameterizedStatement.class); 37 38 private String _SQL; 39 private PreparedStatement _statement; 40 private IParameter[] _parameters; 41 42 50 51 public ParameterizedStatement(String SQL, Connection connection, List parameters) throws SQLException 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 63 _parameters[i].set(_statement, i + 1); 64 } 65 } 66 67 71 72 public String getSQL() 73 { 74 return _SQL; 75 } 76 77 81 82 public Statement getStatement() 83 { 84 return _statement; 85 } 86 87 91 92 public void close() throws SQLException 93 { 94 _statement.close(); 95 96 _statement = null; 97 _SQL = null; 98 } 99 100 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 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 } | Popular Tags |