⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 resultset_base.java

📁 数据仓库工具
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
//
// Copyright 1998, 1999 CDS Networks, Inc., Medford Oregon
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
// 1. Redistributions of source code must retain the above copyright
//    notice, this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright
//    notice, this list of conditions and the following disclaimer in the
//    documentation and/or other materials provided with the distribution.
// 3. All advertising materials mentioning features or use of this software
//    must display the following acknowledgement:
//      This product includes software developed by CDS Networks, Inc.
// 4. The name of CDS Networks, Inc.  may not be used to endorse or promote
//    products derived from this software without specific prior
//    written permission.
//
// THIS SOFTWARE IS PROVIDED BY CDS NETWORKS, INC. ``AS IS'' AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED.  IN NO EVENT SHALL CDS NETWORKS, INC. BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
// OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
// OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
// SUCH DAMAGE.
//


package com.internetcds.jdbc.tds;

import java.sql.*;
import java.math.BigDecimal;
import java.util.Vector;
// import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Calendar;
import java.io.*;

/**
 * <P>A ResultSet provides access to a table of data generated by
 * executing a Statement. The table rows are retrieved in
 * sequence. Within a row its column values can be accessed in any
 * order.
 *
 * <P>A ResultSet maintains a cursor pointing to its current row of
 * data.  Initially the cursor is positioned before the first row.
 * The 'next' method moves the cursor to the next row.
 *
 * <P>The getXXX methods retrieve column values for the current
 * row.  You can retrieve values either using the index number of the
 * column, or by using the name of the column.  In general using the
 * column index will be more efficient.  Columns are numbered from 1.
 *
 * <P>For maximum portability, ResultSet columns within each row should be
 * read in left-to-right order and each column should be read only once.
 *
 * <P>For the getXXX methods, the JDBC driver attempts to convert the
 * underlying data to the specified Java type and returns a suitable
 * Java value.  See the JDBC specification for allowable mappings
 * from SQL types to Java types with the ResultSet.getXXX methods.
 *
 * <P>Column names used as input to getXXX methods are case
 * insensitive.  When performing a getXXX using a column name, if
 * several columns have the same name, then the value of the first
 * matching column will be returned. The column name option is
 * designed to be used when column names are used in the SQL
 * query. For columns that are NOT explicitly named in the query, it
 * is best to use column numbers. If column names were used there is
 * no way for the programmer to guarantee that they actually refer to
 * the intended columns.
 *
 * <P>A ResultSet is automatically closed by the Statement that
 * generated it when that Statement is closed, re-executed, or is used
 * to retrieve the next result from a sequence of multiple results.
 *
 * <P>The number, types and properties of a ResultSet's columns are
 * provided by the ResulSetMetaData object returned by the getMetaData
 * method.
 *
 * @author Craig Spannring
 * @author The FreeTDS project
 * @version  $Id: ResultSet_base.java,v 1.1 2003/04/29 18:07:50 sinisa Exp $
 *
 * @see Statement#executeQuery
 * @see Statement#getResultSet
 * @see ResultSetMetaData
 @ @see Tds#getRow
 */

public class ResultSet_base 
{
   public static final String cvsVersion = "$Id: ResultSet_base.java,v 1.1 2003/04/29 18:07:50 sinisa Exp $";


   Tds               tds            = null;
   Statement         stmt           = null;
   Columns           columnsInfo    = null;
   ResultSetMetaData metaData       = null;
   PacketRowResult   currentRow     = null;
   boolean           lastGetWasNull = false;

   boolean           hitEndOfData   = false;
   boolean           isClosed       = false;

   private SQLWarningChain warningChain = null;   // The warnings chain.
   
   public ResultSet_base(Tds tds_, Statement stmt_, Columns columns_)
   {
      tds         = tds_;
      stmt        = stmt_;
      columnsInfo = columns_;

      hitEndOfData = false;
      warningChain = new SQLWarningChain();
   }


   protected void NotImplemented() throws java.sql.SQLException
      {
         throw new SQLException("Not implemented");
      }


   /**
    * After this call getWarnings returns null until a new warning is
    * reported for this ResultSet.
    *
    * @exception SQLException if a database-access error occurs.
    */
   public void clearWarnings() throws SQLException
   {
      warningChain.clearWarnings();
   }

   /**
    * In some cases, it is desirable to immediately release a
    * ResultSet's database and JDBC resources instead of waiting for
    * this to happen when it is automatically closed; the close
    * method provides this immediate release.
    *
    * <P><B>Note:</B> A ResultSet is automatically closed by the
    * Statement that generated it when that Statement is closed,
    * re-executed, or is used to retrieve the next result from a
    * sequence of multiple results. A ResultSet is also automatically
    * closed when it is garbage collected.
    *
    * @exception SQLException if a database-access error occurs.
    */
   public void close() throws SQLException
   {
      Exception   exception = null;
      
      if (isClosed)
      {
         // nop ???
      }
      else
      {
         isClosed = true;
         try
         {
            if (!hitEndOfData)
            {
               tds.discardResultSet(columnsInfo);
               hitEndOfData = true;
            }
            else
            {
               // nop
            }
         }
         catch(com.internetcds.jdbc.tds.TdsException e)
         {
             e.printStackTrace();
            exception = e;
      }
         catch(java.io.IOException e)
         {
             e.printStackTrace();
            exception = e;
         }
         
         currentRow  = null;
         metaData    = null;
         columnsInfo = null;
         stmt        = null;
         
         if (exception != null)
         {
            throw new SQLException(exception.getMessage());
         }
      }
   }


   //----------------------------------------------------------------

   /**
    * Map a Resultset column name to a ResultSet column index.
    *
    * @param columnName the name of the column
    * @return the column index
    * @exception SQLException if a database-access error occurs.
    */
   public int findColumn(String columnName) throws SQLException
   {
      int   i;

      for(i=1; i<=columnsInfo.getColumnCount(); i++)
      {
         if (columnsInfo.getName(i).equalsIgnoreCase(columnName))
         {
            return i;
         }
         // XXX also need to look at the fully qualified name ie. table.column
      }
      throw new SQLException("No such column " + columnName);
   }


   /**
    * A column value can be retrieved as a stream of ASCII characters
    * and then read in chunks from the stream.  This method is particularly
    * suitable for retrieving large LONGVARCHAR values.  The JDBC driver will
    * do any necessary conversion from the database format into ASCII.
    *
    * <P><B>Note:</B> All the data in the returned stream must be
    * read prior to getting the value of any other column. The next
    * call to a get method implicitly closes the stream. . Also, a
    * stream may return 0 for available() whether there is data
    * available or not.
    *
    * @param columnIndex the first column is 1, the second is 2, ...
    * @return a Java input stream that delivers the database column value
    * as a stream of one byte ASCII characters.  If the value is SQL NULL
    * then the result is null.
    * @exception SQLException if a database-access error occurs.
    */
   public java.io.InputStream getAsciiStream(int columnIndex) throws SQLException
   {
       String val = getString(columnIndex);
       if (val == null)
           return null;
       try {
           return new ByteArrayInputStream(val.getBytes("ASCII"));
       } catch (UnsupportedEncodingException ue) {
           // plain impossible with encoding ASCII
           return null;
       }
   }


   /**
    * A column value can be retrieved as a stream of ASCII characters
    * and then read in chunks from the stream.  This method is particularly
    * suitable for retrieving large LONGVARCHAR values.  The JDBC driver will
    * do any necessary conversion from the database format into ASCII.
    *
    * <P><B>Note:</B> All the data in the returned stream must
    * be read prior to getting the value of any other column. The
    * next call to a get method implicitly closes the stream.
    *
    * @param columnName is the SQL name of the column
    * @return a Java input stream that delivers the database column value
    * as a stream of one byte ASCII characters.  If the value is SQL NULL
    * then the result is null.
    * @exception SQLException if a database-access error occurs.
    */
   public java.io.InputStream getAsciiStream(String columnName) throws SQLException
   {
      return getAsciiStream(findColumn(columnName));
   }


   /**
    * Get the value of a column in the current row as a
    * java.lang.BigDecimal object.
    *
    * @param columnIndex the first column is 1, the second is 2, ...
    * @param scale the number of digits to the right of the decimal
    * @return the column value; if the value is SQL NULL, the result is null
    * @exception SQLException if a database-access error occurs.
    */
   public BigDecimal getBigDecimal(int columnIndex, int scale)
      throws SQLException
   {
      Object      tmp    = getObject(columnIndex);
      BigDecimal  result = null;


      if (tmp == null)
      {
         result = null;
      }
      else if (tmp instanceof java.lang.Double)
      {
         result = new BigDecimal(((Double)tmp).doubleValue());
         result = result.setScale(scale, BigDecimal.ROUND_HALF_UP);
      }
      else if (tmp instanceof java.lang.Float)
      {
         result = new BigDecimal(((Float)tmp).doubleValue());
         result = result.setScale(scale, BigDecimal.ROUND_HALF_UP);
      }
      else if (tmp instanceof java.lang.Number)
      {
         // This handles Byte, Short, Integer, and Long
         result = BigDecimal.valueOf(((Number)tmp).longValue(), scale);
      }
      else if (tmp instanceof BigDecimal)
      {
         result = (BigDecimal)tmp;
      }
      else if (tmp instanceof java.lang.String)
      {
         try
         {
            result = new BigDecimal((String)tmp);
         }
         catch (NumberFormatException e)
         {
            throw new SQLException(e.getMessage());
         }
      }
      return result;
   }

   /**
    * Get the value of a column in the current row as a
    * java.lang.BigDecimal object.
    *
    * @param columnName is the SQL name of the column
    * @param scale the number of digits to the right of the decimal
    * @return the column value; if the value is SQL NULL, the result is null
    * @exception SQLException if a database-access error occurs.
    */
   public BigDecimal getBigDecimal(String columnName, int scale) throws SQLException
   {
      return getBigDecimal(findColumn(columnName), scale);
   }


   /**
    * A column value can be retrieved as a stream of uninterpreted bytes
    * and then read in chunks from the stream.  This method is particularly
    * suitable for retrieving large LONGVARBINARY values.
    *
    * <P><B>Note:</B> All the data in the returned stream must be
    * read prior to getting the value of any other column. The next
    * call to a get method implicitly closes the stream. Also, a
    * stream may return 0 for available() whether there is data
    * available or not.
    *
    * @param columnIndex the first column is 1, the second is 2, ...
    * @return a Java input stream that delivers the database column value
    * as a stream of uninterpreted bytes.  If the value is SQL NULL
    * then the result is null.
    * @exception SQLException if a database-access error occurs.
    */
   public java.io.InputStream getBinaryStream(int columnIndex)
      throws SQLException
   {
       byte[] bytes = getBytes(columnIndex);
       if (bytes != null)
           return new ByteArrayInputStream(bytes);
       return null;
   }


   /**
    * A column value can be retrieved as a stream of uninterpreted bytes
    * and then read in chunks from the stream.  This method is particularly
    * suitable for retrieving large LONGVARBINARY values.
    *
    * <P><B>Note:</B> All the data in the returned stream must
    * be read prior to getting the value of any other column. The
    * next call to a get method implicitly closes the stream.
    *
    * @param columnName is the SQL name of the column
    * @return a Java input stream that delivers the database column value
    * as a stream of uninterpreted bytes.  If the value is SQL NULL
    * then the result is null.
    * @exception SQLException if a database-access error occurs.
    */
   public java.io.InputStream getBinaryStream(String columnName)
      throws SQLException
   {
      return getBinaryStream(findColumn(columnName));
   }


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -