如果查询没有返回任何记录,结果集的值是多少?

问题描述:

public ResultObject takePrefixGroupId(ArrayList prefixGroupName) 
{ 
debugLog(MODULE_NAME, "Inside the takePrefixGroupId() of LCRConfigurationSessionBean"); 
ResultObject resultObject = new ResultObject(LCRResponseCode.LCR_CONFIGURE_SEARCH_ERROR_EJB, null); 

String strSelectQuery = null; 
String strMessage=null; 
ResultSet resSet = null; 
Collection colInValideRecord =new ArrayList(); 
Collection colErrorMessage=new ArrayList(); 
Collection colValidRecord = new ArrayList(); 
Collection colDataValidation=null; 
try{ 
    for(int i=0;i<prefixGroupName.size();i++) 
    { 
    strSelectQuery = "select DESTINATIONGROUPID from TBLMDESTINATIONGROUP where NAME='"+prefixGroupName.get(i)+"'"; 
    debugLog(MODULE_NAME, "Query::::::"+strSelectQuery); 
    resultObject = execute(strSelectQuery); 
    if(resultObject.getResponseCode() == LCRResponseCode.SUCCESS_RESPONSE_CODE) 
    { 
    resSet = (ResultSet)resultObject.getResponseObject(); 
    debugLog(MODULE_NAME, "resSet::::::"+resSet); 
    if(resSet != null) 
    { 
    while(resSet.next()) 
    { 
     colValidRecord.add(resSet.getString("DESTINATIONGROUPID")); 
    } 
    } 
    else 
    { 
    strMessage=LCRResponseCode.errorCodeToMessage(LCRResponseCode.PREFIX_GROUP_DOES_NOT_EXIST_ERROR); 
    debugLog(MODULE_NAME,"MESSAGE::: "+strMessage); 
    colErrorMessage.add(strMessage); 
    colInValideRecord.add(prefixGroupName); 
    debugLog(MODULE_NAME,"No Prefix Group is found."); 
    } 
    colDataValidation=new ArrayList(); 
    colDataValidation.add(colValidRecord); 
    colDataValidation.add(colInValideRecord); 
    colDataValidation.add(colErrorMessage); 
    resultObject.setResponseObject(colDataValidation);  
    resultObject.setResponseCode(LCRResponseCode.SUCCESS_RESPONSE_CODE); 

    } 
    else 
    { 
    debugLog(MODULE_NAME, "Unable to execute search query for in searchDestination() of LCRConfigurationSessionBean."); 
    resultObject.setResponseCode(LCRResponseCode.LCR_CONFIGURE_SEARCH_ERROR_EJB); 
    } 
    } 

} 
catch(Exception e) 
{ 
    e.printStackTrace(); 
    errorLog(MODULE_NAME, "exception in searchDestination() of LCRConfigurationSessionBean"); 
    resultObject.setResponseCode(LCRResponseCode.LCR_CONFIGURE_SEARCH_ERROR_EJB); 
    resultObject.setException(e); 
} 
return resultObject; 
} 

这是代码如果查询没有返回任何记录,结果集的值是多少?

+0

如果您想要答案,则必须重新格式化您的代码。格式化帮助,请访问http://*.com/editing-help – Jason 2010-12-09 06:15:02

+0

@Jason:完成:) – Asaph 2010-12-09 06:16:13

the javadocStatement.executeQuery()永远不会返回null。所以答案是没有行的ResultSet

如果第一次调用next()时返回false,那么可以知道ResultSet为空。

您也可以通过调用可选的isAfterLast()方法来判断。如果支持,该方法将给你一个答案,而不会将光标作为副作用前进。


我不知道是什么,答案将是你的代码,因为你调用一个方法execute其实现您没有提供。

结果集的executeQuery(字符串SQL) 抛出的SQLException执行给定的SQL语句 ,其返回单个 ResultSet对象。

参数: SQL - SQL语句发送到数据库中,通常 静态SQL SELECT语句

返回:包含由 定查询所生成数据的ResultSet对象;不能为null

抛出: SQLException - 如果发生数据库访问错误,这 方法被调用关闭的Statement 或者给定的SQL语句生成其他 任何单个ResultSet 对象

你也可以做到这一点,如:

if(resSet.last().getRow() > 0) 
{ 
resSet.first(); 
while(resSet.next()) 
{ 
    colValidRecord.add(resSet.getString("DESTINATIONGROUPID")); 
} 
} 
else 
{ 
//...