aspose file tools*
The moose likes JDBC and the fly likes Storing BLOB file in Database. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Storing BLOB file in Database." Watch "Storing BLOB file in Database." New topic
Author

Storing BLOB file in Database.

sudhakar Tadepalli
Ranch Hand

Joined: Dec 27, 2001
Posts: 130
Hi,
I am getting an error while trying to insert a byte[] in blob field. Please let me know if I am doing somethng wrong. Here is the code.
Connection jdbcConn = null;
PreparedStatement sqlStatement = null;
//Statement stmt = null;
boolean autoCommit = true;
ResultSet sqlResults = null;
Blob imageBlob;
Blob pdfBlob;

try {

jdbcConn = XPDataSource.getConnection();
autoCommit = jdbcConn.getAutoCommit();
jdbcConn.setAutoCommit(false);
String insertSqlString =
"INSERT INTO XP_M_HOMEPAGE_CONTENT (SKEY, TOPIC,TITLE,MESSAGE,LINK_TEXT, LINK_BLOB,IMAGE_NAME) VALUES ("
+ skey
+ ",'"
+ bean.getTopic()
+ "','"
+ bean.getTitle()
+ "','"
+ bean.getMessage()
+ "','"
+ bean.getLinkText()
+ "', ?,'"
+ bean.getImageName()
+ "')";
Log.debug("", insertSqlString);
//stmt = jdbcConn.createStatement ();
sqlStatement = jdbcConn.prepareStatement(insertSqlString);

//InputStream imgInputStream = new BufferedInputStream(new FileInputStream(bean.getImageLink()));
InputStream pdfInputStream = new BufferedInputStream(new FileInputStream(bean.getPdfLink()));
//sqlStatement.setBinaryStream(1,imgInputStream,imgInputStream.available());
sqlStatement.setBinaryStream(1, pdfInputStream,pdfInputStream.available());

sqlStatement.executeUpdate();
//imgInputStream.close();
pdfInputStream.close();
jdbcConn.setAutoCommit(autoCommit);
jdbcConn.commit();
jdbcConn.close();
Reid M. Pinchback
Ranch Hand

Joined: Jan 25, 2002
Posts: 775
Aren't blobs one of the types where you first do an insert to create the server-side uninitialized object, followed by (before committing) a select to read back a client-side reference to work with?


Reid - SCJP2 (April 2002)
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Storing BLOB file in Database.