Pages

Tuesday 8 January 2008

Saving into a blob

I have noticed that many programmers especially those very much accustomed to programming in event driven languages like Microsoft's Visual Basic have a hard time extracting or saving stuff like images, word documents, PDFs and so on in Java. This is due to the profound differences in JDBC and OLE.

However, I have developed a simple solution that has worked well with MySQL Coonnector/J and of course MySQL database server. Here is the code;

File in = new File("C:/Duke.jpg");
int fileLength = (int) in.length();
InputStream streamFile = new FileInputStream(in);
pstmt.setBinaryStream(1, streamFile, fileLength);

and wala, that's it. pstmt is an established PreparedStatement.

No comments: