Shubham Sahay
Shubham Sahay

Reputation: 88

fetch row id from oracle using Java

I need to fetch rowid from Oracle database using Java. I have used the below code.

Statement st = con.createStatement();
ResultSet rs=st.executeQuery("select * from ABC");
while(rs.next()) {
            RowId str=rs.getRowId(1);        
            System.out.println(str);
        }

The result I am getting is

oracle.sql.ROWID@2ce6c6ec
oracle.sql.ROWID@1bae316d

but I need hexadecimal values like

AAAdItACwAABXcIAAA

Can anyone guide me on this?

thanks

Upvotes: 2

Views: 1891

Answers (2)

Nipuna Priyamal
Nipuna Priyamal

Reputation: 368

You can use https://docs.oracle.com/javase/7/docs/api/javax/xml/bind/DatatypeConverter.html#printHexBinary%28byte%5B%5D%29

This will directly provide you the hexadecimal value

String hexValue = DatatypeConverter.printHexBinary(resultSet.getRowId(10).getBytes())

Here is the full code

Statement st = con.createStatement();
ResultSet rs=st.executeQuery("select * from ABC");
while(rs.next()) 
{
    String str= DatatypeConverter.printHexBinary(resultSet.getRowId(1).getBytes())       
    System.out.println(str);
}

Upvotes: 3

Yugansh
Yugansh

Reputation: 385

From the Java Docs

Retrieving RowId Objects Retrieve a java.sql.RowId object by calling the getter methods defined in the interfaces ResultSet and CallableStatement. The RowId object that is returned is an immutable object that you can use for subsequent referrals as a unique identifier to a row. The following is an example of calling the ResultSet.getRowId method:

java.sql.RowId rowId_1 = rs.getRowId(1);

String toString() Returns a String representing the value of the SQL ROWID designated by this java.sql.RowId object.

Upvotes: 0

Related Questions