This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes JDBC and Relational Databases 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

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Storing BLOB file in Database." Watch "Storing BLOB file in Database." New topic

Storing BLOB file in Database.

sudhakar Tadepalli
Ranch Hand

Joined: Dec 27, 2001
Posts: 130
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();
String insertSqlString =
+ 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, pdfInputStream,pdfInputStream.available());

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)
I agree. Here's the link:
subject: Storing BLOB file in Database.
It's not a secret anymore!