0

I want to upload multiple objects to my oracle database using java.

My objects included { DOC, DOCX , PPT , PPTX , PDF } files.

How I can upload my objects in oracle database and retrieve it from database.

also I am interested to know more about "How can I batch insert list of objects in Oracle database from Java?" but I think this question answered before. If you have any new or interesting resource please share with me please ...

3
  • 1
    Are you using some kind of ORM, Oracle Objects or you want to persist instances in the database? Commented Mar 8, 2011 at 8:18
  • 1
    Did you want to ask, "How can I batch insert list of objects in Oracle database from Java?" Commented Mar 8, 2011 at 8:19
  • I add some note to my question,please read it again Commented Mar 8, 2011 at 8:34

2 Answers 2

3

Insert into db:

int primaryKeyId = getNextPrimaryKeyId();
PreparedStatement stmt1 = conn.prepareStatement(" insert into docTable values (?, ?, empty_blob()) ");
stmt1.setInt(1, primaryKeyId );
stmt1.setString(2, getDocumentTitle());
stmt1.executeUpdate();

PreparedStatement stmt2 = conn.prepareStatement(" select doc from docTable where id = ? for update ");
stmt2.setInt(1, primaryKeyId);
stmt2.execute();

OracleResultSet rset = (OracleResultSet)stmt2.getResultSet();
if (rset.next()) {
  BLOB document = rset.getBLOB("doc");
  document.trim(0);
  OutputStream os = document.getBinaryOutputStream();
  os.write(getDocumentToBeWrittenToDb());
  os.flush;
  os.close;
}

Read from db:

PreparedStatement stmt = conn.prepareStatement(" select title, doc from docTable where id = ?");
stmt.setInt(1, primaryKeyId);
stmt.execute();

BLOB document;
OracleResultSet rset = (OracleResultSet)stmt.getResultSet();

if (rset.next())
{
  ByteArrayOutputStream baos = null;
  InputStream is = null;
  try
  {
    BLOB document = rset.getBLOB("document");
    String title = rset.getString("title");
    is = document.getBinaryStream();
    baos = new ByteArrayOutputStream();
    byte[] data = new byte[2048];
    int len;
    while ((len = is.read(data)) != -1)
    {
      baos.write(data, 0, len);
    }
  } finally
  {
    if (null != baos) baos.close();
    if (null != is) is.close();
  }

  return baos.toByteArray();
}
Sign up to request clarification or add additional context in comments.

1 Comment

I dont have oracleResultSet, where I can find this package?
0

If you are

  1. running on Linux and the database
  2. version is 11gR2 and you have
  3. created a dbfs in it and you have
  4. mounted it using the dbfs_client

you can access the filesystem as a regular networked filesystem. Doing so is the easiest way to store non structured data in the database using regular filesystem io. The dbfs_client translated the calls using fuse. If needed you can create the relations to other data later.

Ronald.

2 Comments

I am running on Windows xp :(
In that case you might be able to re-mount the fs using smb. You still need the dbfs to be mounted on some Linux app/file server that exports it to your Windows machine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.