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

📄 resultset_base.java

📁 数据仓库工具
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
    * In the JDBC 2.0 API, the behavior of method
    * <code>getObject</code> is extended to materialize  
    * data of SQL user-defined types.  When the a column contains
    * a structured or distinct value, the behavior of this method is as 
    * if it were a call to: getObject(columnIndex, 
    * this.getStatement().getConnection().getTypeMap()).
    *
    * <p>This method may also be used to read datatabase specific abstract
    * data types.
    *
    * @param columnName is the SQL name of the column
    * @return A java.lang.Object holding the column value.
    * @exception SQLException if a database-access error occurs.
    */
   public Object getObject(String columnName) throws SQLException
   {
      return getObject(findColumn(columnName));
   }


   /**
    * Get the value of a column in the current row as a Java short.
    *
    * @param columnIndex the first column is 1, the second is 2, ...
    * @return the column value; if the value is SQL NULL, the result is 0
    * @exception SQLException if a database-access error occurs.
    */
   public short getShort(int columnIndex) throws SQLException
   {
      return (short) getLong(columnIndex);
   }


   /**
    * Get the value of a column in the current row as a Java short.
    *
    * @param columnName is the SQL name of the column
    * @return the column value; if the value is SQL NULL, the result is 0
    * @exception SQLException if a database-access error occurs.
    */
   public short getShort(String columnName) throws SQLException
   {
      return getShort(findColumn(columnName));
   }


   //======================================================================
   // Methods for accessing results by column index
   //======================================================================

   /**
    * Get the value of a column in the current row as a Java String.
    *
    * @param columnIndex the first column is 1, the second is 2, ...
    * @return the column value; if the value is SQL NULL, the result is null
    * @exception SQLException if a database-access error occurs.
    */
   public String getString(int columnIndex) throws SQLException
   {
      Object  tmp = getObject(columnIndex);

      if (tmp == null)
      {
         return null;
      }
      else if (tmp instanceof byte[])
      {
         return new String((byte[])tmp);
      }
      else
      {
         return tmp.toString();
      }
   }


   //======================================================================
   // Methods for accessing results by column name
   //======================================================================

   /**
    * Get the value of a column in the current row as a Java String.
    *
    * @param columnName is the SQL name of the column
    * @return the column value; if the value is SQL NULL, the result is null
    * @exception SQLException if a database-access error occurs.
    */
   public String getString(String columnName) throws SQLException
   {
      return getString(findColumn(columnName));
   }


   /**
    * Get the value of a column in the current row as a java.sql.Time object.
    *
    * @param columnIndex the first column is 1, the second is 2, ...
    * @return the column value; if the value is SQL NULL, the result is null
    * @exception SQLException if a database-access error occurs.
    */
   public java.sql.Time getTime(int columnIndex) throws SQLException
   {
      java.sql.Time        result = null;
      java.sql.Timestamp   tmp    =  getTimestamp(columnIndex);
      
      if (tmp != null)
      {
         result = new java.sql.Time(tmp.getTime());
      }
      return result;
   }


   /**
    * Get the value of a column in the current row as a java.sql.Time object.
    *
    * @param columnName is the SQL name of the column
    * @return the column value; if the value is SQL NULL, the result is null
    * @exception SQLException if a database-access error occurs.
    */
   public java.sql.Time getTime(String columnName) throws SQLException
   {
      return getTime(findColumn(columnName));
   }


   /**
    * Get the value of a column in the current row as a java.sql.Timestamp object.
    *
    * @param columnIndex the first column is 1, the second is 2, ...
    * @return the column value; if the value is SQL NULL, the result is null
    * @exception SQLException if a database-access error occurs.
    */
   public java.sql.Timestamp getTimestamp(int columnIndex) throws SQLException
   {
      Timestamp result;

      try
      {
         Object   tmp = currentRow.getElementAt(columnIndex);

         lastGetWasNull = false;
         if (tmp == null)
         {
            lastGetWasNull = true;
            result = null;
         }
         else if (tmp instanceof Timestamp)
         {
            result = (Timestamp)tmp;
         }
         else
         {
            throw new SQLException("Can't convert column " + columnIndex
                                   + " from "
                                   + tmp.getClass().getName()
                                   + " to Timestamp");
         }
      }
      catch (TdsException e)
      {
         throw new SQLException(e.getMessage());
      }
      return result;
   }


   /**
    * Get the value of a column in the current row as a java.sql.Timestamp object.
    *
    * @param columnName is the SQL name of the column
    * @return the column value; if the value is SQL NULL, the result is null
    * @exception SQLException if a database-access error occurs.
    */
   public java.sql.Timestamp getTimestamp(String columnName) throws SQLException
   {
      return getTimestamp(findColumn(columnName));
   }


   /**
    * A column value can be retrieved as a stream of Unicode 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 Unicode.
    *
    * <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 two byte Unicode characters.  If the value is SQL NULL
    * then the result is null.
    * @exception SQLException if a database-access error occurs.
    */
   public java.io.InputStream getUnicodeStream(int columnIndex) throws SQLException
   {
       String val = getString(columnIndex);
       if (val == null)
           return null;
       try {
           return new ByteArrayInputStream(val.getBytes("UTF8"));
       } catch (UnsupportedEncodingException e) {
           // plain impossible with UTF-8
           return null;
       }
   }

   /**
    * A column value can be retrieved as a stream of Unicode 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 Unicode.
    *
    * <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 two byte Unicode characters.  If the value is SQL NULL
    * then the result is null.
    * @exception SQLException if a database-access error occurs.
    */
   public java.io.InputStream getUnicodeStream(String columnName) throws SQLException
   {
      return getUnicodeStream(findColumn(columnName));
   }


   //=====================================================================
   // Advanced features:
   //=====================================================================

   /**
    * <p>The first warning reported by calls on this ResultSet is
    * returned. Subsequent ResultSet warnings will be chained to this
    * SQLWarning.
    *
    * <P>The warning chain is automatically cleared each time a new
    * row is read.
    *
    * <P><B>Note:</B> This warning chain only covers warnings caused
    * by ResultSet methods.  Any warning caused by statement methods
    * (such as reading OUT parameters) will be chained on the
    * Statement object.
    *
    * @return the first SQLWarning or null
    * @exception SQLException if a database-access error occurs.
    */
   public SQLWarning getWarnings() throws SQLException
   {
      return warningChain.getWarnings();
   }


   /**
    * A ResultSet is initially positioned before its first row; the
    * first call to next makes the first row the current row; the
    * second call makes the second row the current row, etc.
    *
    * <P>If an input stream from the previous row is open, it is
    * implicitly closed. The ResultSet's warning chain is cleared
    * when a new row is read.
    *
    * @return true if the new current row is valid; false if there
    * are no more rows
    * @exception SQLException if a database-access error occurs.
    */
   public boolean next() throws SQLException
   {
      boolean       result      = false;
      SQLException  exception   = null;
      boolean       done        = false;
      boolean       wasCanceled = false;

      if (isClosed)
      {
         throw new SQLException("result set is closed");
      }
   if(!hitEndOfData) {
      try
      {
         clearWarnings();

         Context   context = new Context();
         context.setColumnInfo(columnsInfo);


         // Keep eating garbage and warnings until we reach the next result
         while (!tds.isResultSet()    &&
                !tds.isEndOfResults() &&
                !tds.isResultRow())
         {
            // RMK 2000-06-08: don't choke on RET_STAT package.
            if (tds.isProcId() || tds.peek() == Tds.TDS_RET_STAT_TOKEN)
            {
               tds.processSubPacket();
            }
            else if (tds.isDoneInProc())
            {
               PacketDoneInProcResult tmp =
                  (PacketDoneInProcResult)tds.processSubPacket();
            }
            else if (tds.isTextUpdate())
            {
               PacketResult tmp1 =
                  (PacketResult)tds.processSubPacket();
            }
            else if (tds.isMessagePacket() || tds.isErrorPacket())
            {
               PacketMsgResult  tmp = (PacketMsgResult)tds.processSubPacket();
               exception = warningChain.addOrReturn(tmp);
            }
            else
            {
               throw new SQLException("Protocol confusion.  "
                                      + "Got a 0x"
                                      + Integer.toHexString((tds.peek() & 0xff))
                                      + " packet");
            }
         } // end while
         
         if (exception != null)
         {
            throw exception;
         }
         
         if (tds.isResultRow())
         {
            currentRow = (PacketRowResult)tds.processSubPacket(context);
            result = true;
            done   = true;
         }
         else if (tds.isEndOfResults())
         {
            PacketResult tmp = tds.processSubPacket(context);
            currentRow   = null;
            done         = true;
            hitEndOfData = true;
            wasCanceled = wasCanceled
               || ((PacketEndTokenResult)tmp).wasCanceled();
         }
         else if (!tds.isResultSet())
         {
            throw new SQLException("Protocol confusion.  "
                                   + "Got a 0x"
                                   + Integer.toHexString((tds.peek() & 0xff))
                                   + " packet");
         }


         if (exception != null)
         {
            throw exception;
         }
      }
      catch(java.io.IOException e)
      {
         throw new SQLException(e.getMessage());
      }
      catch(TdsException e)
      {
         e.printStackTrace();
         throw new SQLException(e.getMessage());
      }
      if (wasCanceled)
      {
         throw new SQLException("Query was canceled or timed out.");
      }
      }
      return result;
   }


   /**
    * A column may have the value of SQL NULL; wasNull reports whether
    * the last column read had this special value.
    * Note that you must first call getXXX on a column to try to read
    * its value and then call wasNull() to find if the value was
    * the SQL NULL.
    *
    * @return true if last column read was SQL NULL
    * @exception SQLException if a database-access error occurs.
    */
   public boolean wasNull() throws SQLException
   {
      return lastGetWasNull;
   }
}

⌨️ 快捷键说明

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