📄 data.java
字号:
*/
public boolean hasHeader(String column) {
return data.containsKey(column);
}
/**
* Sets the current position of the Data set to the next row.
*
* @return True if there is another row. False if there are no more rows.
*/
public boolean next() {
return (++currentPos < size);
}
/**
* Gets a Data object from a ResultSet.
*
* @param rs
* ResultSet passed in from a database query
* @return a Data object
* @throws java.sql.SQLException
*/
public static Data getDataFromResultSet(ResultSet rs) throws SQLException {
ResultSetMetaData meta = rs.getMetaData();
Data data = new Data();
int numColumns = meta.getColumnCount();
String[] dbCols = new String[numColumns];
for (int i = 0; i < numColumns; i++) {
dbCols[i] = meta.getColumnName(i + 1);
data.addHeader(dbCols[i]);
}
while (rs.next()) {
data.next();
for (int i = 0; i < numColumns; i++) {
Object o = rs.getObject(i + 1);
if (o instanceof byte[]) {
o = new String((byte[]) o);
}
data.addColumnValue(dbCols[i], o);
}
}
return data;
}
/**
* Sets the current position of the Data set to the previous row.
*
* @return True if there is another row. False if there are no more rows.
*/
public boolean previous() {
return (--currentPos >= 0);
}
/**
* Resets the current position of the data set to just before the first
* element.
*/
public void reset() {
currentPos = -1;
}
/**
* Gets the value in the current row of the given column.
*
* @param column
* name of the column.
* @return an Object which holds the value of the column.
*/
public Object getColumnValue(String column) {
try {
if (currentPos < size) {
return ((List) data.get(column)).get(currentPos);
} else {
return null;
}
} catch (Exception e) {
return null;
}
}
/**
* Gets the value in the current row of the given column.
*
* @param column
* index of the column (starts at 0).
* @return an Object which holds the value of the column.
*/
public Object getColumnValue(int column) {
String columnName = (String) header.get(column);
try {
if (currentPos < size) {
return ((List) data.get(columnName)).get(currentPos);
} else {
return null;
}
} catch (Exception e) {
return null;
}
}
public Object getColumnValue(int column, int row) {
setCurrentPos(row);
return getColumnValue(column);
}
public void removeColumn(int col) {
String columnName = (String) header.get(col);
data.remove(columnName);
header.remove(columnName);
}
/**
* Sets the headers for the data set. Each header represents a column of
* data. Each row's data can be gotten with the column header name, which
* will always be a string.
*
* @param h
* array of strings representing the column headers.
*/
public void setHeaders(String[] h) {
int x = 0;
header = new ArrayList(h.length);
for (x = 0; x < h.length; x++) {
header.add(h[x]);
data.put(h[x], new ArrayList());
}
}
/**
* Returns a String array of the column headers.
*
* @return array of strings of the column headers.
*/
public String[] getHeaders() {
String[] r = new String[header.size()];
if (r.length > 0) {
r = (String[]) header.toArray(r);
}
return r;
}
public int getHeaderCount(){
return header.size();
}
/**
* This method will retrieve every entry in a certain column. It returns an
* array of Objects from the column.
*
* @param columnName
* name of the column.
* @return array of Objects representing the data.
*/
public List getColumnAsObjectArray(String columnName) {
return (List) data.get(columnName);
}
/**
* This method will retrieve every entry in a certain column. It returns an
* array of strings from the column. Even if the data are not strings, they
* will be returned as strings in this method.
*
* @param columnName
* name of the column.
* @return array of Strings representing the data.
*/
public String[] getColumn(String columnName) {
String[] returnValue;
Object o;
List temp = (List) data.get(columnName);
if (temp != null) {
returnValue = new String[temp.size()];
Iterator it = temp.iterator();
int index = 0;
while (it.hasNext()) {
o = it.next();
if (o != null) {
if (o instanceof String) {
returnValue[index++] = (String) o;
} else {
returnValue[index++] = o.toString();
}
}
}
} else {
returnValue = new String[0];
}
return returnValue;
}
/**
* Use this method to set the entire data set. It takes an array of strings.
* It uses the first row as the headers, and the next rows as the data
* elements. Delimiter represents the delimiting character(s) that separate
* each item in a data row.
*
* @param contents
* array of strings, the first element is a list of the column
* headers, the next elements each represent a single row of
* data.
* @param delimiter
* the delimiter character that separates columns within the
* string array.
*/
public void setData(String[] contents, String delimiter) {
setHeaders(JOrphanUtils.split(contents[0], delimiter));
int x = 1;
while (x < contents.length) {
setLine(JOrphanUtils.split(contents[x++], delimiter));
}
}
/*
* Deletes a header from the Data object. Takes the column name as input. It
* will delete the entire column.
*
* public void deleteHeader(String s) {
* }
*/
/**
* Sets the data for every row in the column.
*/
public void setColumnData(String colName, Object value) {
List list = this.getColumnAsObjectArray(colName);
while (list.size() < size()) {
list.add(value);
}
}
public void setColumnData(int col, List data) {
reset();
Iterator iter = data.iterator();
String columnName = (String) header.get(col);
while (iter.hasNext()) {
next();
setColumnValue(columnName, iter.next());
}
}
/**
* Adds a header name to the Data object.
*
* @param s
* name of header.
*/
public void addHeader(String s) {
header.add(s);
data.put(s, new ArrayList(Math.max(size(), 100)));
}
/**
* Sets a row of data using an array of strings as input. Each value in the
* array represents a column's value in that row. Assumes the order will be
* the same order in which the headers were added to the data set.
*
* @param line
* array of strings representing column values.
*/
public void setLine(String[] line) {
List tempList;
String[] h = getHeaders();
for (int count = 0; count < h.length; count++) {
tempList = (List) data.get(h[count]);
if (count < line.length && line[count].length() > 0) {
tempList.add(line[count]);
} else {
tempList.add("N/A");
}
}
size++;
}
/**
* Sets a row of data using an array of strings as input. Each value in the
* array represents a column's value in that row. Assumes the order will be
* the same order in which the headers were added to the data set.
*
* @param line
* array of strings representing column values.
* @param deflt
* default value to be placed in data if line is not as long as
* headers.
*/
public void setLine(String[] line, String deflt) {
List tempList;
String[] h = getHeaders();
for (int count = 0; count < h.length; count++) {
tempList = (List) data.get(h[count]);
if (count < line.length && line[count].length() > 0) {
tempList.add(line[count]);
} else {
tempList.add(deflt);
}
}
size++;
}
/**
* Returns all the data in the Data set as an array of strings. Each array
* gives a row of data, each column separated by tabs.
*
* @return array of strings.
*/
public String[] getDataAsText() {
StringBuffer temp = new StringBuffer("");
String[] line = new String[size + 1];
String[] elements = getHeaders();
for (int count = 0; count < elements.length; count++) {
temp.append(elements[count]);
if (count + 1 < elements.length) {
temp.append("\t");
}
}
line[0] = temp.toString();
reset();
int index = 1;
while (next()) {
temp = new StringBuffer("");
for (int count = 0; count < elements.length; count++) {
temp.append(getColumnValue(count));
if (count + 1 < elements.length) {
temp.append("\t");
}
}
line[index++] = temp.toString();
}
return line;
}
public String toString() {
String[] contents = getDataAsText();
StringBuffer sb = new StringBuffer();
boolean first = true;
for (int x = 0; x < contents.length; x++) {
if (!first) {
sb.append("\n");
} else {
first = false;
}
sb.append(contents[x]);
}
return sb.toString();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -