View Javadoc

1   package com.workingdogs.village;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.sql.Connection;
23  import java.sql.ResultSet;
24  import java.sql.SQLException;
25  
26  /***
27   * This class is used for doing SQL select statements on the database. It should not be used for doing modifications via
28   * update/delete/insert statements. If you would like to perform those functions, please use a <a
29   * href="TableDataSet.html">TableDataSet</a>.
30   *
31   * <P>
32   * Here is some example code for using a QueryDataSet.
33   * <PRE>
34   *  QueryDataSet qds = new QueryDataSet ( connection, "SELECT * from my_table" );
35   *  qds.fetchRecords(10); // fetch the first 10 records
36   *  for ( int i = 0; i < qds.size(); i++ )
37   *  {
38   *  Record rec = qds.getRecord(i);
39   *  int value = rec.getValue("column").asInt();
40   *  System.out.println ( "The value is: " + value );
41   *  }
42   *  qds.close();
43   *  </PRE>
44   * It is important to always remember to close() a QueryDataSet in order to free the allocated resources.
45   * </p>
46   *
47   * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
48   * @version $Revision: 564 $
49   */
50  public class QueryDataSet
51          extends DataSet
52  {
53      /***
54       * Private...does nothing.
55       *
56       * @exception SQLException
57       * @exception DataSetException
58       */
59      public QueryDataSet()
60              throws SQLException, DataSetException
61      {
62      }
63  
64      /***
65       * Creates a new QueryDataSet based on a connection and a select string
66       *
67       * @param conn
68       * @param selectStmt
69       *
70       * @exception SQLException
71       * @exception DataSetException
72       */
73      public QueryDataSet(Connection conn, String selectStmt)
74              throws SQLException, DataSetException
75      {
76          this.conn = conn;
77  
78          selectString = new StringBuffer(selectStmt);
79          stmt = conn.createStatement();
80          resultSet = stmt.executeQuery(selectStmt);
81          schema = new Schema();
82          schema.populate(resultSet.getMetaData(), null);
83      }
84  
85      /***
86       * Create a new QueryDataSet based on an existing resultSet
87       *
88       * @param resultSet
89       *
90       * @exception SQLException
91       * @exception DataSetException
92       */
93      public QueryDataSet(ResultSet resultSet)
94              throws SQLException, DataSetException
95      {
96          this.resultSet = resultSet;
97          schema = new Schema();
98          schema.populate(resultSet.getMetaData(), null);
99      }
100 
101     /***
102      * get the Select String that was used to create this QueryDataSet
103      *
104      * @return a select string
105      */
106     public String getSelectString()
107     {
108         return this.selectString.toString();
109     }
110 }