📄 resultsetcache.java
字号:
package com.dj.db;
/**
* 缓存resultSet的结果
*
* @author dj
* @version
*/
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Vector;
public class ResultSetCache {
/**
* 列名
*/
private String[] columnNames = null;
/**
* 结果集一行的值
*/
private String[] row = null;
/**
* 所有值
*/
private Vector values = null;
/**
* 当前游标位置
*/
private int cursor = -1;
/**
* 打印控制标志
*/
private boolean printFlag = true;
/**
* 默认构造函数
*/
public ResultSetCache() {
columnNames = new String[0];
values = new Vector();
}
/**
* 构造函数,保存结果集
*
* @param rs
* 结果集
* @throws SQLException
* SQL异常
*/
public ResultSetCache(ResultSet rs) throws SQLException {
if (rs != null) {
this.saveColumnNames(rs);
this.saveTableValues(rs);
}
}
/**
* 保存列名
*
* @param rs
* 结果集
* @throws SQLException
* SQL异常
*/
private void saveColumnNames(ResultSet rs) throws SQLException {
// ------------------- 存储列名 ---------------------
ResultSetMetaData rsmd = rs.getMetaData();
int columns = rsmd.getColumnCount();
columnNames = new String[columns];
for (int i = 0; i < columns; i++)
columnNames[i] = rsmd.getColumnName(i + 1);
// ------------------ 列名存储完毕 -------------------
}
/**
* 保存结果集的值
*
* @param rs
* 结果集
* @throws SQLException
* SQL 异常
*/
private void saveTableValues(ResultSet rs) throws SQLException {
ResultSetMetaData rsmd = rs.getMetaData();
int columns = rsmd.getColumnCount();
values = new Vector();
// ------------------ 存储数据 -----------------------
while (rs.next()) {
row = new String[columns];
for (int i = 0; i < columns; i++) {
if (rsmd.getColumnTypeName(i + 1).equalsIgnoreCase("datetime")) {
String temp_date = rs.getString(i + 1);
if (temp_date == null || temp_date.equalsIgnoreCase("null"))
row[i] = "";
else
row[i] = temp_date;
} else if (rsmd.getColumnTypeName(i + 1)
.equalsIgnoreCase("bit")) {
row[i] = rs.getBoolean(i + 1) + "";
} else {
String temp_string = rs.getString(i + 1);
if (temp_string == null
|| temp_string.equalsIgnoreCase("null"))
row[i] = "";
else
row[i] = temp_string;
}
}
values.add(row);
}
// ------------------ 存储数据完毕 ---------------------
}
/**
* 游标下移
*
* @return true 成功下移 false 下移失败
*/
public boolean next() {
cursor++;
if (cursor < values.size())
return true;
else
return false;
}
/**
* 获得当前行位置
*
* @return 当前行位置
*/
public int getRow() {
return cursor;
}
/**
* 根据列号获得字符串结果
*
* @param column
* 列号
* @return 字符串结果
* @throws SQLException
* SQL 异常
*/
public String getString(int column) throws SQLException {
String[] currentRow = (String[]) values.get(cursor);
if (column < 1 || column > currentRow.length)
throw new SQLException("列数超出范围");
return currentRow[column - 1];
}
/**
* 根据列名获得字符串结果
*
* @param columnName
* 列名
* @return 字符串结果
* @throws SQLException
* SQL 异常
*/
public String getString(String columnName) throws SQLException {
for (int i = 0; i < this.columnNames.length; i++) {
if (columnNames[i].equalsIgnoreCase(columnName))
return this.getString(i + 1);
}
throw new SQLException("找不到该列名 : " + columnName);
}
/**
* 根据列号获得字符串结果
*
* @param column
* 列号
* @return 字符串结果
* @throws SQLException
* SQL 异常
*/
public String getDate(int column) throws SQLException {
String[] currentRow = (String[]) values.get(cursor);
if (column < 1 || column > currentRow.length)
throw new SQLException("列数超出范围");
return currentRow[column - 1];
}
/**
* 根据列名获得字符串结果
*
* @param columnName
* 列名
* @return 字符串结果
* @throws SQLException
* SQL 异常
*/
public String getDate(String columnName) throws SQLException {
for (int i = 0; i < this.columnNames.length; i++) {
if (columnNames[i].equalsIgnoreCase(columnName))
return this.getString(i + 1);
}
throw new SQLException("找不到该列名 : " + columnName);
}
/**
* 根据列号获得日期时间结果
*
* @param column
* 列号
* @param type
* 类型 1 "年-月-日" 2 "年-月-日 时:分" 3 "年-月-日 时:分:秒"
* @return 日期
*/
public String getDate(int column, int type) {
String date = "";
try {
date = this.getDate(column);
if (type == 1) {
if (date.indexOf(" ") != -1)
date = date.substring(0, date.indexOf(" "));
} else if (type == 2) {
if (date.lastIndexOf(":") != -1)
date = date.substring(0, date.lastIndexOf(":"));
} else if (type == 3) {
if (date.lastIndexOf(".") != -1)
date = date.substring(0, date.lastIndexOf("."));
}
} catch (SQLException sqle) {
sqle.printStackTrace();
this.print("getDate 方法产生异常! ");
}
return date;
}
/**
* 根据列名获得日期时间结果
*
* @param columnName
* 列名
* @param type
* 类型 1 "年-月-日" 2 "年-月-日 时:分" 3 "年-月-日 时:分:秒"
* @return 日期
*/
public String getDate(String columnName, int type) {
String date = "";
try {
date = this.getDate(columnName);
if (type == 1) {
if (date.indexOf(" ") != -1)
date = date.substring(0, date.indexOf(" "));
} else if (type == 2) {
if (date.lastIndexOf(":") != -1)
date = date.substring(0, date.lastIndexOf(":"));
} else if (type == 3) {
if (date.lastIndexOf(".") != -1)
date = date.substring(0, date.lastIndexOf("."));
}
} catch (SQLException sqle) {
sqle.printStackTrace();
this.print("getDate 方法产生异常! ");
}
return date;
}
/**
* 根据列号获得整型结果
*
* @param column
* 列号
* @return 整型结果
* @throws SQLException
* SQL 异常
*/
public int getInt(int column) throws SQLException {
String[] currentRow = (String[]) values.get(cursor);
if (column < 1 || column > currentRow.length)
throw new SQLException("列数超出范围");
if (currentRow[column - 1].equalsIgnoreCase(""))
currentRow[column - 1] = "0";
return Integer.parseInt(currentRow[column - 1]);
}
/**
* 根据列名获得整型结果
*
* @param columnName
* 列名
* @return 整型结果
* @throws SQLException
* SQL 异常
*/
public int getInt(String columnName) throws SQLException {
for (int i = 0; i < this.columnNames.length; i++) {
if (columnNames[i].equalsIgnoreCase(columnName))
return this.getInt(i + 1);
}
throw new SQLException("找不到该列名 : " + columnName);
}
/**
* 根据列号获得布尔结果
*
* @param column
* 列号
* @return 布尔结果
* @throws SQLException
* SQL 异常
*/
public boolean getBoolean(int column) throws SQLException {
String[] currentRow = (String[]) values.get(cursor);
if (column < 1 || column > currentRow.length)
throw new SQLException("列数超出范围");
return new Boolean(currentRow[column - 1]).booleanValue();
}
/**
* 根据列名获得布尔结果
*
* @param columnName
* 列名
* @return 布尔结果
* @throws SQLException
* SQL 异常
*/
public boolean getBoolean(String columnName) throws SQLException {
for (int i = 0; i < this.columnNames.length; i++) {
if (columnNames[i].equalsIgnoreCase(columnName))
return this.getBoolean(i + 1);
}
throw new SQLException("找不到该列名 : " + columnName);
}
/**
* 根据列号获得浮点型结果
*
* @param column
* 列号
* @return 浮点型结果
* @throws SQLException
* SQL 异常
*/
public float getFloat(int column) throws SQLException {
String[] currentRow = (String[]) values.get(cursor);
if (column < 1 || column > currentRow.length)
throw new SQLException("列数超出范围");
if (currentRow[column - 1].equalsIgnoreCase(""))
currentRow[column - 1] = "0";
return Float.parseFloat(currentRow[column - 1]);
}
/**
* 根据列名获得浮点型结果
*
* @param columnName
* 列名
* @return 浮点型结果
* @throws SQLException
* SQL 异常
*/
public float getFloat(String columnName) throws SQLException {
for (int i = 0; i < this.columnNames.length; i++) {
if (columnNames[i].equalsIgnoreCase(columnName))
return this.getFloat(i + 1);
}
throw new SQLException("找不到该列名 : " + columnName);
}
/**
* 根据列号获得双精度型结果
*
* @param column
* 列号
* @return 双精度型结果
* @throws SQLException
* SQL 异常
*/
public double getDouble(int column) throws SQLException {
String[] currentRow = (String[]) values.get(cursor);
if (column < 1 || column > currentRow.length)
throw new SQLException("列数超出范围");
if (currentRow[column - 1].equalsIgnoreCase(""))
currentRow[column - 1] = "0";
return Double.parseDouble(currentRow[column - 1]);
}
/**
* 根据列名获得双精度型结果
*
* @param columnName
* 列名
* @return 双精度型结果
* @throws SQLException
* SQL 异常
*/
public double getDouble(String columnName) throws SQLException {
for (int i = 0; i < this.columnNames.length; i++) {
if (columnNames[i].equalsIgnoreCase(columnName))
return this.getDouble(i + 1);
}
throw new SQLException("找不到该列名 : " + columnName);
}
/**
* 根据列号获得长整型结果
*
* @param column
* 列号
* @return 长整型结果
* @throws SQLException
* SQL 异常
*/
public long getLong(int column) throws SQLException {
String[] currentRow = (String[]) values.get(cursor);
if (column < 1 || column > currentRow.length)
throw new SQLException("列数超出范围");
if (currentRow[column - 1].equalsIgnoreCase(""))
currentRow[column - 1] = "0";
return Long.parseLong(currentRow[column - 1]);
}
/**
* 根据列名获得长整型结果
*
* @param columnName
* 列名
* @return 长整型结果
* @throws SQLException
* SQL 异常
*/
public long getLong(String columnName) throws SQLException {
for (int i = 0; i < this.columnNames.length; i++) {
if (columnNames[i].equalsIgnoreCase(columnName))
return this.getLong(i + 1);
}
throw new SQLException("找不到该列名 : " + columnName);
}
private void print(String s) {
if (this.printFlag)
System.out.println(s);
}
public String toString() {
return "ResultSetCache : columns -> " + columnNames.length
+ " rows -> " + values.size();
}
public String[] getColumnNames() {
return this.columnNames;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -