View Javadoc

1   /***
2    * Copyright 2004 Steven Caswell
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package com.mungoknotwise.sqlcli;
17  
18  import java.sql.Connection;
19  import java.sql.ResultSet;
20  import java.sql.SQLException;
21  import java.sql.Statement;
22  import org.apache.log4j.Logger;
23  
24  /***
25   * Performs a select command and handles the results.
26   *
27   * @author  Steven Caswell
28   * @version $Id: SelectCommand.java,v 1.2 2004/07/28 23:24:18 mungoknotwise Exp $
29   */
30  public class SelectCommand
31    implements ResultSetExtractingCommand
32  {
33  
34    //----------------------------------------------------------------------------
35    // Static variables
36    //----------------------------------------------------------------------------
37    
38    private static Logger logger = Logger.getLogger(SelectCommand.class);
39    
40    //----------------------------------------------------------------------------
41    // Static methods
42    //----------------------------------------------------------------------------
43    
44    //----------------------------------------------------------------------------
45    // Constants
46    //----------------------------------------------------------------------------
47    
48    //----------------------------------------------------------------------------
49    // Instance variables
50    //----------------------------------------------------------------------------
51  
52    private String command;
53    private Connection connection;
54    private ExceptionHandler exceptionHandler;
55    private ResultSetExtractor resultSetExtractor;
56    
57    //----------------------------------------------------------------------------
58    // Constructors
59    //----------------------------------------------------------------------------
60    
61    /***
62     * Constructs a new instance of
63     * <code>SelectCommand</code>.
64     */
65    public SelectCommand()
66    {
67    }
68  
69    //----------------------------------------------------------------------------
70    // Interface implementations
71    //----------------------------------------------------------------------------
72    //----------------------------------------------------------------------------
73    // Implementation of interface Interface1
74    //----------------------------------------------------------------------------
75    
76    //----------------------------------------------------------------------------
77    // Extends overrides
78    //----------------------------------------------------------------------------
79    //----------------------------------------------------------------------------
80    // Override of class Class1
81    //----------------------------------------------------------------------------
82    
83    
84    //----------------------------------------------------------------------------
85    // Public methods exposed by this class
86    //----------------------------------------------------------------------------
87  
88    /***
89     * {@inheritDoc}
90     */
91    public void execute()
92    {
93      try
94      {
95        Statement statement = this.connection.createStatement();
96        ResultSet resultSet = statement.executeQuery(this.command);
97        this.resultSetExtractor.extractData(resultSet);
98      }
99      catch(SQLException e)
100     {
101       this.exceptionHandler.handleException(e);
102     }
103   }
104 
105   /***
106    * Sets the SQL select command for this command to execute.
107    *
108    * @param command the command
109    */
110   public void setCommand(final String command)
111   {
112     this.command = command;
113   }
114 
115   /***
116    * Sets the connection for this command.
117    *
118    * @param connection the connection
119    */
120   public void setConnection(final Connection connection)
121   {
122     this.connection = connection;
123   }
124 
125   /***
126    * {@inheritDoc}
127    */
128   public void setExceptionHandler(final ExceptionHandler exceptionHandler)
129   {
130     this.exceptionHandler = exceptionHandler;
131   }
132 
133   /***
134    * {@inheritDoc}
135    */
136   public void setResultSetExtractor(final ResultSetExtractor resultSetExtractor)
137   {
138     this.resultSetExtractor = resultSetExtractor;
139   }
140   
141   //----------------------------------------------------------------------------
142   // Protected abstract methods
143   //----------------------------------------------------------------------------
144   
145   //----------------------------------------------------------------------------
146   // Protected methods for use by subclasses
147   //----------------------------------------------------------------------------
148   
149   //----------------------------------------------------------------------------
150   // Other methods
151   //----------------------------------------------------------------------------
152   
153   //----------------------------------------------------------------------------
154   // Member classes
155   //----------------------------------------------------------------------------
156   
157 }