001: package com.jamonapi.utils;
002:
003: import java.sql.*;
004: import java.util.*;
005: import com.jamonapi.*;
006:
007: /**
008: * Purpose: Provides various methods for obtaining resultset data.
009: * Author: Andy Evans
010: * Date: 3/28/2001
011: */
012:
013: public class ResultSetUtils extends java.lang.Object {
014: static ResultSetUtils resultSetUtils = new ResultSetUtils();
015:
016: protected ResultSetUtils() {
017: }
018:
019: public static ResultSetUtils createInstance() {
020: return resultSetUtils;
021: }
022:
023: /**
024: * The following method returns an ArrayList containing the ResultSet column names and data.
025: * The first row of the ArrayList will always contain a string array of the column names.
026: * Subsequent rows contain ResultSet rows as object arrays. The getColumnNames() method is
027: * called to get the column names string array, and the getRowData() to convert ResultSet
028: * row data into an object array.
029: */
030: public void fullResultSetToArrayList(ArrayList arrayList,
031: ResultSet resultSet) throws SQLException {
032:
033: resultSetMetaDataToArrayList(arrayList, resultSet);
034: resultSetToArrayList(arrayList, resultSet);
035:
036: }
037:
038: private Monitor start(String locator) {
039: return AppConstants.start("ResultSetUtils." + locator);
040: }
041:
042: /**
043: * The following method returns an ArrayList containing the ResultSet column names and data.
044: * The first row of the ArrayList will always contain a string array of the column names.
045: * Subsequent rows contain ResultSet rows as object arrays. The getColumnNames() method is
046: * called to get the column names string array, and the getRowData() to convert ResultSet
047: * row data into an object array.
048: */
049: public void resultSetToArrayList(ArrayList arrayList,
050: ResultSet resultSet) throws SQLException {
051: //Monitor mon=start("resultSetToArrayList");
052: ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
053: int columnCount = resultSetMetaData.getColumnCount();
054:
055: //Loop through the ResultSet data rows and add each one to the ArrayList
056: while (resultSet.next()) {
057: arrayList.add(getRowData(resultSet, columnCount));
058: }
059:
060: //mon.stop();
061:
062: }
063:
064: /**
065: * The following method returns an ArrayList containing the ResultSetMetaData column names.
066: * There is only 1 row in the ArrayList and it will always contain a string array of the column names.
067: */
068: public void resultSetMetaDataToArrayList(ArrayList arrayList,
069: ResultSet resultSet) throws SQLException {
070: ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
071: int columnCount = resultSetMetaData.getColumnCount();
072:
073: //Add the String array of column names to the ArrayList
074: arrayList.add(getColumnNames(resultSetMetaData));
075:
076: }
077:
078: /**
079: * The following method converts ResultSet data to a Map of object keys and values using an instance
080: * of HashMap.
081: *
082: * The first column in the ResultSet supplies the key, the second column the value. If the ResultSet
083: * contains more than two columns then subsequent columns are ignored. If the ResultSet supplies
084: * only one column then an SQLException is thrown. The ResultSet values are converted to objects
085: * using the getRowData() method which converts a ResultSet row into an array of objects.
086: */
087: public void resultSetToMap(Map map, ResultSet resultSet)
088: throws SQLException {
089: int columnCount = resultSet.getMetaData().getColumnCount();
090: Object[] rowData;
091:
092: if (columnCount < 2) {
093: throw new SQLException(
094: "resultSetToHashMap: At least two columns needed for conversion.");
095: }
096:
097: //Loop through the ResultSet data rows and adding to the HashMap the first column as the key,
098: //and the second column as the value.
099: while (resultSet.next()) {
100: rowData = getRowData(resultSet, columnCount);
101: map.put(rowData[0], rowData[1]);
102: }
103:
104: }
105:
106: /**
107: * The following method converts an ArrayList, containing string arrays of ResultSet row data,
108: * into a two dimensional string array. This array must be first initialized before it
109: * can be passed as an argument to the ArrayList toArray() method.
110: *
111: * The first dimension represents the number of rows, including the column names, and
112: * can be obtained from the ArrayList size() method which returns the number of elements
113: * in the ArrayList.
114: *
115: * The second dimension represents the number of columns. This value is obtained from the
116: * length of the string array in the first element of the ArrayList since:
117: * 1. The length of all the string arrays in the ArrayList must be the same.
118: * 2. There must also be at least one row in the ArrayList representing the column names.
119: */
120: public String[][] arrayListToString(ArrayList arrayList) {
121: //Monitor mon=start("arrayListToString");
122:
123: String[][] resultStrings = new String[arrayList.size()][((String[]) arrayList
124: .get(0)).length];
125: arrayList.toArray(resultStrings);
126:
127: //mon.stop();
128: return resultStrings;
129: }
130:
131: /**
132: * The following method simply takes the ResultSet and converts it to a two dimensional
133: * array of strings containing the column names and data using calls to
134: * the resultSetToArrayList and arrayListToString methods.
135: */
136: public String[][] resultSetToString(ResultSet resultSet)
137: throws SQLException {
138: Monitor mon = start("resultSetToString[][]");
139: String[][] arr = null;
140: try {
141: ArrayList arrayList = new ArrayList();
142: resultSetToArrayList(arrayList, resultSet);
143: arr = arrayListToString(arrayList);
144: } finally {
145: mon.stop();
146: }
147: return arr;
148: }
149:
150: /**
151: * The following method simply takes the ResultSet and converts it to a two dimensional
152: * array of strings containing the column names and data using calls to
153: * the resultSetToArrayList and arrayListToString methods.
154: */
155: public String[][] fullResultSetToString(ResultSet resultSet)
156: throws SQLException {
157: ArrayList arrayList = new ArrayList();
158: fullResultSetToArrayList(arrayList, resultSet);
159: return arrayListToString(arrayList);
160: }
161:
162: /**
163: * The following method returns an array of strings containing the column names for
164: * a given ResultSetMetaData object.
165: */
166: public String[] getColumnNames(ResultSetMetaData resultSetMetaData)
167: throws SQLException {
168: int columnCount = resultSetMetaData.getColumnCount();
169: String columnNames[] = new String[columnCount];
170:
171: for (int colIndex = 1; colIndex <= columnCount; colIndex++) {
172: columnNames[colIndex - 1] = resultSetMetaData
173: .getColumnName(colIndex);
174: }
175:
176: return columnNames;
177: }
178:
179: /**
180: * The following method returns an array of strings containing the column names for
181: * a given ResultSetMetaData object. NEW CODE
182: */
183: public String[] getColumnNames(ResultSetMetaData resultSetMetaData,
184: int[] returnCols) throws SQLException {
185: int columnCount = returnCols.length; //resultSetMetaData.getColumnCount();
186: String columnNames[] = new String[returnCols.length];
187:
188: for (int colIndex = 0; colIndex < columnCount; colIndex++) {
189: columnNames[colIndex] = resultSetMetaData
190: .getColumnName(returnCols[colIndex]);
191: }
192:
193: return columnNames;
194: }
195:
196: /**
197: * The following method returns an array of objects containing the data values for
198: * each column for the current row in a given ResultSet. The columnCount parameter
199: * saves the method having to address the ResultSetMetaData to obtain the number of
200: * columns.
201: */
202: private Object[] getRowData(ResultSet resultSet, int columnCount)
203: throws SQLException {
204: Object rowData[] = new String[columnCount];
205:
206: for (int colIndex = 1; colIndex <= columnCount; colIndex++) {
207: rowData[colIndex - 1] = resultSet.getObject(colIndex);
208: }
209: return rowData;
210: }
211:
212: /*
213: public TabularResults.ResultSet toTabularResultSet(ResultSet resultSet) throws SQLException
214: {
215: Monitor mon=start("toTabularResultSet()");
216: TabularResults.ResultSet trs=null;
217: try {
218: trs = com.sybase.CORBA.jdbc11.IDL.getResultSet(resultSet);
219: }
220: finally
221: {
222: mon.stop();
223: return trs;
224: }
225: }
226:
227:
228: public ResultSet toResultSet(TabularResults.ResultSet tabularResultSet) throws SQLException
229: {
230: Monitor mon=start("toResultSet()");
231: ResultSet rs=null;
232: try {
233: rs=com.sybase.CORBA.jdbc11.SQL.getResultSet(tabularResultSet);
234: } finally
235: {
236: mon.stop();
237: return rs;
238: }
239:
240: }
241: */
242: }
|