001: package com.workingdogs.village;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import java.sql.Connection;
023: import java.sql.ResultSet;
024: import java.sql.SQLException;
025:
026: /**
027: * This class is used for doing SQL select statements on the database. It should not be used for doing modifications via
028: * update/delete/insert statements. If you would like to perform those functions, please use a <a
029: * href="TableDataSet.html">TableDataSet</a>.
030: *
031: * <P>
032: * Here is some example code for using a QueryDataSet.
033: * <PRE>
034: * QueryDataSet qds = new QueryDataSet ( connection, "SELECT * from my_table" );
035: * qds.fetchRecords(10); // fetch the first 10 records
036: * for ( int i = 0; i < qds.size(); i++ )
037: * {
038: * Record rec = qds.getRecord(i);
039: * int value = rec.getValue("column").asInt();
040: * System.out.println ( "The value is: " + value );
041: * }
042: * qds.close();
043: * </PRE>
044: * It is important to always remember to close() a QueryDataSet in order to free the allocated resources.
045: * </p>
046: *
047: * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
048: * @version $Revision: 564 $
049: */
050: public class QueryDataSet extends DataSet {
051: /**
052: * Private...does nothing.
053: *
054: * @exception SQLException
055: * @exception DataSetException
056: */
057: public QueryDataSet() throws SQLException, DataSetException {
058: }
059:
060: /**
061: * Creates a new QueryDataSet based on a connection and a select string
062: *
063: * @param conn
064: * @param selectStmt
065: *
066: * @exception SQLException
067: * @exception DataSetException
068: */
069: public QueryDataSet(Connection conn, String selectStmt)
070: throws SQLException, DataSetException {
071: this .conn = conn;
072:
073: selectString = new StringBuffer(selectStmt);
074: stmt = conn.createStatement();
075: resultSet = stmt.executeQuery(selectStmt);
076: schema = new Schema();
077: schema.populate(resultSet.getMetaData(), null);
078: }
079:
080: /**
081: * Create a new QueryDataSet based on an existing resultSet
082: *
083: * @param resultSet
084: *
085: * @exception SQLException
086: * @exception DataSetException
087: */
088: public QueryDataSet(ResultSet resultSet) throws SQLException,
089: DataSetException {
090: this .resultSet = resultSet;
091: schema = new Schema();
092: schema.populate(resultSet.getMetaData(), null);
093: }
094:
095: /**
096: * get the Select String that was used to create this QueryDataSet
097: *
098: * @return a select string
099: */
100: public String getSelectString() {
101: return this.selectString.toString();
102: }
103: }
|