在我们的Struts2应用程序中,要获取数据,我们结果集已关闭。我们正在使用一种结构来获取重新绑定数据。我们正在使用 BaseResultSetHandler 获取DAO文件中的重新设置数据。我无法找到关闭结果集的原因。在下面的内容中,我提到的代码可以帮助我们
我们有BaseResultSetHandler抽象类
public abstract class BaseResultSetHandler {
protected ResultSet resultSet;
public abstract Object handleResult() throws SQLException;
public Object handleResultSet(ResultSet rs) throws SQLException{
setResultSet(rs);
Object obj = handleResult();
return obj;
}
public void setResultSet(ResultSet resultSet) {
this.resultSet = resultSet;
}
public ResultSet getResultSet() {
return resultSet;
}
public Integer getInteger(String columnName,ResultSet resultSet) throws SQLException{
return StringUtility.getInt(resultSet.getInt(columnName));
}
public String getString(String columnName,ResultSet resultSet) throws SQLException{
return StringUtility.getString(resultSet.getString(columnName));
}
public String getString(String columnName) throws SQLException{
return StringUtility.getString(resultSet.getString(columnName));
}
}
我们有一个DatabaseManger类,用于处理数据库操作,因为我们具有从数据库中获取数据的功能
protected Object executePreparedQuery(String query, List arguments, BaseResultSetHandler BaseResultSetHandler, Connection useThisConnection)
throws SQLException, InstantiationException, Exception, Throwable
{
ResultSet resultSet = null;
try {
if (ReferenceCheckUtlity.isNull(useThisConnection)) {
this.connection = getConnection();
}else{
this.connection = useThisConnection;
}
this.preparedStatement = this.connection.prepareStatement(query);
if (arguments != null) {
Iterator argumentsIterator = arguments.iterator();
int index = 1;
while (argumentsIterator.hasNext()) {
this.preparedStatement.setObject(index++, argumentsIterator.next());
}
}
resultSet = this.preparedStatement.executeQuery();
Object localObject2 = BaseResultSetHandler.handleResultSet(resultSet);
} finally {
if(resultSet!=null){
resultSet.close();
}
if(this.statement!=null){
this.statement.close();
}
if(this.preparedStatement!=null){
this.preparedStatement.close();
}
if(this.connection!=null){
this.connection.close();
}
}
return localObject2;
}
在DAO文件中,我仅共享2种方法,这里使用 BaseResultSetHandler
的文件在大约25个以上的类中有很多方法 public String getCheckId(String Id) throws NumberFormatException, SQLException, InstantiationException, Exception, Throwable {
ArrayList<Object> argumentList=new ArrayList<Object>();
argumentList.add(Id);
int cnt=Integer.parseInt(""+executePreparedQuery(getCheckIdSql(), argumentList, new getCheckIdHandler(), null));
if(cnt==0){
return "N";
}else{
return "Y";
}
}
private String getCheckIdSql() {
StringBuffer query=new StringBuffer();
query.append("select count(1) as cnt from EMPLOYEES where ID=? for read only ");
return query.toString();
}
class getCheckIdHandler extends BaseResultSetHandler {
@Override
public Object handleResult() throws SQLException {
int cnt=0;
while(resultSet.next()){
cnt=getInteger("cnt");
}
return cnt;
}
}
public HashMap<String,String> getMajorData() throws InstantiationException, SQLException, Exception, Throwable{
LinkedHashMap<String,String> list = new LinkedHashMap<String,String>();
list=(LinkedHashMap<String, String>) executePreparedQuery(getMajorDataSql(), null, new getMajorDataHandler(), null);
return list;
}
private String getMajorDataSql() {
StringBuffer query=new StringBuffer();
query.append("select RMH_ode,MH_Name from RECMH order by MH_NM for read only");
return query.toString();
}
class getMajorDataHandler extends BaseResultSetHandler {
@Override
public Object handleResult() throws SQLException {
LinkedHashMap<String,String> list = new LinkedHashMap<String,String>();
while(resultSet.next()){
String RMH_CD=resultSet.getString("RMH_CD");
String MH_NM=resultSet.getString("MH_Name");
list.put(RMH_CD+"*"+MH_Name, RMH_CD+"-"+MH_Name);
}
return list;
}
}
我还必须知道它正在共享 BaseResultSetHandler resultSet对象