www.pudn.com > town-1[1].0.4.rar > TableDataSet.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 select/insert/delete/update on the database. 
A TableDataSet cannot be used to join multiple tables for an update, if you 
need join functionality on a select, you should use a 
QueryDataSet. 

Here is an example usage for this code that gets the first 10 records where column "a" = 1:

 
KeyDef kd = new KeyDef().setAttrib("column"); 
TableDataSet tds = new TableDataSet(connection, "table_name", kd ); 
tds.where ("a=1" ); // WHERE a = 1 
tds.fetchRecords(10); // fetch first 10 records where column a=1 
for ( int i=0;i< tds.size(); i++ ) 
{ 
  Record rec = tds.getRecord(0); // zero based 
  String columnA = rec.getValue("a"); 
  if ( columnA.equals ("1") ) 
	System.out.print ("We got a column!"); 
} 
tds.close(); 
  

It is important to remember to always close() the TableDataSet when you are finished with it.

As you can see, using a TableDataSet makes doing selects from the database trivial. You do not need to write any SQL and it makes it easy to cache a TableDataSet for future use within your application. @author Jon S. Stevens jon@working-dogs.com @author Serge Knystautas sergek@lokitech.com @version 1.0 */ public class TableDataSet extends DataSet { /** the optimistic locking column value */ private String optimisticLockingCol; /** the value for the sql where clause */ private String where = null; /** the value for the sql order by clause */ private String order = null; /** the value for the sql other clause */ private String other = null; // by default this is false; private boolean refreshOnSave = false; /** the columns in the SELECT statement for this DataSet */ protected String columns; /** the table names in the SELECT statement for this DataSet */ protected String tableName; /* the select string that was used to build this DataSet */ //protected StringBuffer selectString; /** the KeyDef for this DataSet */ protected KeyDef keyDefValue; public TableDataSet (String driver, String connString, String tableName) throws ConnectionException, DataSetException { this (driver, connString, null, null, tableName, new KeyDef ()); } public TableDataSet (String driver, String connString, String tableName, KeyDef keydef) throws ConnectionException, DataSetException { this (driver, connString, null, null, tableName, keydef); } public TableDataSet (String driver, String connString, String username, String password, String tableName) throws ConnectionException, DataSetException { this (driver, connString, username, password, tableName, new KeyDef ()); } public TableDataSet (String driver, String connString, String username, String password, String tableName, KeyDef keydef) throws ConnectionException, DataSetException { super (driver, connString, username, password, keydef); this.tableName = tableName; this.keyDefValue = keydef; this.columns = "*"; this.schema = new Schema (this, tableName); } /** * Add a record * @return com.workingdogs.town.Record */ public Record addRecord () throws DataSetException, ConnectionException { if (records == null) records = new Vector (10); Record rec = new Record(this, false); rec.markForInsert(); records.addElement (rec); return rec; } /** * Use the TDS fetchRecords instead of the DataSet.fetchRecords * * @return an instance of myself * @exception ConnectionException * @exception DataSetException */ public DataSet fetchRecords() throws ConnectionException, DataSetException { return fetchRecords (-1); } /** * Use the TDS fetchRecords instead of the DataSet.fetchRecords * * @param max * @return an instance of myself * @exception ConnectionException * @exception DataSetException */ public DataSet fetchRecords(int max) throws ConnectionException, DataSetException { return fetchRecords (0, max); } /** * Fetch start to max records. start is at Record 0 * * @param start * @param max * @return an instance of myself * @exception ConnectionException * @exception DataSetException */ public DataSet fetchRecords(int start, int max) throws ConnectionException, DataSetException { //buildSelectString(); return super.fetchRecords (start, max); } /** * this is a string that contains the columns for the table * that this TableDataSet represents. * * @return columns separated by "," */ public String getColumns () { return columns; } /** * Returns the KeyDef for the DataSet * * @return a keydef */ public KeyDef getKeyDef() throws DataSetException { return keyDefValue; } /** Gets the value of the SQL portion of ORDER. @returns string */ public String getOrder() { return order; } /** Gets the value of the SQL portion of OTHER. @returns string */ public String getOther() { return this.other; } /** Used by getSelectString to build the select string that was used to populate this TableDataSet. */ protected String getSelectString () throws ConnectionException, DataSetException { //if (selectString == null) StringBuffer selectString = new StringBuffer (256); //else // selectString.setLength (0); selectString.append ("SELECT "); selectString.append (columns); selectString.append (" FROM "); selectString.append (tableName); if (this.where != null && this.where.length() > 0) selectString.append (" WHERE " + this.where); if (this.order != null && this.order.length() > 0) selectString.append (" ORDER BY " + this.order); if (this.other != null && this.other.length() > 0) selectString.append (this.other); return selectString.toString (); } /** Gets the tableName upon table data set creation @return string */ public String getTableName() throws DataSetException, ConnectionException { return tableName; } /** Gets the value of the SQL portion of WHERE. @returns string */ public String getWhere() { return where; } /** Setting this causes each Record to refresh itself when a save() is performed on it.

Default value is false. @returns true if it is on; false otherwise */ public boolean isRefreshOnSave() { return refreshOnSave; } /** Gets the table column used for optimistic locking. @returns string */ public String optimisticLockingCol() { return this.optimisticLockingCol; } /** Removes any records that are marked as a zombie. */ public void removeDeletedRecords() throws DataSetException { Enumeration enum = records.elements(); while (enum.hasMoreElements()) { Record rec = (Record) enum.nextElement(); if (rec.isAZombie()) removeRecord (rec); } } /** Saves all the records in the DataSet. @return total number of records updated/inserted/deleted */ public int save() throws ConnectionException, DataSetException { return save (connection(), false); } /** Saves all the records in the DataSet with the given connection and intransaction boolean value. @return total number of records updated/inserted/deleted */ private int save(Connection conn, boolean intransaction) throws ConnectionException, DataSetException { int j = 0; for (int i = 0; i < size(); i++) { j += findRecord(i).save(conn); } return j; } /** Saves all the records in the DataSet with the intransaction boolean value. @return total number of records updated/inserted/deleted */ public int save(boolean intransaction) throws ConnectionException, DataSetException { return save (connection(), intransaction); } /** Sets the table column used for optomistic locking. */ public void setOptimisticLockingColumn(String olc) { this.optimisticLockingCol = olc; } /** Sets the value for the SQL portion of the ORDER statement @returns instance of self */ public void setOrder(String order) throws DataSetException { this.order = order; } /** Sets the value for the SQL portion of the OTHER statement @returns instance of self */ public void setOther (String other) throws DataSetException { this.other = other; } /** Setting this causes each Record to refresh itself when a save() is performed on it.

Default value is false. @returns true if it is on; false otherwise */ public void setRefreshOnSave(boolean val) { this.refreshOnSave = val; } /** * Set the WHERE parameter in the SELECT statement * @param where java.lang.String */ public void setWhere (String where) { this.where = where; } }