Hello I was wondering if someone knows
how to record an image to a database as BLOB type
Thanks!
Sys Admin - 28 Jun 2003 10:44 GMT
Create a prepared Statement and then feed in as the parameter the
InputStream :
File file = new File("afile");
FileInputStream fs = new FileInputStream(file);
PreparedStatement pstmt = connection.prepareStatement("insert into table
values (?)");
pstmt.setBinaryStream(1,fs,file.length());
pstmt.executeUpdate();
pstmt.close();
fs.close();
...I found this somewhare, hope it helps.
Regards
Fredy
>Hello I was wondering if someone knows
>how to record an image to a database as BLOB type
>
>Thanks!
Thomas Mueller - 28 Jun 2003 17:20 GMT
Hi,
BLOBs are very database specific... I guess you already know how to
store a binary array to the database? (otherwise, use LDBC, that deals
with database specific issues: http://ldbc.sf.net).
About converting image to byte array: The amout of data is smaller if
you have the image available as .jpg or .png. Here is a class that can
create a image from a byte array, and read the byte array from a file.
Not sure how to convert a in-memory image to .jpg or .png. This code is
from NewSQL (http://newsql.sf.net):
public class Picture extends Component {
private byte[] data;
private Image image;
Picture(String fileName) {
try {
RandomAccessFile file=new RandomAccessFile(fileName,"r");
byte b[]=new byte[(int)file.length()];
file.readFully(b);
file.close();
setData(b);
} catch(IOException e) {
e.printStackTrace();
}
}
Picture() {
}
Picture(byte[] data) {
setData(data);
}
public int getWidth() {
return image==null ? 0 : image.getWidth(this);
}
public int getHeight() {
return image==null ? 0 : image.getHeight(this);
}
byte[] getData() {
return data;
}
Image getImage() {
return image;
}
private void setData(byte data[]) {
if(data==null || data.length==0) {
return;
}
try {
Toolkit toolkit=Toolkit.getDefaultToolkit();
image=toolkit.createImage(data);
MediaTracker tracker=new MediaTracker(this);
tracker.addImage(image,0);
tracker.waitForAll();
if(!tracker.isErrorAny()) {
this.data=data;
}
} catch(Exception e) {
e.printStackTrace();
image=null;
}
}
}
> Hello I was wondering if someone knows
> how to record an image to a database as BLOB type
>
> Thanks!