views:

112

answers:

2

I have a Java program that creates a number of xml file and then zips them up and saves them to the file system. Later in the program I want to put that same zip file into a blob column of my oracle database. Problem is I'm not sure how to do that. I don't need to read it or do anything with the data, just move it to the database for persistent, central storage.

Thanks!

A: 

I think you're looking to use the setBytes() method :

So an example for a table representing e-mails where the body is a stream of bytes would look like:

      PreparedStatement ps = con.prepareStatement(
        "INSERT INTO Image (ID, Subject, Body) VALUES (2,?,?)");
      ps.setString(1, subject);
      byte[] bodyIn = {(byte)0xC9, (byte)0xCB, (byte)0xBB,
        (byte)0xCC, (byte)0xCE, (byte)0xB9,
        (byte)0xC8, (byte)0xCA, (byte)0xBC,
        (byte)0xCC, (byte)0xCE, (byte)0xB9,
        (byte)0xC9, (byte)0xCB, (byte)0xBB};
      ps.setBytes(2, bodyIn);
      int count = ps.executeUpdate();
      ps.close();

I'm assuming you can easily convert call getBytes() for your Zipped XML object.

Amir Afghani
+1  A: 

There are several ways you can do this, but PreparedStatement.setBinaryStream is probably the best way.

public void saveFileToDatabase(File file) {
  InputStream inputStream = new FileInputStream(file);

  Connection conn = ...;
  PreparedStatement pS = conn.prepareStatement(...);
  ...
  pS.setBinaryStream(index, inputStream, (int) file.length());
  ...
  pS.executeUpdate();
}

(Note that for simplicity I didn't include any of the necessary try/catch stuff for closing the Connection, PreparedStatement and InputStream, but you would need to do that.)

Done this way, the data will be streamed from the file to the database without having to be loaded in to memory all at once.

ColinD
I tried this (error handling removed):public void updateBlob( String id, File sf){ InputStream is = new FileInputStream(sf); String sql = "INSERT INTO tname ('bCol') " + "VALUES (?) WHERE tnameId=?"; PreparedStatement stmt = null; Connection conn = SqlUtil.getCurrentConnection(); stmt = conn.prepareStatement( sql ); stmt.setBinaryStream(1, is, sf.length() ); stmt.setString( 2, id); stmt.executeUpdate();Got Error:java.lang.AbstractMethodError: org.apache.commons.dbcp.DelegatingPreparedStatement.setBinaryStream(ILjava/io/InputStream;J)V
Ironosity
Try casting the `long` result of `sf.length()` to an `int`... there was a version of `setBinaryStream` that took an `int` length, and then JDBC 4.0 added one that takes a `long` length. The 4.0 method might be being called against a 3.0 library. If that doesn't work, check that both Commons DBCP and the Oracle driver are versions that support at least JDBC 3.0.
ColinD