sql >> Base de Datos >  >> RDS >> Oracle

Cómo insertar UUID en la columna RAW (16)

Debe convertir el UUID en una matriz de bytes. Consulte el método asBytes cómo hacerlo.

Después, el enlace es tan simple como usar setBytes .

Ejemplo

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()

Aquí, por si acaso, el enlace no funciona el método de conversión UUID a matriz de bytes

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