📄 xmlresultset.java
字号:
/*
Copyright (C) 2003 Together
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.webdocwf.util.xml;
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.ArrayList;
import java.util.Map;
import java.util.Calendar;
/**
* Class that implements JDBC ResultSet interface.
*
* @author Zoran Milakovic
*/
public class XmlResultSet implements ResultSet {
/** Metadata for this ResultSet */
protected ResultSetMetaData resultSetMetaData;
/** Statement that produced this ResultSet */
protected Statement statement;
/** Helper class that performs the actual file reads */
protected XmlReader reader;
/** Table referenced by the Statement */
protected String tableName;
/** Array of available columns for referenced table */
protected String[] columnNames;
/** Array of column values for referenced table */
protected String[] columnValues;
protected String[] whereColumnNames;
protected String[] whereColumnValues;
/** Last column name index read */
protected int lastIndexRead = -1;
/** InputStream to keep track of */
protected InputStream is;
/**
* Constructor for the XmlResultSet 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 whereColumnNames is an array of column names
* @param whereColumnValues is an array of column values
*/
protected XmlResultSet(Statement statement, XmlReader reader , String tableName , String[] columnNames
, String[] whereColumnNames , String[] whereColumnValues ) {
this.statement = statement;
this.reader = reader;
this.tableName = tableName;
this.columnNames = columnNames;
this.whereColumnNames = whereColumnNames;
this.whereColumnValues = whereColumnValues;
}
public void select() throws SQLException {
reader.select( tableName , columnNames , whereColumnNames , whereColumnValues);
this.rset = reader.getResultSet();
}
public void selectTableNames() throws SQLException {
reader.selectTableNames();
this.rset = reader.getResultSet();
}
public void close() {
}
private int index = 0;
private ArrayList rset = new ArrayList();
/**
*Description of the Method
*
* @return Description of the Returned Value
* @exception SQLException
* @since
*/
public boolean next() throws SQLException {
boolean retVal = false;
try {
if( !(rset.size() <= index) ) {
this.columnValues = (String[])rset.get( index );
index++;
retVal = true;
} else {
index = 0;
rset = new ArrayList();
this.columnValues = new String[0];
retVal = false;
}
}catch( Exception e ) { throw new SQLException("Error in ResultSet.next() : "+e.getMessage()); }
return retVal;
}
public String getString(int i) throws SQLException {
try {
return this.columnValues[i-1].toString();
}catch(Exception e) { throw new SQLException("Error ResultSet.getString( index ) : "+e.getMessage()); }
}
/**
* 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>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
* of this <code>ResultSet</code> object as
* a <code>java.sql.BigDecimal</code> in the Java programming language.
*
* @param columnIndex the first column is 1, the second is 2, ...
* @param scale the number of digits to the right of the decimal point
* @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
* @deprecated
*/
public BigDecimal getBigDecimal(int columnIndex, int scale)
throws SQLException {
// let getBigDecimal(int) handle this for now
return getBigDecimal(columnIndex);
}
/**
* Retrieves the value of the designated column in the current row
* of this <code>ResultSet</code> object as
* a <code>byte</code> array in the Java programming language.
* The bytes represent the raw values returned by the driver.
*
* @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 byte[] getBytes(int columnIndex) throws SQLException {
String str = getString(columnIndex);
return (str == null) ? null : Utils.hexStringToBytes(str);
}
/**
* Retrieves the value of the designated column in the current row
* of this <code>ResultSet</code> object as
* a <code>java.sql.Date</code> object 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 Date getDate(int columnIndex) throws SQLException {
String str = getString(columnIndex);
return (str == null) ? null : Date.valueOf(str);
}
/**
* Retrieves the value of the designated column in the current row
* of this <code>ResultSet</code> object as
* a <code>java.sql.Time</code> object 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 Time getTime(int columnIndex) throws SQLException {
String str = getString(columnIndex);
return (str == null) ? null : Time.valueOf(str);
}
/**
* Retrieves the value of the designated column in the current row
* of this <code>ResultSet</code> object as a
* <code>java.sql.Timestamp</code> object in the Java programming language.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -