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

📄 csvresultset.java

📁 一种解析csv文件特别好的方法
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
/*
 *  CsvJdbc - a JDBC driver for CSV files
 *  Copyright (C) 2001  Jonathan Ackerman
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library 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
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package org.relique.jdbc.csv;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader;
import java.math.BigDecimal;
import java.net.URL;
import java.sql.*;
import java.util.Calendar;
import java.util.Map;

/**
 * This class implements the ResultSet interface for the CsvJdbc driver.
 *
 * @author     Jonathan Ackerman
 * @author     Michael Maraya
 * @author     Tomasz Skutnik
 * @author     Chetan Gupta
 * @version    $Id: CsvResultSet.java,v 1.16 2004/08/09 21:56:55 jackerm Exp $
 */
public class CsvResultSet implements ResultSet {

    /** Metadata for this ResultSet */
    protected ResultSetMetaData resultSetMetaData;

    /** Statement that produced this ResultSet */
    protected CsvStatement statement;

    protected int isScrollable = ResultSet.TYPE_SCROLL_SENSITIVE;
    
    /** Helper class that performs the actual file reads */
    protected CSVReaderAdapter reader;

    /** Table referenced by the Statement */
    protected String tableName;

    /** Array of available columns for referenced table */
    protected String[] columnNames;
    
    /** Last column name index read */
    protected int lastIndexRead = -1;
    
    /** Number of the column that is used on the where clause */
    protected int whereColumn;
    
    /** String that is sought for on the where column */
    protected String whereValue;
    
    /** InputStream to keep track of */
    protected InputStream is;
    /**
     * Constructor for the CsvResultSet object
     *
     * @param statement Statement that produced this ResultSet
     * @param reader Helper class that performs the actual file reads
     * @param tableName Table referenced by the Statement
     * @param columnNames Array of available columns for referenced table
     */
    protected CsvResultSet(CsvStatement statement, CSVReaderAdapter reader,
                           String tableName, String[] columnNames, int isScrollable) {
    	this(statement,reader,tableName,columnNames,isScrollable,-1,null);
    }
    /**
     * Constructor for the CsvResultSet object 
     *
     * @param statement Statement that produced this ResultSet
     * @param reader Helper class that performs the actual file reads
     * @param tableName Table referenced by the Statement
     * @param columnNames Array of available columns for referenced table
     * @param whereColumn The zero base number for the column
     * @param whereValue The string to be sought for
     */
    protected CsvResultSet(CsvStatement statement, CSVReaderAdapter reader,
            String tableName, String[] columnNames, int isScrollable, int whereColumn, String whereValue) {
        this.statement = statement;
        this.isScrollable = isScrollable;
        this.reader = reader;
        this.tableName = tableName;
        this.columnNames = columnNames;
        this.whereColumn = whereColumn;
        this.whereValue = whereValue;
        if(columnNames[0].equals("*")) {
            this.columnNames = reader.getColumnNames();
        }
    }

    /**
     * Moves the cursor down one row from its current position.
     * A <code>ResultSet</code> cursor is initially positioned
     * before the first row; the first call to the method
     * <code>next</code> makes the first row the current row; the
     * second call makes the second row the current row, and so on.
     *
     * <P>If an input stream is open for the current row, a call
     * to the method <code>next</code> will
     * implicitly close it. A <code>ResultSet</code> object's
     * warning chain is cleared when a new row is read.
     *
     * @return <code>true</code> if the new current row is valid;
     * <code>false</code> if there are no more rows
     * @exception SQLException if a database access error occurs
     */
    public boolean next() throws SQLException {
    	boolean answer = false;
    	answer = reader.next();
    	//We have a where clause, honor it    	
    	if(whereColumn>-1) {      		
    		while(answer && !reader.getColumn(whereColumn).equals(whereValue)) {
    			answer = reader.next();
    		}
    	}
    	return answer;
    }

    /**
     * Releases this <code>ResultSet</code> object's database and
     * JDBC resources immediately instead of waiting for
     * this to happen when it is automatically closed.
     *
     * <P><B>Note:</B> A <code>ResultSet</code> object
     * is automatically closed by the
     * <code>Statement</code> object that generated it when
     * that <code>Statement</code> object is closed,
     * re-executed, or is used to retrieve the next result from a
     * sequence of multiple results. A <code>ResultSet</code> object
     * is also automatically closed when it is garbage collected.
     *
     * @exception SQLException if a database access error occurs
     */
    public void close() throws SQLException {
        reader.close();
    }

    /**
     * Reports whether
     * the last column read had a value of SQL <code>NULL</code>.
     * Note that you must first call one of the getter methods
     * on a column to try to read its value and then call
     * the method <code>wasNull</code> to see if the value read was
     * SQL <code>NULL</code>.
     *
     * @return <code>true</code> if the last column value read was SQL
     *         <code>NULL</code> and <code>false</code> otherwise
     * @exception SQLException if a database access error occurs
     */
    public boolean wasNull() throws SQLException {
        if(lastIndexRead >= 0) {
            return getString(lastIndexRead) == null;
        } else {
            throw new SQLException("No previous getter method called");
        }
    }

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

    /**
     * Retrieves the value of the designated column in the current row
     * of this <code>ResultSet</code> object as
     * a <code>String</code> in the Java programming language.
     *
     * @param columnIndex the first column is 1, the second is 2, ...
     * @return the column value; if the value is SQL <code>NULL</code>, the
     * value returned is <code>null</code>
     * @exception SQLException if a database access error occurs
     */
    public String getString(int columnIndex) throws SQLException {
        // perform pre-accessor method processing
        preAccessor(columnIndex);
        // use CsvReader.getColumn(String) to retrieve the column
        if (columnIndex < 1 || columnIndex > columnNames.length) {
            throw new SQLException("Column not found: invalid index: "+columnIndex);
        }
        return reader.getColumn(columnNames[columnIndex-1]);
    }

    /**
     * Retrieves the value of the designated column in the current row
     * of this <code>ResultSet</code> object as
     * a <code>boolean</code> in the Java programming language.
     *
     * @param columnIndex the first column is 1, the second is 2, ...
     * @return the column value; if the value is SQL <code>NULL</code>, the
     * value returned is <code>false</code>
     * @exception SQLException if a database access error occurs
     */
    public boolean getBoolean(int columnIndex) throws SQLException {
        String str = getString(columnIndex);
        return (str == null) ? false : Boolean.valueOf(str).booleanValue();
    }

    /**
     * Retrieves the value of the designated column in the current row
     * of this <code>ResultSet</code> object as
     * a <code>byte</code> in the Java programming language.
     *
     * @param columnIndex the first column is 1, the second is 2, ...
     * @return the column value; if the value is SQL <code>NULL</code>, the
     * value returned is <code>0</code>
     * @exception SQLException if a database access error occurs
     */
    public byte getByte(int columnIndex) throws SQLException {
        String str = getString(columnIndex);
        return (str == null) ? 0 : Byte.parseByte(str);
    }

    /**
     * Retrieves the value of the designated column in the current row
     * of this <code>ResultSet</code> object as
     * a <code>short</code> in the Java programming language.
     *
     * @param columnIndex the first column is 1, the second is 2, ...
     * @return the column value; if the value is SQL <code>NULL</code>, the
     * value returned is <code>0</code>
     * @exception SQLException if a database access error occurs
     */
    public short getShort(int columnIndex) throws SQLException {
        String str = getString(columnIndex);
        return (str == null) ? 0 : Short.parseShort(str);
    }

    /**
     * Gets the value of the designated column in the current row
     * of this <code>ResultSet</code> object as
     * an <code>int</code> in the Java programming language.
     *
     * @param columnIndex the first column is 1, the second is 2, ...
     * @return the column value; if the value is SQL <code>NULL</code>, the
     * value returned is <code>0</code>
     * @exception SQLException if a database access error occurs
     */
    public int getInt(int columnIndex) throws SQLException {
        String str = getString(columnIndex);
        return (str == null) ? 0 : Integer.parseInt(str);
    }

    /**
     * Retrieves the value of the designated column in the current row
     * of this <code>ResultSet</code> object as
     * a <code>long</code> in the Java programming language.
     *
     * @param columnIndex the first column is 1, the second is 2, ...
     * @return the column value; if the value is SQL <code>NULL</code>, the
     * value returned is <code>0</code>
     * @exception SQLException if a database access error occurs
     */
    public long getLong(int columnIndex) throws SQLException {
        String str = getString(columnIndex);
        return (str == null) ? 0L : Long.parseLong(str);
    }

    /**
     * Gets the value of the designated column in the current row
     * of this <code>ResultSet</code> object as
     * a <code>float</code> in the Java programming language.
     *
     * @param columnIndex the first column is 1, the second is 2, ...
     * @return the column value; if the value is SQL <code>NULL</code>, the
     * value returned is <code>0</code>
     * @exception SQLException if a database access error occurs
     */
    public float getFloat(int columnIndex) throws SQLException {
        String str = getString(columnIndex);
        return (str == null) ? 0F : Float.parseFloat(str);
    }

    /**
     * Retrieves the value of the designated column in the current row
     * of this <code>ResultSet</code> object as
     * a <code>double</code> in the Java programming language.
     *
     * @param columnIndex the first column is 1, the second is 2, ...
     * @return the column value; if the value is SQL <code>NULL</code>, the
     * value returned is <code>0</code>
     * @exception SQLException if a database access error occurs
     */
    public double getDouble(int columnIndex) throws SQLException {
        String str = getString(columnIndex);
        return (str == null) ? 0D : Double.parseDouble(str);
    }

    /**
     * Retrieves the value of the designated column in the current row

⌨️ 快捷键说明

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