Inserting a file into a BLOB field in a database!
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Amir_Pashazadeh
Posted On:   Friday, April 4, 2003 08:33 AM

Hi, How can I insert a file in a BLOB field in a database? I wrote the following code: PreparedStatement ps = con.prepareStatement( "update mytable set image = ? where id = 1" ); // image is a BLOB field File f = new File(fileName); int fileSize = (int) f.length(); InputStream i = new FileInputStream(fileName); byte[] data = new byte[fileSize]; i.read(data, 0, fileSize - 1); preparedStatement.setBinaryStream(1, i, fileSize); int updated = preparedStatement.executeUpdate(); con.commit(); but if file size is more than 1Kb nothing will be inserted in database! I user setBinaryStr   More>>

Hi,



How can I insert a file in a BLOB field in a database?

I wrote the following code:


			
PreparedStatement ps = con.prepareStatement( "update mytable set image = ? where id = 1" );
// image is a BLOB field
File f = new File(fileName);
int fileSize = (int) f.length();
InputStream i = new FileInputStream(fileName);
byte[] data = new byte[fileSize];
i.read(data, 0, fileSize - 1);
preparedStatement.setBinaryStream(1, i, fileSize);
int updated = preparedStatement.executeUpdate();
con.commit();


but if file size is more than 1Kb nothing will be inserted in database!

I user setBinaryStream because I didn't find any class implementing Blob so I can use setBlob .


Can anyone help me inserting a file into a Blob field?


Thanx,

Amir Pashazadeh

   <<Less
About | Sitemap | Contact