📄 simpletextresultset.java
字号:
//----------------------------------------------------------------------------//// Module: SimpleTextResultSet.java//// Description: Implementation of the JDBC ResultSet interface//// Author: Karl Moss//// Copyright: (C) 1996,1997 Karl Moss. All rights reserved.// You may study, use, modify and distribute this example// for any purpose, provided that this copyright notice// appears in all copies. This example is provided WITHOUT// WARRANTY either expressed or implied.//----------------------------------------------------------------------------package jdbc.SimpleText;//----------------------------------------------------------------------------// A ResultSet provides access to a table of data generated by// executing a Statement. The table rows are retrieved in// sequence. Within a row its column values can be accessed in any// order.//// A ResultSet maintains a cursor pointing to its current row of// data. Initially the cursor is positioned before the first row.// The 'next' method moves the cursor to the next row.//// The getXXX methods retrieve column values for the current// row. You can retrieve values either using the index number of the// column, or by using the name of the column. In general using the// column index will be more efficient. Columns are numbered from 1.//// For maximum portability, ResultSet columns within each row should be// read in left-to-right order and each column should be read only once.//// For the getXXX methods, the JDBC driver attempts to convert the// underlying data to the specified Java type and returns a suitable// Java value. See the JDBC specification for allowable mappings// from SQL types to Java types with the ResultSet.getXXX methods.//// Column names used as input to getXXX methods are case insensitive.// When performing a getXXX using a column name if several columns have// the same name then the value of the first matching column will be// returned.//// A ResultSet is automatically closed by the Statement that// generated it when that Statement is closed, re-executed, or is used// to retrieve the next result from a sequence of multiple results.//// The number, types and properties of a ResultSet's columns are// provided by the ResulSetMetaData object returned by the getMetaData// method.//----------------------------------------------------------------------------// NOTE - this is an implementation of the JDBC API version 1.20//---------------------------------------------------------------------------import java.sql.*;import java.util.Hashtable;import java.io.*;public class SimpleTextResultSet extends SimpleTextObject implements ResultSet{ //------------------------------------------------------------------------ // initialize //------------------------------------------------------------------------ public void initialize( SimpleTextIStatement statement, String catalog, String table, Hashtable columns, SimpleTextFilter filter) throws SQLException { // Save the owning statement object ownerStatement = statement; ownerConnection = ownerStatement.getConnection(); // Save the in-memory column definitions inMemoryColumns = columns; // Save the select WHERE filter selectFilter = filter; // If a table was given, open it now if (table != null) { openSDF(catalog, table); } } public void initialize( SimpleTextIStatement statement, Hashtable columns, Hashtable rows) throws SQLException { // Save the in-memory rows (used for catalog functions) inMemoryRows = rows; rowNum = 0; initialize(statement, null, null, columns, null); } //------------------------------------------------------------------------ // next - JDBC API // 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. // // 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. // // Returns true if the new current row is valid; false if there // are no more rows //------------------------------------------------------------------------ public boolean next() throws SQLException { boolean rc = true; boolean validRow = false; // In memory result set, get the next row if (inMemoryRows != null) { rowNum++; // No more rows, return end-of-file if (rowNum > inMemoryRows.size()) { rc = false; } } else { // Not in-memory, read the next line from the file until a // valid row has been found while (!validRow) { currentLine = readLine(rafSDF); if (currentLine == null) { rc = false; break; } // We'll cheat a little bit here. We'll use the SQL // parser to break the line up, then treat it as a // comma separated list (much like a select list) String data[] = ownerConnection.parseSQL(currentLine); Hashtable dataList = new Hashtable(); ownerStatement.buildList(data, 0, "", dataList); // Now go through each data element and create a // CommonValue object. Then, put the CommonValue object // on our columnValues list SimpleTextColumn column; columnValues = new Hashtable(); String s; CommonValue value; for (int i = 1; i <= dataList.size(); i++) { column = (SimpleTextColumn) dataList.get(new Integer(i)); // Get the data item s = column.name; // Remove any quotes if (s.startsWith("'") && s.endsWith("'")) { s = s.substring(1, s.length() - 1); } // Create a CommonValue object using the string value = new CommonValue(s); // Create a CommonValue object columnValues.put(new Integer(i), value); } // Filter the row, if necessary validRow = filterRow(columnValues, selectFilter); } } return rc; } //------------------------------------------------------------------------ // close - JDBC API // In some cases, it is desirable to immediately release a // ResultSet's database and JDBC resources instead of waiting for // this to happen when it is automatically closed; the close // method provides this immediate release. // // Note: A ResultSet is automatically closed by the // Statement that generated it when that Statement is closed, // re-executed, or is used to retrieve the next result from a // sequence of multiple results. A ResultSet is also automatically // closed when it is garbage collected. //------------------------------------------------------------------------ public void close() throws SQLException { try { if (rafSDF != null) { rafSDF.close(); } if (rafSBF != null) { rafSBF.close(); } } catch (Exception ex) { } } //------------------------------------------------------------------------ // wasNull - JDBC API // 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. // // Returns true if last column read was SQL NULL //------------------------------------------------------------------------ public boolean wasNull() throws SQLException { return lastNull; } //------------------------------------------------------------------------ // getString - JDBC API // Get the value of a column in the current row as a Java String. // // columnIndex the first column is 1, the second is 2, ... // // Returns the column value; if the value is SQL NULL the result is null //------------------------------------------------------------------------ public String getString( int columnIndex) throws SQLException { // Verify the column verify(columnIndex); String s = null; if (inMemoryRows != null) { s = (getColumn(rowNum, columnIndex)).getString(); } else { CommonValue value = getValue(columnIndex); if (value != null) { s = value.getString(); } } if (s == null) { lastNull = true; } return s; } //------------------------------------------------------------------------ // getBoolean - JDBC API // Get the value of a column in the current row as a Java boolean. // // columnIndex the first column is 1, the second is 2, ... // // Returns the column value; if the value is SQL NULL the result is false //------------------------------------------------------------------------ public boolean getBoolean( int columnIndex) throws SQLException { // We could coerce the data, but for now an exception is thrown throw DataTypeNotSupported(); } //------------------------------------------------------------------------ // getByte - JDBC API // Get the value of a column in the current row as a Java byte. // // columnIndex the first column is 1, the second is 2, ... // Returns the column value; if the value is SQL NULL the result is 0 //------------------------------------------------------------------------ public byte getByte( int columnIndex) throws SQLException { // We could coerce the data, but for now an exception is thrown throw DataTypeNotSupported(); } //------------------------------------------------------------------------ // getShort - JDBC API // Get the value of a column in the current row as a Java short. // // columnIndex the first column is 1, the second is 2, ... // // Returns the column value; if the value is SQL NULL the result is 0 //------------------------------------------------------------------------ public short getShort( int columnIndex) throws SQLException { // We could coerce the data, but for now an exception is thrown throw DataTypeNotSupported(); } //------------------------------------------------------------------------ // getInt - JDBC API // Get the value of a column in the current row as a Java int. // // columnIndex the first column is 1, the second is 2, ... // // Returns the column value; if the value is SQL NULL the result is 0 //------------------------------------------------------------------------ public int getInt( int columnIndex) throws SQLException { // Verify the column verify(columnIndex); CommonValue value; if (inMemoryRows != null) { value = getColumn(rowNum, columnIndex); } else { value = getValue(columnIndex); } // Check for a null value if (value == null) { lastNull = true; return 0; } if (value.isNull()) { lastNull = true; return 0; } return value.getInt(); } //------------------------------------------------------------------------ // getLong - JDBC API // Get the value of a column in the current row as a Java long. // // columnIndex the first column is 1, the second is 2, ... // // Returns the column value; if the value is SQL NULL the result is 0 //------------------------------------------------------------------------ public long getLong( int columnIndex) throws SQLException { // We could coerce the data, but for now an exception is thrown throw DataTypeNotSupported(); } //------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -