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 015package org.apache.tapestry.contrib.jdbc; 016 017import java.sql.Connection; 018import java.sql.ResultSet; 019import java.sql.SQLException; 020import java.sql.Statement; 021 022import org.apache.commons.logging.Log; 023import org.apache.commons.logging.LogFactory; 024 025/** 026 * A wrapper around {@link Statement}. 027 * 028 * @author Howard Lewis Ship 029 * 030 **/ 031 032public class SimpleStatement implements IStatement 033{ 034 private static final Log LOG = LogFactory.getLog(SimpleStatement.class); 035 036 private String _sql; 037 private Statement _statement; 038 039 public SimpleStatement(String SQL, Connection connection) throws SQLException 040 { 041 _sql = SQL; 042 _statement = connection.createStatement(); 043 } 044 045 public SimpleStatement(String SQL, Connection connection, int resultSetType, int resultSetConcurrency) 046 throws SQLException 047 { 048 _sql = SQL; 049 _statement = connection.createStatement(resultSetType, resultSetConcurrency); 050 } 051 052 /** 053 * Returns the SQL associated with this statement. 054 * 055 **/ 056 057 public String getSQL() 058 { 059 return _sql; 060 } 061 062 /** 063 * Returns the underlying {@link Statement}. 064 * 065 **/ 066 067 public Statement getStatement() 068 { 069 return _statement; 070 } 071 072 /** 073 * Closes the underlying statement, and nulls the reference to it. 074 * 075 **/ 076 077 public void close() throws SQLException 078 { 079 _statement.close(); 080 081 _statement = null; 082 _sql = null; 083 } 084 085 /** 086 * Executes the statement as a query, returning a {@link ResultSet}. 087 * 088 **/ 089 090 public ResultSet executeQuery() throws SQLException 091 { 092 if (LOG.isDebugEnabled()) 093 LOG.debug("Executing query: " + this); 094 095 return _statement.executeQuery(_sql); 096 } 097 098 /** 099 * Executes the statement as an update, returning the number of rows 100 * affected. 101 * 102 **/ 103 104 public int executeUpdate() throws SQLException 105 { 106 if (LOG.isDebugEnabled()) 107 LOG.debug("Executing update: " + this); 108 109 return _statement.executeUpdate(_sql); 110 } 111 112 public String toString() 113 { 114 StringBuffer buffer; 115 116 buffer = new StringBuffer("SimpleStatement@"); 117 buffer.append(Integer.toHexString(hashCode())); 118 119 buffer.append("[SQL=<\n"); 120 buffer.append(_sql); 121 buffer.append("\n>]"); 122 123 return buffer.toString(); 124 } 125 126}