Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / Databases / June 2003

Tip: Looking for answers? Try searching our database.

Binary stream - to database record

Thread view: 
Daniel Leontiev - 28 Jun 2003 03:30 GMT
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!


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.