View Javadoc

1   /*
2    * The Working-Dogs.com License, Version 1.1
3    *
4    * Copyright (c) 1999 Working-Dogs.com.  All rights reserved.
5    *
6    * Redistribution and use in source and binary forms, with or without
7    * modification, are permitted provided that the following conditions
8    * are met:
9    *
10   * 1. Redistributions of source code must retain the above copyright
11   *    notice, this list of conditions and the following disclaimer.
12   *
13   * 2. Redistributions in binary form must reproduce the above copyright
14   *    notice, this list of conditions and the following disclaimer in
15   *    the documentation and/or other materials provided with the
16   *    distribution.
17   *
18   * 3. The end-user documentation included with the redistribution, if
19   *    any, must include the following acknowlegement:
20   *       "This product includes software developed by the
21   *        Working-Dogs.com <http://www.Working-Dogs.com/>."
22   *    Alternately, this acknowlegement may appear in the software itself,
23   *    if and wherever such third-party acknowlegements normally appear.
24   *
25   * 4. The names "Working-Dogs.com" and "Village" must not be used to
26   *    endorse or promote products derived from this software without
27   *    prior written permission. For written permission, please contact
28   *    jon@working-dogs.com.
29   *
30   * 5. Products derived from this software may not be called
31   *    "Working-Dogs.com" nor may "Village" appear in their names
32   *    without prior written permission of Working-Dogs.com.
33   *
34   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
35   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
36   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
37   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
38   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
39   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
40   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
41   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
42   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
43   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
44   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45   * SUCH DAMAGE.
46   * ====================================================================
47   *
48   * This software consists of voluntary contributions made by many
49   * individuals on behalf of the Working-Dogs.com.  For more
50   * information on the Working-Dogs.com, please see
51   * <http://www.Working-Dogs.com/>.
52   */
53  package com.workingdogs.village;
54  
55  import java.sql.Connection;
56  import java.sql.ResultSet;
57  import java.sql.SQLException;
58  
59  /***
60   * This class is used for doing SQL select statements on the database. It should not be used for doing modifications via
61   * update/delete/insert statements. If you would like to perform those functions, please use a <a
62   * href="TableDataSet.html">TableDataSet</a>.
63   *
64   * <P>
65   * Here is some example code for using a QueryDataSet.
66   * <PRE>
67   *  QueryDataSet qds = new QueryDataSet ( connection, "SELECT * from my_table" );
68   *  qds.fetchRecords(10); // fetch the first 10 records
69   *  for ( int i = 0; i < qds.size(); i++ )
70   *  {
71   *  Record rec = qds.getRecord(i);
72   *  int value = rec.getValue("column").asInt();
73   *  System.out.println ( "The value is: " + value );
74   *  }
75   *  qds.close();
76   *  </PRE>
77   * It is important to always remember to close() a QueryDataSet in order to free the allocated resources.
78   * </p>
79   *
80   * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
81   * @version $Revision: 564 $
82   */
83  public class QueryDataSet
84          extends DataSet
85  {
86      /***
87       * Private...does nothing.
88       *
89       * @exception SQLException
90       * @exception DataSetException
91       */
92      public QueryDataSet()
93              throws SQLException, DataSetException
94      {
95      }
96  
97      /***
98       * Creates a new QueryDataSet based on a connection and a select string
99       *
100      * @param conn
101      * @param selectStmt
102      *
103      * @exception SQLException
104      * @exception DataSetException
105      */
106     public QueryDataSet(Connection conn, String selectStmt)
107             throws SQLException, DataSetException
108     {
109         this.conn = conn;
110 
111         selectString = new StringBuffer(selectStmt);
112         stmt = conn.createStatement();
113         resultSet = stmt.executeQuery(selectStmt);
114         schema = new Schema();
115         schema.populate(resultSet.getMetaData(), null);
116     }
117 
118     /***
119      * Create a new QueryDataSet based on an existing resultSet
120      *
121      * @param resultSet
122      *
123      * @exception SQLException
124      * @exception DataSetException
125      */
126     public QueryDataSet(ResultSet resultSet)
127             throws SQLException, DataSetException
128     {
129         this.resultSet = resultSet;
130         schema = new Schema();
131         schema.populate(resultSet.getMetaData(), null);
132     }
133 
134     /***
135      * get the Select String that was used to create this QueryDataSet
136      *
137      * @return a select string
138      */
139     public String getSelectString()
140     {
141         return this.selectString.toString();
142     }
143 }