home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / VCafe / prosrc.bin / ResultSetEnumeration.java < prev    next >
Encoding:
Java Source  |  1998-03-18  |  2.9 KB  |  91 lines

  1. /*
  2.  * @(#ResultSetEnumeration.java
  3.  *
  4.  * Copyright (c) 1997 Symantec Corporation. All Rights Reserved.
  5.  *
  6.  */
  7.  
  8. package symantec.itools.db.beans.jdbc;
  9.  
  10. import java.sql.*;
  11. public class ResultSetEnumeration implements java.util.Enumeration
  12. {
  13.     ResultSet m_ResultSet=null;
  14.     ResultSetMetaData m_ResultSetMD;
  15.     boolean m_RowAvailable;
  16.     Class m_RowClass;
  17.     ColumnMetaData cmd;
  18.     String tableName;
  19.  
  20.     ResultSetEnumeration(ResultSet resultSet,
  21.         ResultSetMetaData resultSetMD,
  22.         Class rowClass, String tName) {
  23.         m_ResultSet = resultSet;
  24.         m_ResultSetMD = resultSetMD;
  25.         m_RowAvailable = false;
  26.         m_RowClass = rowClass;
  27.         tableName=tName;
  28.     }
  29.  
  30.     public void closeResultSet() throws SQLException{
  31.         if(m_ResultSet!=null){
  32.             m_ResultSet.close();
  33.         }
  34.     }
  35.     
  36.     
  37.     public boolean hasMoreElements() {
  38.         if (!m_RowAvailable) {
  39.             try {
  40.                 if((m_RowAvailable = m_ResultSet.next())==false){
  41.                     m_ResultSet.close();
  42.                 }
  43.             }
  44.             catch (SQLException e) {
  45.                 // Swallow it.  All we care is did it work or didn't it.
  46.             }
  47.         }
  48.         return m_RowAvailable;
  49.     }
  50.  
  51.     public Object nextElement() {
  52.         RecordDefinition row = null;
  53.         if (!m_RowAvailable) {
  54.             m_RowAvailable = hasMoreElements();
  55.         }
  56.         if (m_RowAvailable) {
  57.             m_RowAvailable = false;
  58.             try {
  59.                 row = (RecordDefinition)m_RowClass.newInstance();
  60.                 // row.setTableName(tableName);
  61.                 row.initDataStorage();
  62.                 int numColumns = m_ResultSetMD.getColumnCount();
  63.                 Object objSwitch;
  64.                 for (int colIndex = 0; colIndex < numColumns; colIndex++) {
  65.                     try{
  66.                         objSwitch=row.getObjectFromResultSet(colIndex,m_ResultSet);
  67.                         if (m_ResultSet.wasNull()){
  68.                             objSwitch=null;
  69.                         }
  70.                         row.setValueAsObject( colIndex, objSwitch);
  71.                         ColumnMetaData cmd = (ColumnMetaData)row.getMemberModel(colIndex);
  72.                         cmd.setColumnState(ColumnMetaData.VALID_STATE);
  73.                     }
  74.                     // we can't bring up the value from the DB( the driver ??) so the
  75.                     // column is in a invalid state
  76.                     catch(SQLException sqe){
  77.                         ColumnMetaData cmd = (ColumnMetaData)row.getMemberModel(colIndex);
  78.                         cmd.setColumnState(ColumnMetaData.INVALID_STATE);
  79.                     }
  80.                         
  81.                 }
  82.                } // end for each column
  83.  
  84.             catch (Exception e) {
  85.                 System.out.println(e.getMessage());
  86.             }
  87.         }
  88.         row.bringUpToDate();
  89.         return row;
  90.     }
  91. }