I database Oracle usano il tipo CLOB per la memorizzazione di testi estremamente lunghi (superiori ai 4000 caratteri nella versione 10), la memorizzazione di una stringa Java in questi campi non è possibile direttamente e richiede dunque una conversione.
Di seguito il codice per la conversione:
public static CLOB getCLOB( String clobData )
{
try{
return getCLOB(createConnection(), clobData);
}catch ( Exception exp ) {
//TODO: Stampa dell’errore
return null;
}
}
public static CLOB getCLOB(Connection connection, String clobData )
throws Exception {
CLOB tempClob = null;
try {
// crea un nuovo CLOB temporaneo
tempClob = CLOB.createTemporary(connection, true, CLOB.DURATION_SESSION );
// Aple il CLOB temporaneo in modalità read/write per abilitarne la scirttura
tempClob.open( CLOB.MODE_READWRITE );
Writer tempClobWriter = tempClob.getCharacterOutputStream( );
tempClobWriter.write( clobData );
tempClobWriter.flush( );
//Chiude lo stream
tempClobWriter.close( );
// Chiude il CLOB temporaneo
tempClob.close( );
} catch ( Exception exp ) {
// Rilascia l’oggetto CLOB
tempClob.freeTemporary( );
//TODO
}
//Restituisce il clob elaborato
return tempClob;
}