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

📄 tinysqlresultset.java

📁 有关JDBC的使用一些编程实例,有关与数据库连接的代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * * The tinySQLResultSet class for the tinySQL JDBC Driver * * A lot of this code is based on or directly taken from * George Reese's (borg@imaginary.com) mSQL driver. * * So, it's probably safe to say: * * Portions of this code Copyright (c) 1996 George Reese * * The rest of it: * * Copyright 1996, Brian C. Jepson *                 (bjepson@ids.net) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */import java.sql.Date;import java.math.BigDecimal;import java.math.BigInteger;import java.sql.ResultSet;import java.sql.ResultSetMetaData;import java.sql.SQLException;import java.sql.SQLWarning;import java.sql.Time;import java.sql.Timestamp;import java.sql.Types;import java.text.SimpleDateFormat;import java.util.Hashtable;import java.text.ParsePosition;public class tinySQLResultSet implements java.sql.ResultSet {  /**   *   * The tsResultSet   *    */  private tsResultSet result;  /**   *   * A tsRow object to hold the current row   *   */  private tsRow current_row;    /**   *   * The index of the current row   *   */  private int current_row_index = 0;  /**   *   * The meta data for this result set.   *   */  private tinySQLResultSetMetaData meta;  /**   *   * A Hashtable that maps column names to columns   *   */  private Hashtable column_map = null;  /**   *   * Given a tsResultSet, this will construct a new tinySQLResultSet   * @param res the tsResultSet from a query   *   */  public tinySQLResultSet(tsResultSet res) {    result = res;  }  /**   *   * Advance to the next row in the result set.    * @see java.sql.ResultSet#next   * @exception SQLException thrown in case of error   * @return true if there are any more rows to process, otherwise false.   *   */  public synchronized boolean next() throws SQLException {    try {      // automatically return false if the      // result set is empty      //      if( result.size() < 1 ) {	return false;      }      // return false if the index of the current row      // is equal to the size of the result set      //      if( current_row_index == result.size() ) {	return false;      }      // increment the current row index      //      current_row_index++;      // retrieve the row at the current_row_index and store      // it in the current_row field.      //      current_row = result.rowAt(current_row_index - 1);      return true;    } catch( Exception e ) {      throw new SQLException(e.getMessage());    }  }      /**   *   * Provides a method to close a ResultSet   *    * @see java.sql.ResultSet#close   *   */  public void close() throws SQLException {  }  /**   *   * Returns whether or not the last column read was null.   * tinySQL doesn't have nulls, so this is inconsequential...   * @see java.sql.ResultSet#wasNull   * @return true if the column was null, false otherwise   *   */  public boolean wasNull() throws SQLException {    return false;  }  /**   *    * Gets the value of a column (by index) as a String.   * @see java.sql.ResultSet#getString   * @exception SQLException thrown for bogus column index   * @param column the column index   * @return the column's String value   *   */  public String getString(int column) throws SQLException {    // retrieve the column at the specified index. tinySQL    // has a column offset of zero, while JDBC uses one.    // Because of this, I need to subtract one from the    // index to get the tinySQL index.    //    tsColumn col = result.columnAtIndex(column-1);    // return the column's value    //    return current_row.columnAsString(col);  }  /**   *   * Get the value of a column in the current row as a Java byte.   *   * @param columnIndex the first column is 1, the second is 2, ...   * @return the column value   *   */  public byte getByte(int column) throws SQLException {    // get the column as a string    //    String str = getString(column);       // if it's a blank string, return 0,    // otherwise, cast the first character     // in the string to byte and return it.    //    if( str.equals("") ) {      return 0;    } else {      return (byte)str.charAt(0);    }  }    /**   *   * Get the value of a column in the current row as boolean   * @see java.sql.ResultSet#getBoolean   * @exception SQLException Harum Scarum's coming down fast, Clay...   * @param column the column index   * @return false for "", null, or "0"; true otherwise   */  public boolean getBoolean(int column) throws SQLException {    try {          // get the column as a string      //      String str = getString(column);            // a blank string is false      //      if( str.equals("") ) return false;      // a zero is false      //      if( str.equals("0") ) return false;      // nothing is true... everything is permitted      //      return true;    } catch( Exception e ) {      throw new SQLException(e.getMessage());    }  }  /**   *   * Get the value of a column in the current row as a short.   * @see java.sql.ResultSet#getShort   * @exception SQLException D'ohh!   * @param column the column being retrieved   * @return the column as a short   *   */  public short getShort(int column) throws SQLException {        // get the column as a string    //    String str = getString(column);    // if it's null, return 0    //    if( str == null ) return 0;    // try to convert it to an integer, and cast it to short    //    try {      return (short)(Integer.valueOf(str).intValue());    } catch( NumberFormatException e ) {      throw new SQLException(e.getMessage());    }  }    /**   *   * Retrieve a column from the current row as an int   * @see java.sql.ResultSet#getInt   * @exception SQLException bad things... the wind began to howl...   * @param column the column being retrieved   * @return the column as an integer   *   */  public int getInt(int column) throws SQLException {        // get the column as a string    //    String str = getString(column);    // if it's null, return 0    //    if( str == null ) return 0;    // try to convert this string to integer    //    try {      return Integer.valueOf(str).intValue();    } catch( NumberFormatException e ) {      throw new SQLException(e.getMessage());    }      }      /**   *    * Get the value of a column in the current row as a long   * @see java.sql.ResultSet#getLong   * @exception SQLException in case of an error   * @param column the column being retrieved   * @return the column as a long   *   */  public long getLong(int column) throws SQLException {        // get the column as a string    //    String str = getString(column);    // it it's null, return 0    if( str == null ) return 0;    // try to convert the String to a long    //    try {      return Long.valueOf(str).longValue();    } catch( NumberFormatException e ) {      throw new SQLException(e.getMessage());    }      }    /**   *   * Return a column as a float.   * @see java.sql.ResultSet#getFloat   * @exception SQLException in case of error   * @param column the column being retrieved   * @return the column as a float   *   */  public float getFloat(int column) throws SQLException {        // get the column as a string    //    String str = getString(column);       // if it's null, assume zero    //    if( str == null ) return 0;    // try to perform the conversion    //    try {      return Float.valueOf(str).floatValue();    } catch( NumberFormatException e ) {      throw new SQLException(e.getMessage());    }  }    /**   *   * Return a column as a double   * @see java.sql.ResultSet#getDouble   * @exception SQLException in case of error   * @param column the column being retrieved   * @return the column as a double   *   */  public double getDouble(int column) throws SQLException {        // get the column as a string    //    String str = getString(column);    // it it's null, return zero    //    if( str == null ) return 0;    // attempt the conversion    //    try {      return Double.valueOf(str).doubleValue();    } catch( NumberFormatException e ) {      throw new SQLException(e.getMessage());    }  }    /**   *   * Return a column as a BigDecimal object   * @see java.sql.ResultSet#getBigDecimal   * @exception SQLException in case of a problem   * @param column the column being retrieved   * @param scale the number of digits to the right of the decimal   * @return the column as a BigDecimal   *   */  public BigDecimal getBigDecimal(int column, int scale)       throws SQLException {        // get the column as a string    //    String str = getString(column);    // return null as zero, otherwise use the string    //    if( str == null ) {      return new BigDecimal(new BigInteger("0"), scale);    } else {      return new BigDecimal(new BigInteger(str), scale);    }  }  /**   *   * Get the value of a column in the current row as a Java byte array.   * @see java.sql.ResultSet#getBytes   * @exception SQLException thrown in case of trouble   * @param column the column being retrieved   * @return a byte array that is the value of the column   *   */  public byte[] getBytes(int column) throws SQLException {        // get the column as a string    //    String str = getString(column);    if( str == null ) return null;    try {      return str.getBytes(str);    }    catch( java.io.UnsupportedEncodingException e ) {      throw new java.sql.SQLException("Bad bytes!: " + e.getMessage());    }  }  /**   *   * Get the value of a column in the current row as a java.sql.Date object.   * @see java.sqlResultSet#getDate   * @exception SQLException thrown in case of error   * @param column the column being retrieved   * @return the java.sql.Date object for the column   *   */  public java.sql.Date getDate(int column)       throws SQLException {        // get the column as a string    //    String str = getString(column);    // return null if the string is null    //    if( str == null ) return null;    // try to use the string to instantiate a java.util.Date object,    // then use that object to instantiate a java.sql.Date object.    //    try {      SimpleDateFormat fmt = new SimpleDateFormat("EEE MMM dd hh:mm:ss z yyyy");      java.util.Date d = fmt.parse(str, new ParsePosition(0));      return new java.sql.Date(d.getTime());        } catch( Exception e ) {      throw new SQLException("Date format error: " + e.getMessage());    }  }  /**   *   * Get the value of a column in the current row as a java.sql.Time object.   *   * @see java.sql.ResultSet#getTime   * @exception SQLException thrown in the event of troubles   * @param column the column being retrieved   * @return the column as a java.sql.Time object   *   */  public java.sql.Time getTime(int column)       throws SQLException {        // get the column as a string    //    String str = getString(column);    // if the string is null, return null    //    if( str == null ) return null;    // try to use the string to instantiate a java.util.Date object,    // then use that object to instantiate a java.sql.Time object.    //    try {      SimpleDateFormat fmt = new SimpleDateFormat("EEE MMM dd hh:mm:ss z yyyy");      java.util.Date d = fmt.parse(str, new ParsePosition(0));      return new java.sql.Time(d.getTime());          } catch( Exception e ) {      throw new SQLException("Data format error: " + e.getMessage());    }  }  /**   * Get the value of a column in the current row as a java.sql.Timestamp   * @see java.sql.ResultSet#getTimestamp   * @exception SQLException thrown in the event of troubles   * @param column the column being retrieved   * @return the column as a java.sql.Timestamp object   */  public java.sql.Timestamp getTimestamp(int column)       throws SQLException {

⌨️ 快捷键说明

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