ibatis-user-java mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Javier Urbaneja" <urbanej...@gmail.com>
Subject Re: Ibatis and the blob
Date Mon, 18 Sep 2006 15:56:14 GMT
Try something like this

import java.sql.*;
import java.io.*;

public class BlobHandler {

    public static void main(String args[]) {
        Connection con = null;
        if (args.length < 5) {
            System.err.println("Syntax: <java BlobHandler [driver] [url] "
                    + "[uid] [pass] [file]");
            return;
        }
        try {
            Class.forName(args[0]).newInstance();
            con = DriverManager.getConnection(args[1], args[2], args[3]);
            File f = new File(args[4]);
            PreparedStatement stmt;
            if (!f.exists()) {
                // if the file does not exist
                // retrieve it from the database and write it
                // to the named file
                ResultSet rs;
                stmt = con.prepareStatement("SELECT blobData "
                        + "FROM BlobTest " + "WHERE fileName = ?");
                stmt.setString(1, args[4]);
                rs = stmt.executeQuery();
                if (!rs.next()) {
                    System.out.println("No such file stored.");
                } else {
                    Blob b = rs.getBlob(1);
                    BufferedOutputStream os;
                    os = new BufferedOutputStream(new FileOutputStream(f));
                    os.write(b.getBytes(0, (int) b.length()), 0, (int) b
                            .length());
                    os.flush();
                    os.close();
                }
            } else {
                // otherwise read it and save it to the database
                FileInputStream fis = new FileInputStream(f);
                byte[] tmp = new byte[1024]; // arbitrary size
                byte[] data = null;
                int sz, len = 0;
                while ((sz = fis.read(tmp)) != -1) {
                    if (data == null) {
                        len = sz;
                        data = tmp;
                    } else {
                        byte[] narr;
                        int nlen;
                        nlen = len + sz;
                        narr = new byte[nlen];
                        System.arraycopy(data, 0, narr, 0, len);
                        System.arraycopy(tmp, 0, narr, len, sz);
                        data = narr;
                        len = nlen;
                    }
                }
                if (len != data.length) {
                    byte[] narr = new byte[len];
                    System.arraycopy(data, 0, narr, 0, len);
                    data = narr;
                }
                stmt = con
                        .prepareStatement("INSERT INTO BlobTest  +
(fileName, "
                                + " blobData) VALUES(?, ?)");
                stmt.setString(1, args[4]);
                stmt.setObject(2, data);
                stmt.executeUpdate();
                f.delete();
            }
            con.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (con != null) {
                try {
                    con.close();
                } catch (Exception e) {
                }
            }
        }
    }
}

On 9/18/06, soussou97 <dboussebha@yahoo.fr> wrote:
>
>
> Hi Clinton;
>
> I understood well, I use the first solution (file) so you know or I can
> find
> an example ?
>
> Thanks;
>
>
> Clinton Begin wrote:
> >
> > You should use a straigt JDBC ResultSet, get the InputStream and write
> it
> > to
> > a file for further use.  Or if possible, stream it directly to the
> browser
> > (maybe with a file buffer/cache).  You shouldn't  keep 20 MB of data in
> > memory regardless of how you get it there.
> >
> > Clinton
> >
> >
> > On 9/18/06, soussou97 <dboussebha@yahoo.fr> wrote:
> >>
> >>
> >> Hi Larry;
> >>
> >> OK I willl ue JDBC + Rowset for getting the blob, have you a example or
> a
> >> implementaition ?
> >>
> >> Regards;
> >>
> >>
> >> Larry Meadors-2 wrote:
> >> >
> >> > For 20M of data? No.
> >> >
> >> > Larry
> >> >
> >> >
> >> > On 9/18/06, soussou97 <dboussebha@yahoo.fr> wrote:
> >> >>
> >> >> Hi;
> >> >>
> >> >> Please somebody can answer me if  its possible to use Ibatis (last
> >> >> version)
> >> >> for getting a big Blob (20 Mo) from a database (Oracle 10) with of
> the
> >> >> good
> >> >> performance ?
> >> >> If yes which lib (InputStream or sql.Blob) I must to use for
> >> extracting
> >> >> the
> >> >> data inside of blob (byte []) ?
> >> >>
> >> >> regards;
> >> >> --
> >> >> View this message in context:
> >> >> http://www.nabble.com/Ibatis-and-the-blob-tf2290439.html#a6361369
> >> >> Sent from the iBATIS - User - Java mailing list archive at
> Nabble.com.
> >> >>
> >> >>
> >> >
> >> >
> >>
> >> --
> >> View this message in context:
> >> http://www.nabble.com/Ibatis-and-the-blob-tf2290439.html#a6362935
> >> Sent from the iBATIS - User - Java mailing list archive at Nabble.com.
> >>
> >>
> >
> >
>
> --
> View this message in context:
> http://www.nabble.com/Ibatis-and-the-blob-tf2290439.html#a6366141
> Sent from the iBATIS - User - Java mailing list archive at Nabble.com.
>
>

Mime
View raw message