?? resultset.java
字號:
// Copyright 2003 Nokia Corporation.
//
// THIS SOURCE CODE IS PROVIDED 'AS IS', WITH NO WARRANTIES WHATSOEVER,
// EXPRESS OR IMPLIED, INCLUDING ANY WARRANTY OF MERCHANTABILITY, FITNESS
// FOR ANY PARTICULAR PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE
// OR TRADE PRACTICE, RELATING TO THE SOURCE CODE OR ANY WARRANTY OTHERWISE
// ARISING OUT OF ANY PROPOSAL, SPECIFICATION, OR SAMPLE AND WITH NO
// OBLIGATION OF NOKIA TO PROVIDE THE LICENSEE WITH ANY MAINTENANCE OR
// SUPPORT. FURTHERMORE, NOKIA MAKES NO WARRANTY THAT EXERCISE OF THE
// RIGHTS GRANTED HEREUNDER DOES NOT INFRINGE OR MAY NOT CAUSE INFRINGEMENT
// OF ANY PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OWNED OR CONTROLLED
// BY THIRD PARTIES
//
// Furthermore, information provided in this source code is preliminary,
// and may be changed substantially prior to final release. Nokia Corporation
// retains the right to make changes to this source code at
// any time, without notice. This source code is provided for informational
// purposes only.
//
// Nokia and Nokia Connecting People are registered trademarks of Nokia
// Corporation.
// Java and all Java-based marks are trademarks or registered trademarks of
// Sun Microsystems, Inc.
// Other product and company names mentioned herein may be trademarks or
// trade names of their respective owners.
//
// A non-exclusive, non-transferable, worldwide, limited license is hereby
// granted to the Licensee to download, print, reproduce and modify the
// source code. The licensee has the right to market, sell, distribute and
// make available the source code in original or modified form only when
// incorporated into the programs developed by the Licensee. No other
// license, express or implied, by estoppel or otherwise, to any other
// intellectual property rights is granted herein.
package example.mesql;
import java.util.*;
public class ResultSet
{
// keeps track of the current row
private int currentRow = 0;
private Vector allRows = new Vector();
private Vector columns = new Vector();
private Vector columnTypes = new Vector();
private Statement statement = null;
private int rowsUpdated = -1;
ResultSet(String response) throws SQLException
{
parseResponse(response);
}
// moves to an absolute toe
public boolean absolute(int row)
{
if (row < 0)
{
row = allRows.size() - row;
}
if (row < 0)
{
row = 1;
}
if (row > allRows.size())
{
row = allRows.size();
}
currentRow = row;
return currentRow > 0 && currentRow <= allRows.size();
}
// move to the row after the last
public void afterLast()
{
currentRow = allRows.size() + 1;
}
// move to the row before the first one
public void beforeFirst()
{
currentRow = 0;
}
// finds a column by name
public int findColumn(String columnName)
throws SQLException
{
for (int i = 0; i < columns.size(); i++)
{
if (columns.elementAt(i).equals(columnName.toLowerCase()))
{
return i + 1;
}
}
throw new SQLException(new StringBuffer("Column ")
.append(columnName).append(" not in this result set")
.toString());
}
// goes to the first row
public boolean first()
{
return absolute(1);
}
// checks whether it is after the last row
public boolean isAfterLast()
{
return currentRow == allRows.size() + 1;
}
// checks whether it is before the first row
public boolean isBeforeFirst()
{
return currentRow == 0;
}
// checks whether it is in the first row
public boolean isFirst()
{
return currentRow == 1;
}
// checks whether it is in the kast row
public boolean isLast()
{
return currentRow == allRows.size();
}
// goes to the last row
public boolean last()
{
return absolute(-1);
}
// goes to the next row
public boolean next()
{
if (currentRow <= allRows.size())
{
currentRow++;
}
return currentRow <= allRows.size();
}
// goes back to the first row
public boolean previous()
{
if (currentRow > 0)
{
currentRow--;
}
return currentRow > 0;
}
// move a relative amount of rows
public boolean relative(int rows)
{
return absolute(currentRow + rows);
}
// the following methods return the value of a
// column based on index or column name
// there are different methods for different types
// if the column doesn't exist or the type cannot be
// converted an exception is thrown
public boolean getBoolean(int columnIndex)
throws SQLException
{
String value = findValue(columnIndex);
return "true".equals(value) || "TRUE".equals(value);
}
public boolean getBoolean(String columnName)
throws SQLException
{
return getBoolean(findColumn(columnName));
}
public byte getByte(int columnIndex)
throws SQLException
{
String value = findValue(columnIndex);
byte result = 0;
if (value != null && !value.toLowerCase().equals("null"))
{
try
{
result = Byte.parseByte(value);
}
catch (NumberFormatException e)
{
throw new SQLException(e.getMessage());
}
}
return result;
}
byte getByte(String columnName)
throws SQLException
{
return getByte(findColumn(columnName));
}
public int getInt(int columnIndex)
throws SQLException
{
String value = findValue(columnIndex);
int result = 0;
if (value != null && !value.toLowerCase().equals("null"))
{
try
{
result = Integer.parseInt(value);
}
catch (NumberFormatException e)
{
throw new SQLException(e.getMessage());
}
}
return result;
}
public int getInt(String columnName)
throws SQLException
{
return getInt(findColumn(columnName));
}
long getLong(int columnIndex)
throws SQLException
{
String value = findValue(columnIndex);
long result = 0;
if (value != null && !value.toLowerCase().equals("null"))
{
try
{
result = Long.parseLong(value);
}
catch (NumberFormatException e)
{
throw new SQLException(e.getMessage());
}
}
return result;
}
long getLong(String columnName)
throws SQLException
{
return getLong(findColumn(columnName));
}
public int getRow()
{
return currentRow;
}
public short getShort(int columnIndex)
throws SQLException
{
String value = findValue(columnIndex);
short result = 0;
if (value != null && !value.toLowerCase().equals("null"))
{
try
{
result = Short.parseShort(value);
}
catch (NumberFormatException e)
{
throw new SQLException(e.getMessage());
}
}
return result;
}
public short getShort(String columnName)
throws SQLException
{
return getShort(findColumn(columnName));
}
public String getString(int columnIndex)
throws SQLException
{
return findValue(columnIndex);
}
public String getString(String columnName)
throws SQLException
{
return getString(findColumn(columnName));
}
// parses the HTTP response from the server
private void parseResponse(String response)
throws SQLException
{
// first a sanity check
if (response.startsWith("OK\nSELECT\n"))
{
response = response.substring(10);
// tokenize by lines
Tokenizer tokenizer = new Tokenizer(response, '\n');
String line = (String) tokenizer.nextElement();
if (line == null) {
throw new SQLException("Malformed query response");
}
// first fill in the column names
Tokenizer cols = new Tokenizer(line, ',');
while (cols.hasMoreElements())
{
columns.addElement(((String) cols.nextElement()).toLowerCase());
}
line = (String) tokenizer.nextElement();
if (line == null) {
throw new SQLException("Malformed query response");
}
// next fill in the column types
cols = new Tokenizer(line, ',');
while (cols.hasMoreElements())
{
columnTypes.addElement(cols.nextElement());
}
while (tokenizer.hasMoreElements())
{
line = (String)tokenizer.nextElement();
// store the rows unparsed
allRows.addElement(line);
}
}
}
private String findValue(int columnIndex)
throws SQLException
{
if (currentRow > allRows.size())
{
throw new SQLException("Row index too large");
}
String line = (String) allRows.elementAt(currentRow - 1);
Tokenizer tokenizer = new Tokenizer(line, ',');
int currentCol = 1;
while (tokenizer.hasMoreElements())
{
String token = (String) tokenizer.nextElement();
if (currentCol == columnIndex)
{
if ("%0".equals(token))
{
return null;
}
return Codec.decode(token);
}
currentCol++;
}
throw new SQLException("Column index too large");
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -