www.pudn.com > town-1[1].0.4.rar > QueryDataSet.java


package com.workingdogs.town; 
 
import java.sql.*; 
import java.util.*; 
 
/* 
Town, a Java JDBC abstraction layer 
Copyright (C) 1999  Serge Knystautas, Jon S. Stevens 
 
This library is free software; you can redistribute it and/or 
modify it under the terms of the GNU Library General Public 
License as published by the Free Software Foundation; either 
version 2 of the License, or (at your option) any later version. 
 
This library is distributed in the hope that it will be useful, 
but WITHOUT ANY WARRANTY; without even the implied warranty of 
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
Library General Public License for more details. 
 
You should have received a copy of the GNU Library General Public 
License along with this library; if not, write to the 
Free Software Foundation, Inc., 59 Temple Place - Suite 330, 
Boston, MA  02111-1307, USA. 
*/ 
 
/** 
This class is used for doing SQL select statements on the database. It 
should not be used for doing modifications via update/delete/insert statements. 
If you would like to perform those functions, please use a 
TableDataSet. 

Here is some example code for using a QueryDataSet.

 
QueryDataSet qds = new QueryDataSet ( connection, "SELECT * from my_table" ); 
qds.fetchRecords(10); // fetch only the first 10 records 
for ( int i = 0; i < qds.size(); i++ ) 
{ 
  Record rec = qds.getRecord(i); 
  int value = rec.getValue("column").asInt(); 
  System.out.println ( "The value is: " + value ); 
} 
qds.close(); 
It is important to always remember to close() a QueryDataSet in order to free the allocated resources. @author Jon S. Stevens jon@working-dogs.com @author Serge Knystautas sergek@lokitech.com @version 1.0 */ public class QueryDataSet extends DataSet { protected String selectString; /** * Creates a new QueryDataSet based on a connection and a select string * * @param conn * @param selectStmt * @exception ConnectionException * @exception DataSetException */ public QueryDataSet(String driver, String connString, String selectStmt) throws ConnectionException, DataSetException { this (driver, connString, null, null, selectStmt); } /** * Creates a new QueryDataSet based on a connection and a select string * * @param conn * @param selectStmt * @exception ConnectionException * @exception DataSetException */ public QueryDataSet(String driver, String connString, String username, String password, String selectStmt) throws ConnectionException, DataSetException { super (driver, connString, username, password); try { //this.conn = conn; //if (! (selectStmt.startsWith ("select") || // selectStmt.startsWith ("SELECT"))) // throw new DataSetException ("QueryDataSet must be a SELECT string"); selectString = selectStmt; stmt = dbconn.createStatement(); resultSet = stmt.executeQuery (selectStmt); schema = new Schema(); schema.populate (resultSet.getMetaData()); } catch (SQLException sqle) { throw new ConnectionException (sqle); } } /** * This method was created in VisualAge. */ public Record addRecord () throws ConnectionException, DataSetException { throw new DataSetException ("Cannot add a record to a QueryDataSet"); } /** * Return the KeyDef for this dataset * @return com.workingdogs.town.KeyDef */ public KeyDef getKeyDef () throws DataSetException { throw new DataSetException ("QueryDataSet does not support a KeyDef for now."); } /** * get the Select String that was used to create this QueryDataSet * * @return a select string */ protected String getSelectString () throws ConnectionException, DataSetException { return selectString; } /** Gets the tableName upon table data set creation @return string */ public String getTableName() throws DataSetException, ConnectionException { throw new DataSetException ("QueryDataSet does not support table names now."); } }