The Artima Developer Community
Sponsored Link

Java Answers Forum
Storing and Retrieving PDFs in Oracle

3 replies on 1 page. Most recent reply: Aug 30, 2006 10:53 AM by Viswanatha Basavalingappa

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
Gitesh Gohil

Posts: 2
Nickname: gitesh
Registered: Aug, 2006

Storing and Retrieving PDFs in Oracle Posted: Aug 8, 2006 7:59 AM
Reply to this message Reply
Advertisement
Hi All,

I am struggling to store/retrieve PDF in Oracle database using Java.

Please, help me.

Thanks in advance,
Gitesh


Viswanatha Basavalingappa

Posts: 84
Nickname: viswagb
Registered: Nov, 2003

Re: Storing and Retrieving PDFs in Oracle Posted: Aug 23, 2006 1:19 PM
Reply to this message Reply
Hints,

Use
- BLOB Data type to store the PDF in Oracle
- FileInputStream to read/write file

let me know if u need more info

Gitesh Gohil

Posts: 2
Nickname: gitesh
Registered: Aug, 2006

Re: Storing and Retrieving PDFs in Oracle Posted: Aug 23, 2006 10:46 PM
Reply to this message Reply
Hi Viswanatha,

Thanks for the reply. I have already followed the steps you have mentioned. But its not working. Its throwing "java.sql.SQLException: Io exception: Connection reset " exception.

I even tried using CLOB, but it stores some junk numbers into DB and while retrieving it is not converting into original document.

Hope I have provided enough information of the issue.

Waiting for your response.
Thanks in Advance.
Gitesh

Viswanatha Basavalingappa

Posts: 84
Nickname: viswagb
Registered: Nov, 2003

Re: Storing and Retrieving PDFs in Oracle Posted: Aug 30, 2006 10:53 AM
Reply to this message Reply
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.DriverManager;
import java.sql.CallableStatement;
import oracle.sql.BLOB;
// IO Imports
import java.io.Writer;
import java.io.File;
import java.io.FileInputStream;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileNotFoundException;



public class TestBLOB {

// Variable for reading id value.
static String id = null;
// Variable to read the filename
static String fileName = null;
// Create connection object.
static Connection conn = null;
/*
* Default Constuctor that takes no arguments.
*/
public TestBLOB() {
}
/*
* Constuctor that takes in values of id, filename and streaming flag.
*/
public TestBLOB(String idn, String filename, String flag ) {
id = idn;
fileName = filename;
}

public static void main (String args [])
throws SQLException, FileNotFoundException {

TestBLOB testBlob = new TestBLOB(args[0], args[1]);
testBlob.getConnection();
testBlob.callUpdate(id, fileName);

}
}

/*
* Method that gets database connection and returns the same.
*/
public void getConnection() throws SQLException{
// Load Oracle driver.
DriverManager.registerDriver (new oracle.jdbc.OracleDriver());
try {
// Connect to the database.
conn = DriverManager.getConnection
("jdbc:oracle:thin:@lcw2k7:1531:or920",
"vbasaval", "vbasaval");
} catch (SQLException sqlex) {
System.out.println("SQLException while getting db connection: "+sqlex);
if (conn != null) {
conn.close();
}
} catch (Exception ex) {
System.out.println("Exception while getting db connection: "+ex);
if (conn != null) {
conn.close();
}
}
}


/*
* Method that calls update stored procedure on the database. This will use
* the getCLOB method() and oracle.sql.BLOB class to create the CLOB object
* before passing it to the procedure.
*/
public void callUpdate(String id, String filename)
throws SQLException {

CallableStatement cs = null;
BLOB blob = null;
String blobData = null;
try {
// Read the file whose content has to be updated in the BLOB column.
String lineSep = System.getProperty("line.separator");
BufferedReader br = new BufferedReader(new FileReader(filename));
String nextLine = "";
StringBuffer sb = new StringBuffer();
while ((nextLine = br.readLine()) != null) {
sb.append(nextLine);
sb.append(lineSep);
}
// Convert the content to string.
blobData = sb.toString();

// call Stored DB procedure for updating CLOB column.
cs = (CallableStatement)
conn.prepareCall( "begin updateStory(?,?); end;" );

// create the CLOB object
blob = getCLOB(blobData);


// set id
cs.setObject(1, id );

//set clob data
cs.setObject(2, blob );

// Execute callable statement.
cs.execute();

// Close the Statement object
cs.close();

} catch ( SQLException ex ) {
System.out.println("SQLException status : " + ex.getMessage( ) );
} catch ( Exception ex ) {
System.out.println("some exception " + ex.getMessage( ) );

} finally {
try {
// Close Statement
if ( cs != null ) {
cs.close( );
}

// Free BLOB
if ( blob != null ) {
blob.freeTemporary();
}
// Close connection
if (conn !=null) {
conn.close();
}
} catch ( Exception ex ) {
System.out.println("Some exception in callUpdate method of given "
+ "status : " + ex.getMessage( ) );

}
}
}

/*
* This method creates BLOB object using temporary clob and returns the same.
*/
private BLOB getBLOB( String blobData )
throws Exception {
CLOB tempClob = null;
try {
// create a new temporary BLOB
tempBlob = BLOB.createTemporary( conn, true, BLOB.DURATION_SESSION );
// Open the temporary BLOB in readwrite mode to enable writing
tempBlob.open( BLOB.MODE_READWRITE );
// Get the output stream to write
Writer tempBlobWriter = tempBlob.getCharacterOutputStream( );
// Write the data into the temporary BLOB
tempBlobWriter.write( blobData );
// Flush and close the stream
tempBlobWriter.flush( );
tempBlobWriter.close( );
// Close the temporary BLOB
tempBlob.close( );
} catch ( Exception exp ) {
tempBlob.freeTemporary();
System.out.println("Exception thrown in getBLOB method "
+ "of given status : "
+ exp.getMessage( ) );
}
return tempBlob;
}
}
}

Flat View: This topic has 3 replies on 1 page
Topic: reading weblinks used ina pdf file Previous Topic   Next Topic Topic: Event Handlers

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use