Search code examples
javaoracle-databaseplsqlrowid

Getting java.sql.RowId from Java ResultSet


This is proving to be more difficult than I expected. I am trying to get the Oracle rowid (java.sql.RowId) for a row from a ResultSet object.

RowId rowid = rs.getRowId("rowid");

this fails, doesn't like the input String "rowid".

Integer columnIndex = 2;
RowId rowid = rs.getRowId(columnIndex);

this fails because it doesn't like the integer value of the column index.

So which column index do I pass for a metacolumn such as rownum or rowid?

If I were the designers, I would have made rowid have a columnindex of 0, -1, or -2, or -3, or something, but that's me.


Solution

  • It looks like you need to select the rowid as a column in your query, then access the column (by name or index) with the getRowId() method.

    For example.

        select
            rowid,
            blammy
        from
            tablename
        where
            something = desiredvalue
    

    Then

        RowId rowid = rs.getRowId("rowid");
    

    or

        RowId rowid = rs.getRowId(1);