0

How to insert UUID into RAW(16) column with JDBC Template? I am trying to convert UUID to Oracle Raw 16 with JDBC Template. This reference below uses prepared statement/conn, not JBDC Template.

I need to setBytes(2, with JBDC Template, after calling asBytes function.

Reference:

How to insert UUID into RAW(16) column

https://stackoverflow.com/a/51025630/15435022

"You must convert the UUID to a byte array. See the method asBytes how to do it. After it the binding is a s simple as using setBytes."

Example

def stmt = con.prepareStatement("insert into TAB_UUID (id, uuid) values (?,?)") 
    // bind
stmt.setInt(1,1)
def uuid = UUID.randomUUID()
stmt.setBytes(2,asBytes(uuid)) 
def rowCount = stmt.executeUpdate()

Here just for case the link doesn't work the conversion method UUID to byte array

public static byte[] asBytes(UUID uuid) {
  ByteBuffer bb = ByteBuffer.wrap(new byte[16]);
  bb.putLong(uuid.getMostSignificantBits());
  bb.putLong(uuid.getLeastSignificantBits());
  return bb.array();
}
mattsmith5
  • 503
  • 1
  • 7
  • 32

0 Answers0