|
||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||
java.lang.Objectcom.softtech.jdbc.SQLResults
public class SQLResults
SQLResults stores the results of a sql query in the form of objects stored in an Arraylist
container object. It contains handy methods like getLong(), getDate(), getString(), etc.
for accessing fields in a SQLResults (result set).
Here is a complete code example:
driverName = "oracle.jdbc.driver.OracleDriver";
connURL = "jdbc:oracle:thin:@SNOWMASS:1521:WDEV";
username = "wdevprja";
password = "password";
ConnectionPool conPool = new ConnectionPool(int numPooledCon,
String driverName,
String conURL,
String username,
String password);
SQLExecutor sqlExec = new SQLExecutor(conPool);
SQLResults res = sqlExec.runQueryCloseCon("SELECT * FROM INV");
String out = "";
for (int row=0; row < res.getRowCount(); row++)
out += res.getLong(row, "TEST_ID") + " " + res.getString(row, "NOTES") + " " +
res.getDate(row, "TEST_DT") + " " + res.getDouble(row, "AMOUNT") + " " +
res.getString(row, "CODE") + "\n";
System.out.println(out);
There is also a handy toString() method which can be used to generate a text table
of the entire contents of the SQLResults result set. Here is a code example:
System.out.println(res.toString()); //output results as a text tableFor a simple employee table, the output of this toString() method will look something like:
EMPID FNAME LNAME ---------------------------------- 1.0 mark cook 2.0 carlos moya 3.0 Jeff Smith
| Field Summary | |
|---|---|
private int |
columnCount
number of columns returned by the sql query |
private java.util.ArrayList |
columnNames
ArrayList containing the column names returned by the sql query |
private int |
dbType
database type (e.g. |
private java.util.ArrayList |
results
ArrayList containing the results of the sql query |
private int |
toStringFormatWidth
formatted width of each field included in toString() |
| Constructor Summary | |
|---|---|
SQLResults(int dbType,
int columnCount)
Constructor creates ArrayList objects and initializes the dbType and columnCount |
|
| Method Summary | |
|---|---|
boolean |
add(java.lang.Object o)
Adds an object (a field value from a SELECT) to the sql results container) |
void |
addColumnName(java.lang.String columnName)
Adds a column name to the columnNames list |
private java.lang.String |
formatWithSpaces(java.lang.String s)
Formats a string by adding spaces on the end if the string is shorter than toStringFormatWidth, or truncates string if it is too long. |
boolean |
getBoolean(int row,
int col)
Gets the value of the field corresponding to (row, col) as a boolean |
boolean |
getBoolean(int row,
java.lang.String columnName)
Gets the value of the field corresponding to row, columnName as a boolean |
int |
getColumnCount()
Getter for number of columns |
private int |
getColumnIndex(java.lang.String columnName)
Given a columnName, return the corresponding index |
java.util.ArrayList |
getColumnNames()
Getter for SQL column names |
java.sql.Date |
getDate(int row,
int col)
Gets the value of the field corresponding to (row, col) as a java.sql.Date |
java.sql.Date |
getDate(int row,
java.lang.String columnName)
Gets the value of the field corresponding to row, columnName as a java.sql.Date |
double |
getDouble(int row,
int col)
Gets the value of the field corresponding to (row, col) as a double |
double |
getDouble(int row,
java.lang.String columnName)
Gets the value of the field corresponding to row, columnName as a double |
float |
getFloat(int row,
int col)
Gets the value of the field corresponding to (row, col) as a float |
float |
getFloat(int row,
java.lang.String columnName)
Gets the value of the field corresponding to row, columnName as a float |
int |
getInt(int row,
int col)
Gets the value of the field corresponding to (row, col) index as an int |
int |
getInt(int row,
java.lang.String columnName)
Gets the value of the field corresponding to row, columnName as an int |
long |
getLong(int row,
int col)
Gets the value of the field corresponding to row, col as a long |
long |
getLong(int row,
java.lang.String columnName)
Gets the value of the field corresponding to row, columnName as a long |
java.lang.Object |
getObject(int row,
int col)
GetObject returns an object in the ArrayList results given a row and column index |
java.util.ArrayList |
getResults()
Getter for the entire SQL results object |
int |
getRowCount()
Gets the number of rows returned by query |
java.lang.String |
getString(int row,
int col)
Gets the value of the field corresponding to (row, col) index as a String |
java.lang.String |
getString(int row,
java.lang.String columnName)
Gets the value of the field corresponding to row, columnName as a String |
java.sql.Time |
getTime(int row,
int col)
Gets the value of the field corresponding to (row, col) as a java.sql.Time |
java.sql.Time |
getTime(int row,
java.lang.String columnName)
Gets the value of the field corresponding to row, columnName as a java.sql.Time |
java.sql.Timestamp |
getTimestamp(int row,
int col)
Gets the value of the field corresponding to (row, col) as a java.sql.Timestamp |
java.sql.Timestamp |
getTimestamp(int row,
java.lang.String columnName)
Gets the value of the field corresponding to row, columnName as a java.sql.Timestamp |
boolean |
isNull(int row,
int col)
Determines if field given by (row,col) is null |
boolean |
isNull(int row,
java.lang.String columnName)
Determines if field given by (row, columnName) is null |
void |
setToStringFormatWidth(int toStringFormatWidth)
Setter for string format width (used in toString() method) |
java.util.List |
toList()
Returns the SQLResults in the form of a List |
java.lang.String |
toString()
Returns the contents of SQLResults in a text table. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Field Detail |
|---|
private java.util.ArrayList results
private java.util.ArrayList columnNames
private int columnCount
private int dbType
private int toStringFormatWidth
| Constructor Detail |
|---|
public SQLResults(int dbType,
int columnCount)
dbType - columnCount - | Method Detail |
|---|
public java.util.ArrayList getResults()
public int getColumnCount()
public java.util.ArrayList getColumnNames()
public java.util.List toList()
public void setToStringFormatWidth(int toStringFormatWidth)
toStringFormatWidth - public boolean add(java.lang.Object o)
o - public int getRowCount()
public java.lang.Object getObject(int row,
int col)
row - col -
public void addColumnName(java.lang.String columnName)
columnName - columnName to addprivate int getColumnIndex(java.lang.String columnName)
columnName -
public int getInt(int row,
java.lang.String columnName)
row - columnName -
public int getInt(int row,
int col)
row - col -
public long getLong(int row,
java.lang.String columnName)
row - intcolumnName - String
public long getLong(int row,
int col)
row - col -
public java.lang.String getString(int row,
int col)
row - intcol - int
public java.lang.String getString(int row,
java.lang.String columnName)
row - intcolumnName - String
public boolean getBoolean(int row,
int col)
row - intcol - int
public boolean getBoolean(int row,
java.lang.String columnName)
row - intcolumnName - String
public java.sql.Date getDate(int row,
int col)
row - intcol - int
public java.sql.Date getDate(int row,
java.lang.String columnName)
row - intcolumnName - String
public java.sql.Time getTime(int row,
int col)
row - intcol - int
public java.sql.Time getTime(int row,
java.lang.String columnName)
row - intcolumnName - String
public java.sql.Timestamp getTimestamp(int row,
int col)
row - intcol - int
public java.sql.Timestamp getTimestamp(int row,
java.lang.String columnName)
row - intcolumnName - String
public double getDouble(int row,
int col)
row - intcol - int
public double getDouble(int row,
java.lang.String columnName)
row - intcolumnName - String
public float getFloat(int row,
int col)
row - intcol - int
public float getFloat(int row,
java.lang.String columnName)
row - intcolumnName - String
public java.lang.String toString()
toString in class java.lang.Objectprivate java.lang.String formatWithSpaces(java.lang.String s)
s - String
public boolean isNull(int row,
int col)
row - col -
public boolean isNull(int row,
java.lang.String columnName)
row - col -
|
||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||