how upload object in oracle database by java
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 retrie开发者_高级运维ve 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 ...
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();
}
If you are
- running on Linux and the database
- version is 11gR2 and you have
- created a dbfs in it and you have
- 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.
精彩评论