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

📄 resultset_base.java

📁 数据仓库工具
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
   }


   /**
    * Get the value of a column in the current row as a Java long.
    *
    * @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 long getLong(int columnIndex) throws SQLException
   {
      long    result = 0;
      Object  obj    = getObject(columnIndex);

      if (obj == null)
      {
         result = 0;
      }
      else
      {
         try
         {
            switch(getMetaData().getColumnType(columnIndex))
            {
               case java.sql.Types.TINYINT:
               case java.sql.Types.SMALLINT:
               case java.sql.Types.INTEGER:
               {
                  result = ((Number)obj).longValue();
                  break;
               }
               case java.sql.Types.BIGINT:
               {
                  result = ((Number)obj).longValue();
                  break;
               }
               case java.sql.Types.REAL:
               case java.sql.Types.FLOAT:
               case java.sql.Types.DOUBLE:
               {
                  result = ((Number)obj).longValue();
                  break;
               }
               case java.sql.Types.CHAR:
               case java.sql.Types.VARCHAR:
               case java.sql.Types.LONGVARCHAR:
               {
                  try
                  {
                     Long i   = new Long((String)obj);
                     result   = i.longValue();
                  }
                  catch (NumberFormatException e)
                  {
                     throw new SQLException(e.getMessage());
                  }
                  break;
               }
               case java.sql.Types.NUMERIC:
               {
                  result = ((Number)obj).longValue();
                  break;
               }
               case java.sql.Types.DECIMAL:
               {
                  result = ((Number)obj).longValue();
                  break;
               }
               case java.sql.Types.BIT:
               {
                  // XXX according to JDBC spec we need to handle these
                  // for now just fall through
               }
               default:
               {
                  throw new SQLException("Internal error. "
                                         + "Don't know how to convert from "
                                         + "java.sql.Types " +
                                         TdsUtil.javaSqlTypeToString(getMetaData().getColumnType(columnIndex))
                                         + " to an long");
               }
            }
         }
         catch(ClassCastException e)
         {
            throw new SQLException("Couldn't convert column " + columnIndex
                                   + " to an long.  "
                                   + e.getMessage());
         }
      }
      return result;
   } /* getLong()  */


   /**
    * Get the value of a column in the current row as a Java long.
    *
    * @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 long getLong(String columnName) throws SQLException
   {
      return getLong(findColumn(columnName));
   }


   /**
    * The number, types and properties of a ResultSet's columns
    * are provided by the getMetaData method.
    *
    * @return the description of a ResultSet's columns
    * @exception SQLException if a database-access error occurs.
    */
   public java.sql.ResultSetMetaData getMetaData() throws SQLException
   {
      if (metaData == null)
      {
         metaData = new ResultSetMetaData(columnsInfo);
      }
      return metaData;
   }


   /**
    * <p>Get the value of a column in the current row as a Java object.
    *
    * <p>This method will return the value of the given column as a
    * Java object.  The type of the Java object will be the default
    * Java Object type corresponding to the column's SQL type,
    * following the mapping specified in the JDBC spec.
    *
    * <p>This method may also be used to read datatabase specific abstract
    * data types.
    *
    * JDBC 2.0
    *
    * 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()).
    *
    * @param columnIndex the first column is 1, the second is 2, ...
    * @return A java.lang.Object holding the column value.
    * @exception SQLException if a database-access error occurs.
    */
   public Object getObject(int columnIndex) throws SQLException
   {
      // This method is implicitly coupled to the getRow() method in the
      // Tds class.  Every type that getRow() could return must
      // be handled in this method.
      //
      // The object type returned by getRow() must correspond with the
      // jdbc SQL type in the switch statement below.
      //
      // Note-  The JDBC spec (version 1.20) does not define the type
      // of the Object returned for LONGVARCHAR data.

      // XXX-  Needs modifications for JDBC 2.0
		
      Object   result = null;

      if (currentRow == null)
      {
         throw new SQLException("No current row in the result set.  " +
                                "Did you call ResultSet.next()?");
      }
      
      try
      {
         Object   tmp    = currentRow.getElementAt(columnIndex);
         lastGetWasNull = false;
         if (tmp == null)
         {
            lastGetWasNull = true;

            result = null;
         }
         else
         {
            switch(getMetaData().getColumnType(columnIndex))
            {
               case java.sql.Types.CHAR:
               case java.sql.Types.VARCHAR:
               {
                  if (tmp instanceof String)
                  {
                     result = tmp;
                  }
                  else
                  {
                     throw new SQLException("Was expecting CHAR data.  Got"
                                            + tmp.getClass().getName());
                  }
                  break;
               }
               case java.sql.Types.TINYINT:
               {
                  if (! (tmp instanceof Long))
                  {
                     throw new SQLException("Internal error");
                  }

                  result = new Byte((byte) ((Long)tmp).intValue());
                  break;
               }
               case java.sql.Types.SMALLINT:
               {
                  if (! (tmp instanceof Long))
                  {
                     throw new SQLException("Internal error");
                  }

                  result = new Short((short) ((Long)tmp).intValue());
                  break;
               }
               case java.sql.Types.INTEGER:
               {
                  if (! (tmp instanceof Long))
                  {
                     throw new SQLException("Internal error");
                  }

                  result = new Integer(((Long)tmp).intValue());
                  break;
               }
               case java.sql.Types.BIGINT:
               {
                  if (! (tmp instanceof Long))
                  {
                     throw new SQLException("Internal error");
                  }

                  result = (Long)tmp;
                  break;
               }
               case java.sql.Types.REAL:
               {
                  if (! (tmp instanceof Float))
                  {
                     throw new SQLException("Internal error");
                  }

                  result = (Float)tmp;
                  break;
               }
               case java.sql.Types.FLOAT:
               case java.sql.Types.DOUBLE:
               {
                  if (tmp instanceof Double)
                  {
                     result = (Double)tmp;
                  }
                  else if (tmp instanceof Float)
                  {
                     result = new Double(((Float)tmp).doubleValue());
                  }
                  else
                  {
                     throw new SQLException("Was expecting Double data.  Got"
                                            + tmp.getClass().getName());
                  }

                  break;
               }
               case java.sql.Types.DATE:
               {
                  // XXX How do the time types hold up with timezones?
                  if (! (tmp instanceof Timestamp))
                  {
                     throw new SQLException("Internal error");
                  }

//                   java.util.Calendar  cal = new java.util.GregorianCalendar();
//                   cal.setTime(getTimestamp(columnIndex));
//                   result = cal.getTime();
                  result = new Date(((Timestamp)tmp).getTime());
                  break;
               }
               case java.sql.Types.TIME:
               {
                  if (! (tmp instanceof Timestamp))
                  {
                     throw new SQLException("Internal error");
                  }

                  result = new Time(((Timestamp)tmp).getTime());
                  break;
               }
               case java.sql.Types.TIMESTAMP:
               {
                  if (! (tmp instanceof Timestamp))
                  {
                     throw new SQLException("Internal error");
                  }

                  result = (Timestamp) tmp;
                  break;
               }
               case java.sql.Types.BINARY:
               case java.sql.Types.VARBINARY:
               {
                  result = getBytes(columnIndex);
                  break;
               }
               case java.sql.Types.DECIMAL:
               case java.sql.Types.NUMERIC:
               {
                  if (tmp instanceof BigDecimal)
                  {
                     result = ((BigDecimal)tmp);
                  }
                  else
                  {
                     throw new SQLException("Was expecting NUMERIC data.  Got"
                                            + tmp.getClass().getName());
                  }
                  break;
               }
               case java.sql.Types.LONGVARCHAR:
               {
                  if (tmp instanceof TdsAsciiInputStream)
                  {
                     result = ((TdsAsciiInputStream)tmp).toString();
                  }
                  else if (tmp instanceof java.lang.String)
                  {
                     result = tmp;
                  }
                  else
                  {
                     throw new SQLException("Was expecting LONGVARCHAR data. "
                                            + "Got "
                                            + tmp.getClass().getName());
                  }
                  break;
               }
               case java.sql.Types.LONGVARBINARY:
               {
                  throw new SQLException("Not implemented");
               }
               case java.sql.Types.NULL:
               {
                  throw new SQLException("Not implemented");
               }
               case java.sql.Types.OTHER:
               {
                  throw new SQLException("Not implemented");
               }
               case java.sql.Types.BIT:
               {
                  if (tmp instanceof Boolean)
                  {
                     result = ((Boolean)tmp);
                  }
                  else
                  {
                     throw new SQLException("Was expecting BIT data. "
                                            + "Got"
                                            + tmp.getClass().getName());
                  }
                  break;
               }
               default:
               {
                  String msg = ""
                     + "Unknown datatype "
                     + getMetaData().getColumnType(columnIndex);
                  throw new SQLException(msg);
               }
            }
         }
      }
      catch (com.internetcds.jdbc.tds.TdsException e)
      {
         e.printStackTrace();
         throw new SQLException(e.getMessage());
      }
      return result;
   } // getObject()


   /**
    * <p>Get the value of a column in the current row as a Java object.
    *
    * <p>This method will return the value of the given column as a
    * Java object.  The type of the Java object will be the default
    * Java Object type corresponding to the column's SQL type,
    * following the mapping specified in the JDBC spec.
    *
    * JDBC 2.0
    *
    * 

⌨️ 快捷键说明

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