Register now and start sharing your code snippets.
-->
How to write UTF-8 data to an Oracle BLOB column with Java and JDBC
Java posted about 1 year ago by christian
This example works with Oracle:
1 private Blob getBlob(Connection connection, String data) 2 { 3 BLOB blob = BLOB.createTemporary(connection, true, BLOB.DURATION_SESSION); 4 5 try 6 { 7 blob.open(BLOB.MODE_READWRITE); 8 blob.putBytes(1, data.getBytes("UTF-8")); // Consider streaming, if data size is unknown. Note that setBytes doesn't work 9 } 10 catch(UnsupportedEncodingException ex) 11 { 12 throw new RuntimeException("Unable to get a blob for '" + data + "'", ex); 13 } 14 catch(SQLException ex) 15 { 16 throw new RuntimeException("Unable to get a blob for '" + data + "'", ex); 17 } 18 finally 19 { 20 try { if(blob != null) blob.close(); } catch(Exception ex) {}; 21 } 22 }
Then use the method like this:
1 Connection connection = getConnection(); 2 PreparedStatement statement = getPreparedStatement(yer sequel); 3 4 statement.setBlob(1, getBlob(connection, <Mao's Little Red Book>));