Insert File JDBC

Insert File JDBC

To store a file in Oracle table though JDBC (Insert File JDBC)

Before going into execution, create table by name office with columns FILEID (number) and FILENAME (CLOB).

Example on Insert File JDBC

To write to a CLOB (Character Large Object) data type, The PreparedStatement interface comes with three methods.

  1. void setCharacterStream(int parameterIndex, Reader reader) throws SQLException
  2. void setCharacterStream(int parameterIndex, Reader reader, int length) throws SQLException
  3. void setCharacterStream(int parameterIndex, Reader reader, long length) throws SQLException

In the above code, the second method is used.

pst.setCharacterStream(2, fr1, (int) f1.length());

f1.length() returns the size of the file as a long data type which casted to int data type. fr1 represents FileReader object holding the reference of expenditure.txt file. Otherway to say, to write the complete file.

For storing images, setBinaryStream() method is to be used and to retrieve image getBinaryStream().

View All JDBC Examples

Leave a Reply

Your email address will not be published. Required fields are marked *