ResultSet不包含任何行?

时间:2014-06-03 13:27:54

标签: java sql database oracle jdbc

我正在触发以下查询。

SELECT
    A.FIRST_NAME AS EMP_FIRST_NAME,
    A.LAST_NAME AS EMP_LAST_NAME, 
    A.EMAIL_ID AS EMP_EMAIL_ID,
    A.EMPLOYEE_TYPE AS EMP_EMPLOYEE_TYPE,
    A.SPECIAL_INSTRUCTIONS AS EMP_SPECIAL_INSTRUCTIONS, 
    B.FIRST_NAME AS HR_FIRST_NAME,
    B.LAST_NAME AS HR_LAST_NAME,
    B.FAX_NUMBER AS HR_FAX_NUMBER,  
    B.EMAIL_ID AS HR_EMAIL_ID 
FROM
    HR_EMPLOYEE A ,
    HR_REP B
WHERE
    A.HR_REP= B.REP_ID
    AND 
    A.USER_ID='FOSTER'

当我使用

在Java中解决此问题时
ResultSet rs = statement.executeQuery(query);

我应该得到一行。

所以我这样做

if(rs.next())
{
  rs.getString("xxx");
  rs.getString("yyy");
} 

rs.next()总是返回false,表示没有提取任何行。

我复制相同的查询并在Oracle编辑器中运行我得到了输出。

所以查询是正确的。为什么会这样?

请参阅Oracle中的输出

enter image description here

注意:

当我触发单个表查询,即没有连接时,rs.next()返回true。 我在Java工作了很长时间,但从未遇到过这样的问题。请帮忙。

更新

完整代码:

@Component
@Service
public class GeneralUserImp implements GeneralUserService{

    private final Logger log = LoggerFactory.getLogger(this.getClass());

    @Reference
    ConnectionProvider connectionProvider;

    public Employee getEmployee(String userId) throws Exception{
        Connection connection=null;
        Statement statement=null;
        ResultSet resultSet= null;

        try{
            connection = connectionProvider.getConnection();
            log.info("****************** connection ::>> "+connection);

            statement = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            log.info("****************** statement ::>> "+statement);

            String query = "SELECT A.FIRST_NAME AS EMP_FIRST_NAME,A.LAST_NAME AS EMP_LAST_NAME, A.EMAIL_ID AS EMP_EMAIL_ID,A.EMPLOYEE_TYPE AS EMP_EMPLOYEE_TYPE,A.SPECIAL_INSTRUCTIONS AS EMP_SPECIAL_INSTRUCTIONS," + 
                " B.FIRST_NAME AS HR_FIRST_NAME,B.LAST_NAME AS HR_LAST_NAME,B.FAX_NUMBER AS HR_FAX_NUMBER,  B.EMAIL_ID AS HR_EMAIL_ID"+
                " FROM HR_EMPLOYEE A ,HR_REP B WHERE  A.HR_REP= B.REP_ID AND A.USER_ID='FOSTER'"; // This doesn't work 

            //String query = "SELECT SPECIAL_INSTRUCTIONS FROM HR_EMPLOYEE  WHERE  USER_ID='"+userId+"'"; this works

            log.info("The Query ::"+query);
            resultSet   = statement.executeQuery(query);

        log.info("****************************************) ");         


            if(resultSet.next())
            {
                log.info("-------------------FINE-----------------------------");
                Employee employee= new Employee();              
                employee.setFirstName(resultSet.getString("EMP_FIRST_NAME"));
                employee.setLastName(resultSet.getString("EMP_LAST_NAME"));
                employee.setUserId(resultSet.getString("USER_ID"));
                employee.setSpecialInstructions(resultSet.getString("SPECIAL_INSTRUCTIONS"));
                employee.setEmailId(resultSet.getString("EMP_EMAIL_ID"));
                employee.setEmployeeType(resultSet.getInt("EMP_EMPLOYEE_TYPE"));
                employee.setOfferLetter(null);
                employee.setNonCompeteLetter(null); // Need to be changed later
                employee.setSpecialInstructions(resultSet.getString("EMP_SPECIAL_INSTRUCTIONS"));

                HRRepresentative representative = new HRRepresentative();
                representative.setFirstName(resultSet.getString("HR_FIRST_NAME"));
                representative.setLastName(resultSet.getString("HR_LAST_NAME"));
                representative.setEmailId(resultSet.getString("HR_EMAIL_ID"));
                representative.setFaxNumber(resultSet.getString("HR_FAX_NUMBER"));

                employee.setHrRepresentative(representative);

                return employee;

            }
            log.info("---------------Nothing in ResultSet------------------------------------");
            return null;


        }
        catch(Exception exception)
        {
            log.info("---------------Catch ---------------------------------");
            log.error("VideoJet Exception::", exception );
            throw exception;
        }
        finally
        {
            log.info("--------------------FINAKLYY----------------------------");
            DatabaseUtils.closeConnections(connection, statement, resultSet);
        }


    }

2 个答案:

答案 0 :(得分:1)

问题在于 ResultSet.CONCUR_UPDATABLE

根据oracle的结果集限制

  

生成可更新的结果集:

     

查询只能从一个表中进行选择,不能包含任何表   加入运营。

Please see For more information部分:结果集限制和降级规则

您可以删除该标记以继续。

答案 1 :(得分:0)

我发现了错误。 @Sanjeev你错了。我在其可用的可更新结果集中激活了连接。

这是我的错误实际

我使用toad的编辑器修改了B.REP_ID,但我没有提交。因此,当我在编辑器中触发查询时,它显示了修改后的结果(但未提交STILL数据)。但是当我使用Java JDBC解雇查询时,它显示不存在,因为我没有提交。当我提交实际数据库得到反映,然后我解雇了java querty(JDBC)它工作。

分析我们学到的是什么 但您可以使用Java在UPdatable语句中触发多个连接查询。!! @Sanjeev你所说的适用于相当旧的JDBC规范。