📄 resultsetdatalist.java
字号:
package util.database.datalist;
import java.sql.*;
/**
* ResultSetDataList实现了DataList接口,针对ResultSet使用。
*
* 表示一个查询结果ResultSet的集和。
* @author Michael Zeng
* @version 1.0 September 20, 2002
*/
public class ResultSetDataList implements DataList
{
private RowMapper mapper = null;
private Connection conn = null;
private ResultSet rs = null;
//记录ResultSet当前位置信息的三个变量
private boolean currIsBefore = false, currIsAfter = false;
private int currIndex = 0;
/**
* package构造函数
*
* @param mapper 用于取得结果值的RowMapper对象
* @param conn 数据库连接
* @param rs 查询结果集
*/
ResultSetDataList(RowMapper mapper, Connection conn, ResultSet rs)
{
this.mapper = mapper;
this.conn = conn;
this.rs = rs;
}
/**
* 取出指定位置查询结果中的字段值,放到一个字符串数组中并返回。
* 功能类似于java.util.List.get(int)
*
* @param index 查询结果的索引
* @return String[] 结果中的字段值数组
*
* @throws SQLException
*/
public String[] get(int index) throws SQLException
{
if(index < 0 ) throw new IndexOutOfBoundsException("指定的索引越界!");
rs.absolute(index + 1);
if(rs.getRow() != (index +1)) throw new IndexOutOfBoundsException();
try{ return mapper.rowMap(rs); }
catch (SQLException ex)
{ throw new SQLException("读取字段值产生异常! " + ex.getMessage()); }
}
/**
* 检查查询结果的集和是否为空集合
*
* @return boolean true表示空集合
* @throws SQLException
*/
public boolean isEmpty() throws SQLException
{
boolean isNotBefore = !rs.isBeforeFirst();
boolean isNotAfter = !rs.isAfterLast();
//rs.getRow()==0表示没有当前行
boolean hasNoCurr = (rs.getRow()==0 ? true: false);
return isNotBefore && isNotAfter && hasNoCurr;
}
/**
* 检查是否还有下一个查询结果
*
* @return boolean true表示有下一个
* @throws SQLException
*/
public boolean hasNext() throws SQLException
{
return !rs.isLast() && !rs.isAfterLast() && (rs.getRow() !=0 || rs.isBeforeFirst());
}
/**
* 检查在指定位置上是否有查询结果
*
* @param index 查询结果的索引
* @return boolean true表示有查询结果
* @throws SQLException
*/
public boolean isElementExist(int index) throws SQLException
{
if(index < 0 ) throw new IndexOutOfBoundsException("指定的索引越界!");
//保存的当前位置信息
this.currPosition();
//ResultSet的索引从1开始
boolean isExist = rs.absolute(index + 1);
//返回刚才的位置
this.moveToCurrPosition();
return isExist;
}
/**
* 把游标放到指定的位置上,功能类似于java.sql.ResultSet.absolute(int)
*
* @param index 指定的位置,从0开始
* @return boolean true表示操作成功
* @throws SQLException
*/
public boolean absolute(int index) throws SQLException
{
if(index < 0 ) throw new IndexOutOfBoundsException("指定的索引越界!");
//ResultSet的索引从1开始
return rs.absolute(index + 1);
}
/**
* 把游标放到查询结果集ResultSet的最前面
*
* @throws SQLException
*/
public void beforeFirst() throws SQLException
{
rs.beforeFirst();
}
/**
* 把游标放到查询结果的第一个,功能类似于java.sql.ResultSet.first()
*
* @return boolean true表示移动成功
* @throws SQLException
*/
public boolean first() throws SQLException
{
return rs.first();
}
/**
* 把游标放到查询结果的最后一个,功能类似于java.sql.ResultSet.last()
*
* @return boolean true表示移动成功
* @throws SQLException
*/
public boolean last() throws SQLException
{
return rs.last();
}
/**
* 取得整个查询结果的大小,功能类似于java.util.List.size()
*
* @return size 查询结果的大小
* @throws SQLException
*/
public int size() throws SQLException
{
//保存的当前位置信息
this.currPosition();
rs.last();
int size = rs.getRow();
//返回刚才的位置
this.moveToCurrPosition();
return size;
}
/**
* 提供一个可以遍历查询结果的对象,功能类似于java.util.List.iterator()
*
* @return DataListIterator 可以遍历查询结果的对象
* @throws Exception
*/
public DataListIterator iterator() throws Exception
{
return new ResultSetDataListIterator(this);
}
/**
* helper方法,保存ResultSet的当前位置信息。和moveToCurrPosition()配合使用。
* @throws SQLException
* @see moveToCurrPosition()
*/
private void currPosition() throws SQLException
{
this.currIsBefore = rs.isBeforeFirst();
this.currIsAfter = rs.isAfterLast();
this.currIndex = rs.getRow();
}
/**
* helper方法,返回ResultSet刚才的位置。和currPosition()配合使用。
* @throws SQLException
* @see currPosition()
*/
private void moveToCurrPosition() throws SQLException
{
if(this.currIsBefore) rs.beforeFirst();
else if(this.currIsAfter) rs.afterLast();
else if(this.currIndex != 0) rs.absolute(this.currIndex);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -